标签:
1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 struct pr{ 6 int x,y,t,key;//横纵轴,时间,钥匙 7 }; 8 const int x[10]={0,0,0,1,-1},y[10]={0,1,-1,0,0}; 9 int a,b,t,sx,sy; 10 char map[25][25];//输入图 11 int flag[25][25][(1<<11)]; 12 queue<pr> s; 13 pr q,temp; 14 15 bool check(int x,int y){ 16 return x>0&&x<=a&&y>0&&y<=b&&map[x][y]!=‘*‘; 17 } 18 19 bool door(int x,int y,int key){ 20 if(map[x][y]>=‘A‘&&map[x][y]<=‘Z‘) 21 return (1<<(map[x][y]-‘A‘+1))&key; 22 return 1; 23 } 24 25 void bfs(){ 26 int i,j,k; 27 while(!s.empty()) 28 s.pop(); 29 memset(flag,0,sizeof(flag)); 30 flag[sx][sy][0]=1; 31 q.x=sx; q.y=sy; q.t=0; q.key=0; 32 s.push(q); 33 while(!s.empty()) 34 { 35 q=s.front(); 36 s.pop(); 37 if(q.t>=t){//被抓 38 cout<<-1<<endl; return ; 39 } 40 if(map[q.x][q.y]==‘^‘){//找到出口 41 cout<<q.t<<endl; 42 return; 43 } 44 for(i=1;i<=4;i++){ 45 int nx=q.x+x[i]; 46 int ny=q.y+y[i]; 47 int newkey=q.key; 48 if(check(nx,ny)&&door(nx,ny,newkey)){ 49 if(!flag[nx][ny][newkey]){ 50 flag[nx][ny][newkey]=1; 51 if(map[nx][ny]>=‘a‘&&map[nx][ny]<=‘z‘)//更新钥匙 52 newkey=(1<<(map[nx][ny]-‘A‘+1)|q.key); 53 flag[nx][ny][newkey]=1; 54 temp.x=q.x+x[i]; 55 temp.y=q.y+y[i]; 56 temp.t=q.t+1; 57 temp.key=newkey; 58 s.push(temp); 59 } 60 } 61 } 62 } 63 cout<<-1<<endl; 64 } 65 int main() 66 { 67 int i,j; 68 for(;cin>>a>>b>>t;) 69 { 70 for(i=1;i<=a;i++) 71 for(j=1;j<=b;j++){ 72 cin>>map[i][j]; 73 if(map[i][j]==‘@‘) 74 { 75 sx=i; 76 sy=j; 77 } 78 } 79 bfs(); 80 } return 0; 81 }
标签:
原文地址:http://www.cnblogs.com/nicetomeetu/p/5156471.html