回答編集履歴
2
誤字
test
CHANGED
@@ -176,7 +176,9 @@
|
|
176
176
|
|
177
177
|
|
178
178
|
|
179
|
+
解法③
|
180
|
+
|
179
|
-
|
181
|
+
```Python
|
180
182
|
|
181
183
|
from collections import deque
|
182
184
|
|
1
修正
test
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
解答を頂いたお二方のアプローチをそれぞれ以下に示しておきます:
|
2
|
+
|
3
|
+
|
4
|
+
|
2
|
-
|
5
|
+
解法①
|
3
|
-
|
4
6
|
|
5
7
|
```Python
|
6
8
|
|
@@ -93,3 +95,159 @@
|
|
93
95
|
dic[x, y + 1] += 1
|
94
96
|
|
95
97
|
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
解法②
|
102
|
+
|
103
|
+
```Python
|
104
|
+
|
105
|
+
from collections import deque
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
R, C = map(int, input().split())
|
110
|
+
|
111
|
+
sy, sx = map(int, input().split())
|
112
|
+
|
113
|
+
gy, gx = map(int, input().split())
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
maze = []
|
118
|
+
|
119
|
+
for _ in range(R):
|
120
|
+
|
121
|
+
li = list(str(input()))
|
122
|
+
|
123
|
+
li = [9 if el == '#' else 0 for el in li]
|
124
|
+
|
125
|
+
maze.append(li)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
maze[sy - 1][sx - 1] = 2
|
130
|
+
|
131
|
+
maze[gy - 1][gx - 1] = 1
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
data = deque([[sy - 1, sx - 1, 0]])
|
136
|
+
|
137
|
+
while data[0]:
|
138
|
+
|
139
|
+
x, y, d = data.popleft()
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if x == gy - 1 and y == gx - 1:
|
144
|
+
|
145
|
+
print(d)
|
146
|
+
|
147
|
+
break
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
if maze[x - 1][y] < 2:
|
152
|
+
|
153
|
+
data.append([x - 1, y, d + 1])
|
154
|
+
|
155
|
+
maze[x - 1][y] = 2
|
156
|
+
|
157
|
+
if maze[x + 1][y] < 2:
|
158
|
+
|
159
|
+
data.append([x + 1, y, d + 1])
|
160
|
+
|
161
|
+
maze[x + 1][y] = 2
|
162
|
+
|
163
|
+
if maze[x][y - 1] < 2:
|
164
|
+
|
165
|
+
data.append([x, y - 1, d + 1])
|
166
|
+
|
167
|
+
maze[x][y - 1] = 2
|
168
|
+
|
169
|
+
if maze[x][y + 1] < 2:
|
170
|
+
|
171
|
+
data.append([x, y + 1, d + 1])
|
172
|
+
|
173
|
+
maze[x][y + 1] = 2
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
解法③```Python
|
180
|
+
|
181
|
+
from collections import deque
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
R, C = map(int, input().split())
|
186
|
+
|
187
|
+
sy, sx = map(int, input().split())
|
188
|
+
|
189
|
+
gy, gx = map(int, input().split())
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
maze = []
|
194
|
+
|
195
|
+
for _ in range(R):
|
196
|
+
|
197
|
+
li = list(str(input()))
|
198
|
+
|
199
|
+
li = [9 if el == '#' else 0 for el in li]
|
200
|
+
|
201
|
+
maze.append(li)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
maze[gy - 1][gx - 1] = 1
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
data = deque([[sy - 1, sx - 1, 0]])
|
210
|
+
|
211
|
+
while data[0]:
|
212
|
+
|
213
|
+
x, y, d = data.popleft()
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
if maze[x][y] == 1:
|
218
|
+
|
219
|
+
print(d)
|
220
|
+
|
221
|
+
exit()
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
if maze[x][y] >= 2:
|
226
|
+
|
227
|
+
continue
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
maze[x][y] = 2
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
if maze[x - 1][y] < 2:
|
236
|
+
|
237
|
+
data.append([x - 1, y, d + 1])
|
238
|
+
|
239
|
+
if maze[x + 1][y] < 2:
|
240
|
+
|
241
|
+
data.append([x + 1, y, d + 1])
|
242
|
+
|
243
|
+
if maze[x][y - 1] < 2:
|
244
|
+
|
245
|
+
data.append([x, y - 1, d + 1])
|
246
|
+
|
247
|
+
if maze[x][y + 1] < 2:
|
248
|
+
|
249
|
+
data.append([x, y + 1, d + 1])
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
```
|