社区讨论

spfa模板题,WA on#3,4,5,蒟蒻求调

P4554小明的游戏参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo2icvxv
此快照首次捕获于
2023/10/23 14:19
2 年前
此快照最后确认于
2023/10/23 14:19
2 年前
查看原帖
CPP
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int a = 10100;

int total_row;
int total_col;
char map[550][550];
int head[1000010];
int area[1000010];
int next_area[1000010];
int scale[1000010];
int idx;
int dis[1000010];
bool state[1000010];
int start_row;
int start_col;
int end_row;
int end_col;
int shift_x[] = {-1, 1, 0, 0};
int shift_y[] = {0, 0, 1, -1};
queue<int> line;
void add(int a, int b, int c)
{
    area[idx] = b;
    next_area[idx] = head[a];
    head[a] = idx;
    scale[idx] = c;
    idx++;
}

void spfa()
{
    line.push(start_row * total_col + start_col);
    state[start_row * total_col + start_col] = 1;
    while (line.size())
    {
        int line_head = line.front();
        line.pop();
        state[start_row * total_col + start_col] = 0;
        for (int i = head[line_head]; i != -1; i = next_area[i])
        {
            int now_area = area[i];
            if (dis[now_area] > dis[line_head] + scale[i])
            {
                dis[now_area] = dis[line_head] + scale[i];
                if (state[now_area] == 1)
                {
                    continue;
                }
                else
                {
                    line.push(now_area);
                    state[now_area] = 1;
                }
            }
        }
    }
}

int main()
{
    while (1)
    {
        cin >> total_row >> total_col;
        if (total_row == 0 && total_col == 0)
        {
            break;
        }
        for (int i = 0; i < total_row; i++)
        {
            for (int j = 0; j < total_col; j++)
            {
                cin >> map[i][j];
            }
        }
        memset(head, -1, sizeof(head));
        for (int i = 0; i < total_row; i++)
        {
            for (int j = 0; j < total_col; j++)
            {
                for (int z = 0; z < 4; z++)
                {
                    int new_row = i + shift_x[z];
                    int new_col = j + shift_y[z];
                    if (new_row < 0 || new_row >= total_row || new_col < 0 || new_col >= total_col)
                    {
                        continue;
                    }
                    if (map[i][j] == map[new_row][new_col])
                    {
                        add(i * total_col + j, new_row * total_col + new_col, 0);
                    }
                    else
                    {
                        add(i * total_col + j, new_row * total_col + new_col, 1);
                    }
                }
            }
        }
        cin >> start_row >> start_col;
        cin >> end_row >> end_col;
        memset(dis, 0x3f3f3f3f, sizeof(dis));
        dis[start_row * total_col + start_col] = 0;
        spfa();
        cout << dis[end_row * total_col + end_col] << endl;
        memset(map, '\0', sizeof(map));
        memset(state, 0, sizeof(state));
        memset(head, -1, sizeof(head));
        memset(area, 0, sizeof(area));
        memset(next_area, 0, sizeof(next_area));
        memset(scale, 0, sizeof(scale));
        idx = 0;
        memset(dis, 0x3f3f3f3f, sizeof(dis));
    }
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...