-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitch Plays VIIIbit Explorer - 25319번.py
101 lines (87 loc) · 2.38 KB
/
Twitch Plays VIIIbit Explorer - 25319번.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#https://www.acmicpc.net/problem/25319
#https://www.acmicpc.net/source/80762562
from collections import deque
def main():
n,m,s_length = map(int,input().split())
graph = []
all_alphbet_cnt = {}
target_alphbet_cnt = {}
for _ in range(n):
graph.append(list(input()))
alphabet_position ={}
for i in range(n):
for j in range(m):
if graph[i][j] in all_alphbet_cnt:
all_alphbet_cnt[graph[i][j]] += 1
alphabet_position[graph[i][j]].append((i,j))
else:
all_alphbet_cnt[graph[i][j]] = 1
alphabet_position[graph[i][j]] = deque([(i,j)])
s = list(input())
for c in s:
if c not in target_alphbet_cnt:
target_alphbet_cnt[c] = 1
else:
target_alphbet_cnt[c] += 1
C = int(1e9)
for c in s:
if c not in all_alphbet_cnt:
C = 0
break
C = min(C,all_alphbet_cnt[c]//target_alphbet_cnt[c])
print(C,end=" ")
path = deque([(0,0)])
for _ in range(C):
for c in s:
path.append(alphabet_position[c].popleft())
path.append((n-1,m-1))
solution(path)
def solution(path):
move_cnt = 0
sentence = ""
vx,vy = path.popleft()
nx,ny = path.popleft()
while path:
if vx == nx and vy == ny:
sentence += 'P'
move_cnt += 1
nx,ny = path.popleft()
if vx > nx:
move_cnt += 1
vx -= 1
sentence += 'U'
if vx < nx:
move_cnt += 1
vx += 1
sentence += 'D'
if vy > ny:
move_cnt += 1
vy -= 1
sentence += 'L'
if vy < ny:
move_cnt += 1
vy += 1
sentence += 'R'
while True:
if vx == nx and vy == ny:
break
if vx > nx:
move_cnt += 1
vx -= 1
sentence += 'U'
if vx < nx:
move_cnt += 1
vx += 1
sentence += 'D'
if vy > ny:
move_cnt += 1
vy -= 1
sentence += 'L'
if vy < ny:
move_cnt += 1
vy += 1
sentence += 'R'
print(move_cnt)
print(sentence)
if __name__ == "__main__":
main()