Breaking

Saturday 30 September 2017

March of the King

             March of the King

                                                            Click Here to solve the problem
The white king George lives all alone in a chessboard (A standard chessboard consists of  rows and columns). He lives alone because the evil black king Andrew killed all the other pieces in frustration after George beat him in a game of chess.
Wandering all alone with no purpose and literally bored to death, King George invented a new game to keep him occupied. King George has a secret word in his mind. It is the word he used to shout instead of the usual "checkmate" after beating the black king in a chess game. King George loves to march, so he decided to take a tour through the chessboard. He assigns a single lowercase letter to each square of the chessboard first. He can then start his tour from any square on the chessboard. From a square, the king can move to any neighboring square inside the board as in the game of chess. However, it is not allowed to move to the same square twice during the journey. In other words, all the squares visited by the king must be distinct.
After completing his tour (the tour can be stopped anytime), George concatenates all the characters in the order of his travel to form a word. Since he has nothing better to do, the white king wants to know how many different ways he can complete the tour so that the secret word is formed afterward.
Two ways are considered different if a different square is visited at any moment. Note that the order in which the squares are visited also matters. So for example the path  and  are considered different.

Formally the problem is as follows:
Let  be the lowercase letter assigned to square . Also, let the secret word be .
Your task is to find the number of possible King's tours. A King's tour is a sequence of coordinates  satisfying:
  •  for all 
  •  for all 
  •  for all 
Input Format
The first line of input consists of a single integer  denoting the length of the secret word. The next line contains the secret word consisting of  lowercase letters. The next  lines each contain  characters describing the lowercase letters assigned to the chessboard in row-major order.
Constraints
Subtasks
  • For  of the maximum points, 
Output Format
Print a single line containing a single integer denoting the number of different tours forming the secret word.
Sample Input 0
2
aa
aabbbbbb
aabbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
Sample Output 0
12
Explanation 0
In this case, the secret word is  and is  letters long. There are  valid tours that form this word:
Image of 12 tours forming the word "aa"
Sample Input 1
9
checkmate
checkmat
checkmee
checkmat
checkmee
checkmat
checkmee
checkmat
checkmee
Sample Output 1
7698
Explanation 1
In this case, the secret word is  and is  letters long. There are  valid tours that form this word, which is too large to enumerate, so we'll just provide two tours as examples:

Please do comment If u have any Queries!

No comments:

Post a Comment

Like