编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#20361 #1029. 霍霍想回家 Accepted 100 21 ms 412 K C++ / 2.2 K 192022214033 2024-12-13 12:44:55
显示原始代码
#include <iostream>
#include <queue>
#include <utility>  // 包含pair的定义

using namespace std;
int n = 0, m = 0;
//这里有两个比较,一个是同一个点内部bfs试探出最大路径,另一个是不同点之间的最大路径比较,
int a[25][25] = { 0 }, b[25][25] = { 0 };  //因此a,b分别表示该结果
char map[25][25], mapx[25][25];            // map是模版,mapx会根据路径变化增加墙
int movex[5] = { 0, 0, 0, 1, -1 };
int movey[5] = { 0, 1, -1, 0, 0 };

void input();
void bfs(int x, int y);
int main() {
    cin >> n >> m;  // n行m列
    input();

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            bfs(i, j);
        }
    }

    int res = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            res = max(res, b[i][j]);
        }
    }
    cout << res;

    return 0;
}
void input() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> map[i][j];
            mapx[i][j] = map[i][j];
        }
    }
    return;
}

void bfs(int x, int y) {
    if (map[x][y] == '#')
        return;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            mapx[i][j] = map[i][j];  //重置mapx数组
            a[i][j] = 0;             //重置记录路径的数组
        }
    }

    queue<pair<int, int> > p;  // pair定义点集类型{x,y}
    p.push({ x, y });          //录入开局坐标
    mapx[x][y] = '#';          //入队时自己变成墙避免回路

    do {                                        //这是针对一个点的循环
        pair<int, int> temp = p.front();        //读出点数据
        p.pop();                                //出队,bfs算法要求
        int x1 = temp.first, y1 = temp.second;  //得到x1,y1数据

        for (int i = 1; i <= 4; i++) {
            int nx = x1 + movex[i], ny = y1 + movey[i];
            if (nx <= n && nx >= 1 && ny <= m && ny >= 1 &&
                mapx[nx][ny] == '.')  //如果没有越界,而且走下去的点不是墙
            {
                a[nx][ny] = a[x1][y1] + 1;  //别的路径挪上来的,因而加1
                mapx[nx][ny] = '#';         //入队时自己变成墙避免回路
                p.push({ nx, ny });         //最后入队
            }
        }
    } while (!p.empty());  //队空了就表示跑完数据了

    /*for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                    cout<<a[i][j]<<' ';
            }
            cout<<endl;
    }
    cout<<endl; */  //可以解开这段代码看dfs结果,壮美!

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ans = max(ans, a[i][j]);
        }
    }
    b[x][y] = ans;  //该点的最大路径

    return;
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:4 ms
内存:332 KiB

输入文件(1.in

10 10
......##..
...#....##
##.#..#...
.#.##....#
.#.##.#...
...##..#..
...##.....
.#.#..###.
#.#.#.
<15 bytes omitted>

答案文件(1.out

25

用户输出

25

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:6 ms
内存:412 KiB

输入文件(2.in

20 20
......##.....#....##
##.#..#....#.##....#
.#.##.#......##..#..
...##......#.#..###.
#.#.#.....
<325 bytes omitted>

答案文件(2.out

44

用户输出

44

系统信息

Exited with return code 0
测试点 #3
Accepted
得分:100
用时:4 ms
内存:412 KiB

输入文件(3.in

17 17
......##.....#...
.####.#..#....#.#
#....#.#.##.#....
..##..#.....##...
...#.#..###.#.#.#
....
<211 bytes omitted>

答案文件(3.out

31

用户输出

31

系统信息

Exited with return code 0
测试点 #4
Accepted
得分:100
用时:4 ms
内存:372 KiB

输入文件(4.in

3 5
...#.
.#.#.
.#...

答案文件(4.out

10

用户输出

10

系统信息

Exited with return code 0
测试点 #5
Accepted
得分:100
用时:3 ms
内存:324 KiB

输入文件(5.in

3 3
...
...
...

答案文件(5.out

4

用户输出

4

系统信息

Exited with return code 0