请问这道题为什么用string数组RE,但是用char就过了?
RE代码:
#include <bits/stdc++.h>
using namespace std;
string s[105];
int gx[4] = {-1,0,1,0},gy[4] = {0,1,0,-1};
int n,m,ans;
void check(int x,int y){
for (int i = 0;i < 4;i++){
int tx = x + gx[i];
int ty = y + gy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' || s[tx][ty] == '0') continue;
s[tx][ty] = '#';
check(tx,ty);
}
}
int main(){
cin >> n>> m;
for (int i =1;i <= n;i++){
for (int j = 1;j <= m;j++){
cin >> s[i][j];
}
}
for (int i =1;i <= n;i++){
for (int j = 1;j <= m;j++){
if (s[i][j] >= '1' && s[i][j] <= '9'){
check(i,j);
ans++;
}
}
}
cout << ans;
return 0;
}
AC代码:
#include <bits/stdc++.h>
using namespace std;
char c[105][105];
int gx[4] = {-1,0,1,0},gy[4] = {0,1,0,-1};
int n,m,ans;
void check(int x,int y){
for (int i = 0;i < 4;i++){
int tx = x + gx[i];
int ty = y + gy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m || c[tx][ty] == '#' || c[tx][ty] == '0') continue;
c[tx][ty] = '#';
check(tx,ty);
}
}
int main(){
cin >> n>> m;
for (int i =1;i <= n;i++){
for (int j = 1;j <= m;j++){
cin >> c[i][j];
}
}
for (int i =1;i <= n;i++){
for (int j = 1;j <= m;j++){
if (c[i][j] >= '1' && c[i][j] <= '9'){
check(i,j);
ans++;
}
}
}
cout << ans;
return 0;
}