質問編集履歴
1
修正後のコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -152,6 +152,114 @@
|
|
152
152
|
|
153
153
|
|
154
154
|
|
155
|
+
以下ご指摘のdfs(x,y)→dfs(nx,ny)などの修正を加えたコードです。
|
156
|
+
|
157
|
+
```python
|
158
|
+
|
159
|
+
import sys
|
160
|
+
|
161
|
+
import logging
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
logging.basicConfig(level=logging.CRITICAL)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
sys.setrecursionlimit(5000)
|
170
|
+
|
171
|
+
LAKE = [
|
172
|
+
|
173
|
+
'wwwwwww.',
|
174
|
+
|
175
|
+
'ww......',
|
176
|
+
|
177
|
+
'....w..w']
|
178
|
+
|
179
|
+
# strをlistに変換する
|
180
|
+
|
181
|
+
for i in range(len(LAKE)):
|
182
|
+
|
183
|
+
LAKE[i] = list(LAKE[i])
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
logging.info(str(LAKE))
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
row_size = len(LAKE)
|
192
|
+
|
193
|
+
col_size = len(LAKE[0])
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
def dfs(x, y):
|
200
|
+
|
201
|
+
LAKE[x][y] = '.'
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
for dx in [-1,0, 1]:
|
206
|
+
|
207
|
+
for dy in [-1,0, 1]:
|
208
|
+
|
209
|
+
nx = x + dx
|
210
|
+
|
211
|
+
ny = y + dy
|
212
|
+
|
213
|
+
if (0 <= nx < row_size) and (0 <= ny < col_size):
|
214
|
+
|
215
|
+
logging.info('nx:{},ny:{}'.format(nx, ny))
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if LAKE[nx][ny] == 'w':
|
220
|
+
|
221
|
+
dfs(nx, ny)
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def solve():
|
228
|
+
|
229
|
+
res = 0
|
230
|
+
|
231
|
+
for row in range(row_size):
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
for col in range(col_size):
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
if LAKE[row][col] == 'w':
|
240
|
+
|
241
|
+
# 'w'の部分から再帰関数を開始する
|
242
|
+
|
243
|
+
dfs(row, col)
|
244
|
+
|
245
|
+
# 再帰関数が一段落したら++
|
246
|
+
|
247
|
+
res += 1
|
248
|
+
|
249
|
+
return res
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
print('lake :', solve())
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
|
262
|
+
|
155
263
|
### 補足情報(FW/ツールのバージョンなど)
|
156
264
|
|
157
265
|
|