70分超时了求求大佬救救我
  • 板块P1141 01迷宫
  • 楼主KQYaili
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/5/14 23:44
  • 上次更新2023/10/28 01:25:19
查看原帖
70分超时了求求大佬救救我
704882
KQYaili楼主2022/5/14 23:44
#include <bits/stdc++.h>
using namespace std;
bool visit[1005][1005];
char s[1005][1005];
int ans = 0;
int n, m, x, y;
queue<pair<int, int> > q;
bool fact(int xx, int yy) {
    if (xx<1 || xx>n || yy<1 || yy>n || visit[xx][yy]) return false;
    return true;
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> s[i][j];
        }
    }
    for (int i = 0; i < m; i++) {
        ans = 1;
        cin >> x >> y;   
        q.push(make_pair(x, y));
        memset(visit, false, sizeof(visit));
        visit[x][y] = true;
        while (!q.empty()) {//将上下左右四个方向全部判断一遍
            int x_now = q.front().first;
            int y_now = q.front().second;//遍历
            q.pop();
            if (s[x_now][y_now] + s[x_now - 1][y_now] - 2 * '0' == 1) {
                int xx = x_now - 1;
                int yy = y_now;
                if (fact(xx, yy)) {//判断能否移动
                    visit[xx][yy] = true;//标记走过
                    ans++;
                    q.push(make_pair(xx, yy));
                }
            }
            if (s[x_now][y_now] + s[x_now + 1][y_now] - 2 * '0' == 1) {
                int xx = x_now + 1;
                int yy = y_now;
                if (fact(xx, yy)) {
                    visit[xx][yy] = true;
                    ans++;
                    q.push(make_pair(xx, yy));
                }
            }
            if (s[x_now][y_now] + s[x_now][y_now + 1] - 2 * '0' == 1) {
                int xx = x_now;
                int yy = y_now + 1;
                if (fact(xx, yy)) {
                    visit[xx][yy] = true;
                    ans++;
                    q.push(make_pair(xx, yy));
                }
            }
            if (s[x_now][y_now] + s[x_now][y_now - 1] - 2 * '0' == 1) {
                int xx = x_now;
                int yy = y_now - 1;
                if (fact(xx, yy)) {
                    visit[xx][yy] = true;
                    ans++;
                    q.push(make_pair(xx, yy));
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}
2022/5/14 23:44
加载中...