因为人家就这么设计的~
by zhangbonan1121 @ 2024-03-21 17:47:47
@[zhu5001210249](/user/671246) 我只知道,请把程序改成这样:
```java
//不要加package com.LQ3yue16;
import java.util.Scanner;
//请用 public class Main public class P1506 {
public class Main {
//可以在外面加上一圈0,这个样子在最外面开始搜0,就不会漏掉0,把0变成墙
static int n;
static int m;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static char[][] ans = new char[505][505];
public static void dfs(int x,int y) {
ans[x][y] = '*';
for(int i=0;i<4;i++) {
int bx = x + dx[i];
int by = y + dy[i];
if(bx>=0 && by>=0 && bx<=n+1 && by<=m+1 && ans[bx][by]=='0') {
dfs(bx,by);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
char[][] res = new char[505][505];
//输入字符串一定是0开始的
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
char c = s.charAt(j);
res[i][j] = c;
}
}
//把数组整体下标都往前一位
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
ans[i][j] = res[i-1][j-1];
}
}
dfs(0,0);
int count=0;
for(int i=1;i<=n;i++) {
for (int j = 1; j <= m; j++) {
if(ans[i][j]=='0') {
count++;
}
}
System.out.println();
}
System.out.println(count);
}
}
```
by xk2013 @ 2024-03-21 17:48:51
```java
import java.util.Scanner;
public class Main {
//可以在外面加上一圈0,这个样子在最外面开始搜0,就不会漏掉0,把0变成墙
static int n;
static int m;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int[][] ans = new int[505][505];
public static void dfs(int x,int y) {
ans[x][y] = 1;
for(int i=0;i<4;i++) {
int bx = x + dx[i];
int by = y + dy[i];
if(bx>=0 && by>=0 && bx<=n+1 && by<=m+1 && ans[bx][by]==0) {
dfs(bx,by);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
int[][] res = new int[505][505];
//输入字符串一定是0开始的
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
char c = s.charAt(j);
//在这里转换为int型的,没有搞明白为什么字符java他不会判断
if(c=='0'){
res[i][j]=0;
}
if(c=='*'){
res[i][j] = 1;
}
}
}
//把数组整体下标都往前一位
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
ans[i][j] = res[i-1][j-1];
}
}
dfs(0,0);
int count=0;
for(int i=1;i<=n;i++) {
for (int j = 1; j <= m; j++) {
if(ans[i][j]==0) {
count++;
}
}
}
System.out.println(count);
}
}
```
@[xk2013](/user/998662)
你这个样子改,java运行还是没有修改字符,我直接换为int型,他就可以修改,太离谱了
by zhu5001210249 @ 2024-03-21 17:54:28
呵呵
by ci_xi_peng_yu_yan @ 2024-03-21 17:58:15