dfs 70pts求助
  • 板块P1141 01迷宫
  • 楼主Tjaweiof
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/4/1 19:43
  • 上次更新2023/10/23 19:44:20
查看原帖
dfs 70pts求助
550933
Tjaweiof楼主2023/4/1 19:43
#include <bits/stdc++.h>
using namespace std;
int s[1010][1010], cnt = 0, ans[1010][1010], n, m;
int nextstep[6][3] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void dfs(int x, int y){
    if (x > n || x < 1 || y > n || y < 1) return;
    if (ans[x][y] > -1) return;
    cnt++;
    ans[x][y] = 0;
    for (int i = 0; i < 4; i++){
        if (s[x + nextstep[i][0]][y + nextstep[i][1]] != s[x][y]) dfs(x + nextstep[i][0], y + nextstep[i][1]);
    }
}
int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        char str[100005];
        cin >> str;
        for(int j = 1; j <= n; j++){
            s[i][j] = str[j - 1] - 48;
        }
    }
    for(int i = 1; i <= m; i++){
        memset(ans, -1, sizeof(ans));
        int a, b;
        cnt = 0;
        cin >> a >> b;
        dfs(a, b);
        cout << cnt << endl;
    }
}
2023/4/1 19:43
加载中...