openjudge!!!错在哪里了???为啥RE?
附代码:#include<cstdio>usingnamespacestd;intgetint(){intr=0,k=1;charc=getchar();for(;'0'...
附代码:
#include<cstdio>using namespace std;int getint() { int r = 0, k = 1; char c = getchar(); for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1; for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c; return r * k;}const int maxn = 2333;int dir[4][2] ={ {0, -1}, {0, 1}, {1, 0}, {-1, 0}};int r, c, sum = 0;int field[maxn][maxn];int dfs(int x, int y){ field[x][y] = 1; for(int i = 0;i < 4;i++) { int n = x + dir[i][0]; int m = y + dir[i][1]; if(n < 0 || n > r || m < 0 || m > c) { break; } if(field[n][m] == 2) { dfs(n, m); } }}int main(){ r = getint(); c = getint(); for(int i = 0;i < r;i++) { char ch[10]; scanf("%s", ch); for(int j = 0;j < c;j++) { if(ch[j] == '.') { field[i][j] = 1; } if(ch[j] == '#') { field[i][j] = 2; } } } for(int i = 0;i < r;i++) { for(int j = 0;j < c;j++) { if(field[i][j] == 2) { dfs(i, j); sum++; } } } printf("%d", sum); return 0;} 展开
#include<cstdio>using namespace std;int getint() { int r = 0, k = 1; char c = getchar(); for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1; for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c; return r * k;}const int maxn = 2333;int dir[4][2] ={ {0, -1}, {0, 1}, {1, 0}, {-1, 0}};int r, c, sum = 0;int field[maxn][maxn];int dfs(int x, int y){ field[x][y] = 1; for(int i = 0;i < 4;i++) { int n = x + dir[i][0]; int m = y + dir[i][1]; if(n < 0 || n > r || m < 0 || m > c) { break; } if(field[n][m] == 2) { dfs(n, m); } }}int main(){ r = getint(); c = getint(); for(int i = 0;i < r;i++) { char ch[10]; scanf("%s", ch); for(int j = 0;j < c;j++) { if(ch[j] == '.') { field[i][j] = 1; } if(ch[j] == '#') { field[i][j] = 2; } } } for(int i = 0;i < r;i++) { for(int j = 0;j < c;j++) { if(field[i][j] == 2) { dfs(i, j); sum++; } } } printf("%d", sum); return 0;} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询