回答編集履歴

1

コード修正

2021/07/13 02:58

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -54,17 +54,51 @@
54
54
 
55
55
  ```python
56
56
 
57
+ # coding: utf-8
58
+
59
+ # Your code here!
60
+
61
+
62
+
63
+ maze = [
64
+
65
+ [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
66
+
67
+ [9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
68
+
69
+ [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 9],
70
+
71
+ [9, 9, 0, 0, 0, 0, 0, 0, 0, 9, 0, 9],
72
+
73
+ [9, 9, 0, 0, 9, 9, 9, 9, 0, 9, 0, 9],
74
+
75
+ [9, 9, 9, 0, 0, 0, 0, 9, 0, 9, 0, 9],
76
+
77
+ [9, 9, 9, 9, 9, 9, 0, 9, 0, 9, 0, 9],
78
+
79
+ [9, 1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9],
80
+
81
+ [9, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9],
82
+
83
+ [9, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9],
84
+
85
+ [9, 9, 0, 0, 0, 0, 0, 9, 0, 0, 0, 9],
86
+
87
+ [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
88
+
89
+ ]
90
+
91
+
92
+
93
+
94
+
57
95
  def dfs(x, y, d):
58
96
 
59
97
  if maze[x][y] == 1:
60
98
 
61
- print(d)
62
-
63
99
  return d
64
100
 
65
- #exit()
101
+
66
-
67
-
68
102
 
69
103
  maze[x][y] = 2
70
104
 
@@ -72,22 +106,52 @@
72
106
 
73
107
  if maze[x - 1][y] < 2:
74
108
 
75
- return dfs(x - 1, y, d + 1)
109
+ r = dfs(x - 1, y, d + 1)
110
+
111
+ if r is not None:
112
+
113
+ return r
114
+
115
+
76
116
 
77
117
  if maze[x + 1][y] < 2:
78
118
 
79
- return dfs(x + 1, y, d + 1)
119
+ r = dfs(x + 1, y, d + 1)
120
+
121
+ if r is not None:
122
+
123
+ return r
124
+
125
+
80
126
 
81
127
  if maze[x][y - 1] < 2:
82
128
 
83
- return dfs(x, y - 1, d + 1)
129
+ r = dfs(x, y - 1, d + 1)
130
+
131
+ if r is not None:
132
+
133
+ return r
134
+
135
+
84
136
 
85
137
  if maze[x][y + 1] < 2:
86
138
 
87
- return dfs(x, y + 1, d + 1)
139
+ r = dfs(x, y + 1, d + 1)
140
+
141
+ if r is not None:
142
+
143
+ return r
88
144
 
89
145
 
90
146
 
91
147
  maze[x][y] = 0
92
148
 
149
+ return None
150
+
151
+
152
+
153
+
154
+
155
+ print(dfs(1, 1, 0))
156
+
93
157
  ```