Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- r, c = map(int, input().split())
- grid = [input() for _ in range(r)]
- n = int(input())
- words = [input() for _ in range(n)]
- word_coors = {}
- '''
- grid = ['xatnysnprabvbas', 'teehsdaerpslyug', 'soqgknsscoklpsn', 'ochmacaeceteeei', 'fyasarnprrrhfak']
- r, c = len(grid), len(grid[0])
- words = ['bar', 'engine', 'key', 'networking', 'save', 'scan', 'scanner', 'screen', 'screenshot',
- 'script', 'scroll', 'search', 'security', 'server', 'shareware', 'shell', 'shift', 'snapshot',
- 'social', 'software', 'spam', 'spammer', 'spreadsheet', 'spyware', 'status', 'storage',
- 'supercomputer', 'surf', 'syntax']
- word_coors = {}
- '''
- # 0: right, rotation cc
- direction = [
- (1, 0),
- (1, 1),
- (0, 1),
- (-1, 1),
- (-1, 0),
- (-1, -1),
- (0, -1),
- (1, -1)
- ]
- def search_direction(x, y, d, w, i=0):
- if grid[y][x] == w[i]:
- if len(word) - 1 == i:
- return True
- else:
- x += direction[d][0]
- y += direction[d][1]
- if 0 <= x < c and 0 <= y < r:
- i += 1
- if i < len(word):
- return search_direction(x, y, d, w, i)
- else:
- return False
- else:
- return False
- else:
- return False
- def search_cell(x, y, word):
- search = [search_direction(x, y, d, word) for d in range(8)]
- if True in search:
- return True
- else:
- return False
- for word in words:
- for y in range(r):
- for x in range(c):
- if word[0] == grid[y][x]:
- if search_cell(x, y, word) and word not in word_coors:
- word_coors[word] = (y+1, x+1)
- for word in words:
- print(*word_coors[word])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement