标签:poj sum code open using with win bsp bfs
https://vjudge.net/contest/367733#problem/B
一样的代码,自己的错了,难受
dalaode
#include <iostream> #include<queue> #include<algorithm> #include<string.h> #include<stdio.h> using namespace std; #define maxn 31 char a[maxn][maxn][maxn]; int d[maxn][maxn][maxn]; int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//2向右3向左4向上 int sum=0; int L,R,C; struct node{ int x,y,z; }; node start,ed; int bfs() { queue<node> Q; Q.push(start); node cur; node next; while(Q.size()) { cur=Q.front(); Q.pop(); for(int i=0;i<6;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.z=cur.z+dir[i][2]; if(next.x>=0&&next.x<R&&next.y>=0&&next.y<C&&next.z>=0&&next.z<L&&a[next.z][next.x][next.y]!=‘#‘&&d[next.z][next.x][next.y]==0) { //a[cur.z][cur.x][cur.y]=‘#‘; 这样操作的话在栈中的点,也有可能到达当前的next点, //若用d[next.z][next.x][next.y]==0判断,只要到达next,就不会再判断 d[next.z][next.x][next.y]=d[cur.z][cur.x][cur.y]+1; if(next.x==ed.x&&next.y==ed.y&&next.z==ed.z) return d[ed.z][ed.x][ed.y]; Q.push(next); } } } return -1; } int main() { while(cin>>L>>R>>C&&(L+R+C)!=0) { sum=0; memset(d,0,sizeof(d)); for(int i=0;i<L;i++) for(int j=0;j<R;j++) for(int k=0;k<C;k++) { cin>>a[i][j][k]; if(a[i][j][k]==‘S‘) { start.z=i;start.x=j;start.y=k; } else if(a[i][j][k]==‘E‘) { ed.z=i;ed.x=j;ed.y=k; } } // cout<<start.x<< " "<<start.y<<" "<<start.z<<endl; // cout<<ed.x<<" "<<ed.y<<" "<<ed.z<<endl; if( bfs()==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",d[ed.z][ed.x][ed.y]); } return 0; }
我写的
#include <iostream> #include <cstring> #include <queue> using namespace std; int l, r, c; char ch[35][35][35]; int step[35][35][35]; int dp[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}}; struct node { int x, y, z; }; node start, ed; queue<node> que; node nxt; bool check(int z,int x,int y){ return z >= 0 && z < l && x >= 0 && x < r && y >= 0 && y < c && !step[z][x][y] && ch[z][x][y] != ‘#‘; } int bfs() { que.push(start); while (!que.empty()) { node tp = que.front(); que.pop(); for (int i = 0; i < 6; i++) { nxt.x = tp.x + dp[i][0]; nxt.y = tp.y + dp[i][1]; nxt.z = tp.z + dp[i][2]; if (check(nxt.z,nxt.x,nxt.y)) { step[nxt.z][nxt.x][nxt.y] = step[tp.z][tp.x][tp.y] + 1; if (nxt.x == ed.x && nxt.y == ed.y && nxt.z == ed.z) return step[nxt.z][nxt.x][nxt.y]; que.push(nxt); } } } return -1; } int main() { //freopen("in","r",stdin); ios::sync_with_stdio(0); while (cin >> l >> r >> c && l && r && c) { for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { for (int k = 0; k < c; k++) { cin >> ch[i][j][k]; if (ch[i][j][k] == ‘S‘) { start.z = i; start.x = j; start.y = k; } else if (ch[i][j][k] == ‘E‘) { ed.z = i; ed.x = j; ed.y = k; } } } } memset(step, 0, sizeof(step)); int g = bfs(); if (g == -1) cout << "Trapped!" << endl; else cout << "Escaped in " << g << " minute(s)." << endl; } return 0; }
标签:poj sum code open using with win bsp bfs
原文地址:https://www.cnblogs.com/xcfxcf/p/12684833.html