用户输出
4
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#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); }
10 10
#.########
####.###@#
##########
######.###
@.#.#p#@##
###..##.##
##.#####.#
#.####@#.#
###@##
<15 bytes omitted>
用户输出
-1
系统信息
Exited with return code 0
10 10
.##.@.#.#.
###..##@#.
...#@#.###
.#.#..#...
#..#.....#
....##..##
..##..@.#p
##....##..
..#...
<15 bytes omitted>
用户输出
-1
系统信息
Exited with return code 0
10 10
.#@......#
...#......
..#.....##
..#..#....
...##..@.@
...@#.#...
#.#..#..#.
.p......##
.#..#.
<15 bytes omitted>
用户输出
30
系统信息
Exited with return code 0
20 20
..##.......@........
..#...#....#..##....
...#.#...#..#.#..#..
..#...#..#@###..##..
..#.#....#
<325 bytes omitted>
用户输出
61
系统信息
Exited with return code 0
30 30
##..##.###........#...#...@##.
#.#..#...##.....#.#.#..##p.#.@
...#.##......#...#.#.....#..#@
#
<835 bytes omitted>
用户输出
12
系统信息
Exited with return code 0
30 30
.....#..#...#.@..#..##.#..#...
.#..##.....##..........#.....#
##...##...#.#.......p...#...#.
#
<835 bytes omitted>
用户输出
83
系统信息
Exited with return code 0
30 30
##.....#.......#.....##...#@..
.......#.##.##..#.#.####.##...
.##......#..#.#.......#.....#.
.
<835 bytes omitted>
用户输出
130
系统信息
Exited with return code 0
30 30
...#..###.....##.#....#...##..
#.#...#..#.#..#..#.######..##.
.##..#.@.#......##...#....#.#.
.
<835 bytes omitted>
用户输出
154
系统信息
Exited with return code 0
30 30
..#...#...#...#........#....##
#.##....##.@#.#.#.#.#..@......
#.....#..@....#..#..##.#..#..#
.
<835 bytes omitted>
用户输出
114
系统信息
Exited with return code 0
30 30
#.######.##.####.#######.#####
##.@####.######.#########.##.#
...######.########.########.##
#
<835 bytes omitted>
用户输出
-1
系统信息
Exited with return code 0