• API
• FAQ
• Tools
• Archive
SHARE
TWEET # tim_pa4_wordsearch a guest Feb 21st, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """
2. 1. search main_grid for first letter of first word to be searched
3. 2. if match, check surrounding letters if second letter matches
4. 3. if match again, continue checking if all letters in that direction match
5. 4. if yes, output index of first letter. if no, search for next occurrence of letter and repeat from step 2
6. 5. repeat for other words in words
7. """
8. """
9. For each word to search, output the row and column (two integers separated by a space) on one line,
10. corresponding to the row and column where the first letter of the word is found in the grid.
11. If the word appears multiple times in the grid, output the coordinates of the uppermost occurrence of the word in the grid.
12. If two or more occurrences are uppermost, output the coordinates for the leftmost occurrence in the grid.
13. All words are guaranteed to be found at least once in the grid.
14. """
15.
16. R, C = map(int, input().split())
17. main_grid = []
18. results_a = []
19. results_b = []
20. for i in range(R):
21.     main_grid.append(input()) # grid to be searched
22. N = int(input())
23. words = []
24. for i in range(N):
25.     words.append(input())
26. dirs = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]] # W, E, S, N, SW, NW, SE, NE
27.
28. i = 0
29. while i < N:
30.     coordinates = False
31.     for x in range(R): # row = x
32.         for y in range(C): # col = y
33.             if coordinates == False:
34.                 tempx = x
35.                 tempy = y
36.                 z = 0
37.                 coords = 0
38.                 count = 0
39.                 while z < len(words[i]):
40.                     if coords > 7:
41.                         break
42.                     a,b = dirs[coords]
43.                     if tempx + a >= 0 and tempx + a < R and tempy + b >= 0 and tempy + b < C and main_grid[tempx][tempy] == words[i][z] and z < len(words[i])-1: # searches all coords matching first letter of words[i]
44.                         tempx += a
45.                         tempy += b
46.                         count += 1
47.                         z += 1
48.                     elif main_grid[tempx][tempy] == words[i][z] and z == len(words[i])-1:
49.                         tempx = tempx + count*a
50.                         tempy = tempy + count*b
51.                         results_a.append(x+1)
52.                         results_b.append(y+1)
53.                         coordinates = True
54.                         break
55.                     else:
56.                         tempx = x
57.                         tempy = y
58.                         z = 0
59.                         count = 0
60.                         coords += 1
61.                         continue
62.     i += 1
63.
64. for x in range(len(words)):
65.     print("{} {}". format(results_a[x],results_b[x]))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top