|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
链接:https://www.luogu.com.cn/problem/T308113?contestId=98472
代码:
- #include <bits/stdc++.h>
- using namespace std;
- int ax, ay, az;
- int n, m;
- int step[11][1113][1113];
- bool vis[11][1113][1113];
- int q[100000100][3], front, rear;
- int dir[2][2] = {{0, 1}, {1, 0}};
- char s[11][1113][1113];
- inline bool check(int c, int x, int y) {
- return (c >= 1 && c <= n && x >= 1 && x <= m && y >= 1 && y <= m);
- }
- void bfs() {
- front = 1;
- rear = 1;
- q[1][0] = q[1][1] = q[1][2] = 1;
- step[1][1][1] = 0;
- vis[1][1][1] = 1;
- while (rear + 1 != front) {
- int fx = q[front][0], fy = q[front][1], fz = q[front][2];
- front++;
- if (s[fx][fy][fz] == '@') {
- printf("%d", step[fx][fy][fz]);
- return;
- }
- if (s[fx][fy][fz] >= '1' && s[fx][fy][fz] <= '9') {
- int num = s[fx][fy][fz] - '0';
- if (vis[num][1][1] == 0 && s[num][1][1] != '*') {
- step[num][1][1] = step[fx][fy][fz];
- vis[num][1][1] = 1;
- ++rear;
- q[rear][0] = num;
- q[rear][1] = 1;
- q[rear][2] = 1;
- }
- continue;
- }
- for (int i = 0; i < 2; ++i) {
- int ny = fy + dir[i][0], nz = fz + dir[i][1];
- if (check(fx, ny, nz) && vis[fx][ny][nz] == 0 && s[fx][ny][nz] != '*') {
- step[fx][ny][nz] = step[fx][fy][fz] + 1;
- vis[fx][ny][nz] = 1;
- ++rear;
- q[rear][0] = fx;
- q[rear][1] = ny;
- q[rear][2] = nz;
- }
- }
- }
-
- puts("-1");
- }
- int main() {
- scanf("%d%d", &n, &m);
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- scanf("%s", s[i][j] + 1);
- }
- }
- bfs();
- return 0;
- }
复制代码
总体思路就是 bfs,但是提交一直是错的,大样例怎么调都调不出来,求助
|
|