编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#225 #1038. 1-04E. zzj & liaoy 想要去摄影 Accepted 100 125 ms 1752 K C++ / 2.1 K JM233333 2019-06-20 21:43:36
显示原始代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
typedef long long ll;

class Point {
public:
    int x, y;
    int step;
    int status;
    Point(int i, int j, int t, int s) : x(i), y(j), step(t), status(s) {}
    Point() {}
    friend bool operator==(const Point &A, const Point &B) { return (A.x == B.x && A.y == B.y); }
};

int bfs(Point st, int k);
bool is_passible(int x, int y);

const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
const int MAX = 35, MAXST = (1 << 10) + 5;
char graph[MAX][MAX];
bool visited[MAX][MAX][MAXST];
int n, m;

int main() {
    // freopen("test.txt", "r", stdin);
    while (scanf("%d %d", &n, &m) != EOF) {
        // 初始化
        memset(visited, false, sizeof(visited));
        // 输入
        for (int i = 1; i <= n; i++) {
            scanf("%s", graph[i] + 1);
        }
        // 预处理
        int k = 0;
        Point st;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                if (graph[i][j] == 'p') {
                    st = Point(i, j, 0, 0);
                }
                if (graph[i][j] == '@') {
                    graph[i][j] = k + '0';
                    k++;
                }
            }
        // BFS
        int res = bfs(st, k);
        // 输出
        printf("%d\n", res);
    }
    return 0;
}
// 广度优先搜索
int bfs(Point st, int k) {
    // 主循环
    queue<Point> q;
    q.push(st);
    while (!q.empty()) {
        // 出队
        Point p = q.front();
        q.pop();
        // 遍历
        for (int i = 0; i < 4; i++) {
            // 获取目标坐标
            int nx = p.x + dx[i], ny = p.y + dy[i];
            int nstep = p.step + 1;
            int nstatus = p.status;
            // 不可通行的情况
            if (!is_passible(nx, ny)) {
                continue;
            }
            // 更新状态
            if (graph[nx][ny] >= '0' && graph[nx][ny] <= '9') {
                nstatus |= (1 << (graph[nx][ny] - '0'));
            }
            // 完成的情况
            if (nstatus == (1 << k) - 1) {
                return nstep;
            }
            // 入队
            if (!visited[nx][ny][nstatus]) {
                visited[nx][ny][nstatus] = true;
                q.push(Point(nx, ny, nstep, nstatus));
            }
        }
    }
    // 无解的情况
    return -1;
}
// 判断是否可以通行
inline bool is_passible(int x, int y) { return (graph[x][y] != '#' && x >= 1 && x <= n && y >= 1 && y <= m); }
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:5 ms
内存:1524 KiB

输入文件(1.in

3 3
p@#
#..
..@

答案文件(1.out

4

用户输出

4

系统信息

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

输入文件(2.in

3 3
p##
@#@
###

答案文件(2.out

-1

用户输出

-1

系统信息

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

输入文件(3.in

5 5
...@.
#####
###.@
#@p#@
..#.#

答案文件(3.out

-1

用户输出

-1

系统信息

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

输入文件(4.in

5 5
.@.@#
#.p..
...##
.@..@
##@#.

答案文件(4.out

12

用户输出

12

系统信息

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

输入文件(5.in

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

答案文件(5.out

-1

用户输出

-1

系统信息

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

输入文件(6.in

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

答案文件(6.out

-1

用户输出

-1

系统信息

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

输入文件(7.in

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

答案文件(7.out

30

用户输出

30

系统信息

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

输入文件(8.in

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

答案文件(8.out

61

用户输出

61

系统信息

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

输入文件(9.in

30 30
##..##.###........#...#...@##.
#.#..#...##.....#.#.#..##p.#.@
...#.##......#...#.#.....#..#@
#
<835 bytes omitted>

答案文件(9.out

12

用户输出

12

系统信息

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

输入文件(10.in

30 30
.....#..#...#.@..#..##.#..#...
.#..##.....##..........#.....#
##...##...#.#.......p...#...#.
#
<835 bytes omitted>

答案文件(10.out

83

用户输出

83

系统信息

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

输入文件(11.in

30 30
##.....#.......#.....##...#@..
.......#.##.##..#.#.####.##...
.##......#..#.#.......#.....#.
.
<835 bytes omitted>

答案文件(11.out

130

用户输出

130

系统信息

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

输入文件(12.in

30 30
...#..###.....##.#....#...##..
#.#...#..#.#..#..#.######..##.
.##..#.@.#......##...#....#.#.
.
<835 bytes omitted>

答案文件(12.out

154

用户输出

154

系统信息

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

输入文件(13.in

30 30
..#...#...#...#........#....##
#.##....##.@#.#.#.#.#..@......
#.....#..@....#..#..##.#..#..#
.
<835 bytes omitted>

答案文件(13.out

114

用户输出

114

系统信息

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

输入文件(14.in

30 30
#.######.##.####.#######.#####
##.@####.######.#########.##.#
...######.########.########.##
#
<835 bytes omitted>

答案文件(14.out

-1

用户输出

-1

系统信息

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

输入文件(15.in

30 30
########.###.###.###.###.##.##
######.####..#.###.###########
########.####.#########.######
.
<835 bytes omitted>

答案文件(15.out

11

用户输出

11

系统信息

Exited with return code 0