错了一个数据,但是找不出哪里错了,请大佬给看一下,万分感谢!
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 25;
char str[N][N];
bool vis[N][N];
int m, n, x, y;
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dirx[4] = {-1, 0, 1, 0};
int diry[4] = {0, -1, 0, 1};
int check(int x, int y){
int ans = 0;
for(int i = 0; i < 4; i ++){
int tx = x + dirx[i];
int ty = y + diry[i];
if(tx >= 0 && tx <= m + 1 && ty >= 0 && ty <= n + 1){
if(str[tx][ty] == '.')
ans ++;
}
}
return ans;
}
void dfs(int x, int y){
for(int i = 0; i < 8; i ++){
int tx = x + dx[i];
int ty = y + dy[i];
if(tx >= 1 && tx <= m && ty >= 1 && ty <= n && !vis[tx][ty] && str[tx][ty] == 'X'){
vis[tx][ty] = true;
dfs(tx, ty);
}
}
return;
}
int main(){
cin >> m >> n >> x >> y;
for(int i = 0; i <= m + 1; i ++)
str[i][0] = '.', str[i][n + 1] = '.';
for(int i = 0; i <= n + 1; i ++)
str[0][i] = '.', str[m + 1][i] = '.'; //在四周加点
for(int i = 1; i <= m; i ++){
for(int j = 1; j <= n; j ++)
cin >> str[i][j];
}
dfs(x, y);
int ans = 0;
for(int i = 1; i <= m; i ++){
for(int j = 1; j <= n; j ++){
if(vis[i][j]){
ans += check(i, j);
}
}
}
cout << ans << endl;
return 0;
}