真的改不动了 蒟蒻求助呜呜呜BFS
  • 板块P1141 01迷宫
  • 楼主Otion
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/1/6 13:33
  • 上次更新2023/10/24 05:24:34
查看原帖
真的改不动了 蒟蒻求助呜呜呜BFS
843596
Otion楼主2023/1/6 13:33
#include <bits/stdc++.h>
using namespace std;

struct node
{
    int row;
    int col;
};
queue<node> line;
const int N = 1005;
int map_map[N][N];
int view[N][N];
int scale;
int find_times;
int step;
int shift_x[] = {1, -1, 0, 0};
int shift_y[] = {0, 0, -1, 1};
void bfs(int origin_row, int origin_col, int num)
{
    step++;
    node start;
    start.row = origin_row;
    start.col = origin_col;
    view[origin_row][origin_col] = 1;
    line.push(start);
    int flag = num;
    while (!line.empty())
    {
        int front_row = line.front().row;
        int front_col = line.front().col;
        if (flag == 0)
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }
        for (int i = 0; i < 4; i++)
        {
            if (front_row + shift_x[i] < 0 || front_col + shift_y[i] < 0)
            {
                continue;
            }
            if (front_row + shift_x[i] >= scale || front_col + shift_y[i] >= scale)
            {
                continue;
            }
            if (map_map[front_row + shift_x[i]][front_col + shift_y[i]] == flag)
            {
                if (view[front_row + shift_x[i]][front_col + shift_y[i]] == 0)
                {
                    step++;
                    node temp;
                    temp.row = front_row + shift_x[i];
                    temp.col = front_col + shift_y[i];
                    view[front_row + shift_x[i]][front_col + shift_y[i]] = 1;
                    line.push(temp);
                }
            }
        }
        line.pop();
    }
}
int main()
{
    cin >> scale >> find_times;
    for (int i = 0; i < scale; i++)
    {
        for (int j = 0; j < scale; j++)
        {
            scanf("%1d", &map_map[i][j]);
        }
    }
    while (find_times--)
    {
        int target_row, target_col;
        cin >> target_row >> target_col;
        bfs(target_row - 1, target_col - 1, map_map[target_row - 1][target_col - 1]);
        cout << step << endl;
        step = 0;
        memset(view, 0, sizeof(view));
    }
    return 0;
}
### ```
2023/1/6 13:33
加载中...