标签:
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //#define LOCAL int n,m; struct Node { int x,y,z; int step; }sp,ep,cp; char mp[12][12][12]; int dir[6][3]={{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{0,0,1},{0,0,-1}}; int bfs() { queue<Node> q; while(!q.empty())q.pop(); q.push(sp); while(!q.empty()) { sp=q.front(),q.pop(); if(sp.x==ep.x&&sp.y==ep.y&&sp.z==ep.z)return sp.step; for(int i=0;i<6;i++) { cp.x=sp.x+dir[i][0]; cp.y=sp.y+dir[i][1]; cp.z=sp.z+dir[i][2]; cp.step=sp.step+1; if(mp[cp.x][cp.y][cp.z]==‘X‘)continue; if(cp.x<0||cp.y<0||cp.z<0||cp.x>=n||cp.y>=n||cp.z>=n)continue; mp[cp.x][cp.y][cp.z]=‘X‘; q.push(cp); } } return -1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL ios::sync_with_stdio(false); char t[10]; while(cin>>t>>n) { m=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>mp[i][j]; cin>>sp.z>>sp.y>>sp.x;//x-col,y-raw,z-dim cin>>ep.z>>ep.y>>ep.x; sp.step=0,ep.step=0; mp[sp.x][sp.y][sp.z]=‘X‘; int ans=bfs(); if(ans==-1)printf("NO ROUTE\n"); else printf("%d %d\n",n,ans); while(cin>>t)if(t[0]==‘E‘)break; } return 0; }
标签:
原文地址:http://www.cnblogs.com/gpsx/p/5184751.html