@@ -138,9 +138,129 @@ int main() {
138
138
139
139
### Java
140
140
141
+ ``` JAVA
142
+
143
+ import java.util.Scanner ;
144
+
145
+ public class Main {
146
+ static int [][] dir = { {- 1 , 0 }, {0 , - 1 }, {1 , 0 }, {0 , 1 } }; // 保存四个方向
147
+
148
+ public static void dfs (int [][] grid , int x , int y ) {
149
+ grid[x][y] = 2 ;
150
+ for (int [] d : dir) {
151
+ int nextX = x + d[0 ];
152
+ int nextY = y + d[1 ];
153
+ // 超过边界
154
+ if (nextX < 0 || nextX >= grid. length || nextY < 0 || nextY >= grid[0 ]. length) continue ;
155
+ // 不符合条件,不继续遍历
156
+ if (grid[nextX][nextY] == 0 || grid[nextX][nextY] == 2 ) continue ;
157
+ dfs(grid, nextX, nextY);
158
+ }
159
+ }
160
+
161
+ public static void main (String [] args ) {
162
+ Scanner scanner = new Scanner (System . in);
163
+ int n = scanner. nextInt();
164
+ int m = scanner. nextInt();
165
+ int [][] grid = new int [n][m];
166
+
167
+ for (int i = 0 ; i < n; i++ ) {
168
+ for (int j = 0 ; j < m; j++ ) {
169
+ grid[i][j] = scanner. nextInt();
170
+ }
171
+ }
172
+
173
+ // 步骤一:
174
+ // 从左侧边,和右侧边 向中间遍历
175
+ for (int i = 0 ; i < n; i++ ) {
176
+ if (grid[i][0 ] == 1 ) dfs(grid, i, 0 );
177
+ if (grid[i][m - 1 ] == 1 ) dfs(grid, i, m - 1 );
178
+ }
179
+
180
+ // 从上边和下边 向中间遍历
181
+ for (int j = 0 ; j < m; j++ ) {
182
+ if (grid[0 ][j] == 1 ) dfs(grid, 0 , j);
183
+ if (grid[n - 1 ][j] == 1 ) dfs(grid, n - 1 , j);
184
+ }
185
+
186
+ // 步骤二、步骤三
187
+ for (int i = 0 ; i < n; i++ ) {
188
+ for (int j = 0 ; j < m; j++ ) {
189
+ if (grid[i][j] == 1 ) grid[i][j] = 0 ;
190
+ if (grid[i][j] == 2 ) grid[i][j] = 1 ;
191
+ }
192
+ }
193
+
194
+ for (int i = 0 ; i < n; i++ ) {
195
+ for (int j = 0 ; j < m; j++ ) {
196
+ System . out. print(grid[i][j] + " " );
197
+ }
198
+ System . out. println();
199
+ }
200
+
201
+ scanner. close();
202
+ }
203
+ }
204
+
205
+
206
+ ```
207
+
208
+
141
209
### Python
142
210
143
- #### 广搜版
211
+
212
+ ``` python
213
+
214
+ def dfs (grid , x , y ):
215
+ grid[x][y] = 2
216
+ directions = [(- 1 , 0 ), (0 , - 1 ), (1 , 0 ), (0 , 1 )] # 四个方向
217
+ for dx, dy in directions:
218
+ nextx, nexty = x + dx, y + dy
219
+ # 超过边界
220
+ if nextx < 0 or nextx >= len (grid) or nexty < 0 or nexty >= len (grid[0 ]):
221
+ continue
222
+ # 不符合条件,不继续遍历
223
+ if grid[nextx][nexty] == 0 or grid[nextx][nexty] == 2 :
224
+ continue
225
+ dfs(grid, nextx, nexty)
226
+
227
+ def main ():
228
+ n, m = map (int , input ().split())
229
+ grid = [[int (x) for x in input ().split()] for _ in range (n)]
230
+
231
+ # 步骤一:
232
+ # 从左侧边,和右侧边 向中间遍历
233
+ for i in range (n):
234
+ if grid[i][0 ] == 1 :
235
+ dfs(grid, i, 0 )
236
+ if grid[i][m - 1 ] == 1 :
237
+ dfs(grid, i, m - 1 )
238
+
239
+ # 从上边和下边 向中间遍历
240
+ for j in range (m):
241
+ if grid[0 ][j] == 1 :
242
+ dfs(grid, 0 , j)
243
+ if grid[n - 1 ][j] == 1 :
244
+ dfs(grid, n - 1 , j)
245
+
246
+ # 步骤二、步骤三
247
+ for i in range (n):
248
+ for j in range (m):
249
+ if grid[i][j] == 1 :
250
+ grid[i][j] = 0
251
+ if grid[i][j] == 2 :
252
+ grid[i][j] = 1
253
+
254
+ # 打印结果
255
+ for row in grid:
256
+ print (' ' .join(map (str , row)))
257
+
258
+ if __name__ == " __main__" :
259
+ main()
260
+ ```
261
+
262
+
263
+ 广搜版
144
264
``` Python
145
265
from collections import deque
146
266
@@ -194,6 +314,7 @@ for i in range(n):
194
314
195
315
for row in g:
196
316
print (" " .join(map (str , row)))
317
+
197
318
```
198
319
199
320
0 commit comments