Word Search in a Grid
InterviewTime limit2sMemory limit512 MB
Decide whether a word appears in a grid along a straight line of neighboring cells in any of the eight directions.
- Level
Medium4 of 10
- Topics
- Array, Simulation, Implementation, Brute force
- Solved
- No attempts yet
Problem
An grid holds one uppercase letter in each cell. Given a word , write a program that decides whether the word is in the grid.
The word is in the grid when you can start at one cell and read the letters of in order through neighboring cells. The direction can be up, down, left, right, or one of the four diagonals: upper left, upper right, lower left, lower right. The direction must not change partway.
Input
The first line contains the word . The length of is at most 100, and consists of uppercase letters only.
The second line contains the number of rows and the number of columns of the grid. and are natural numbers at most 100.
Each of the next lines contains the letters of one row of the grid, with no spaces.
Output
Print 1 if the word is in the given grid, and 0 if it is not.