hdu 5094 Maze
生活随笔
收集整理的這篇文章主要介紹了
hdu 5094 Maze
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
n*m大的迷宮 ,有p種鑰匙。鑰匙最多有10種。
然后一個k,然后k行表示 (x1,y1),(x2,y2)直接有門或者墻。 如果g==0 ,就是有墻, 如果g>0 表示有門,且門需要第g把鑰匙才能開。
然后下來一個s,然后s行,表示(x,y)這個點有 第g把鑰匙。
問從(1,1)到(n,m)最少幾步。
輸入:
4 4 9 9 1 2 1 3 2 1 2 2 2 0 2 1 2 2 0 2 1 3 1 0 2 3 3 3 0 2 4 3 4 1 3 2 3 3 0 3 3 4 3 0 4 3 4 4 0 2 2 1 2 4 2 1輸出:
14題解:
如果沒有鑰匙?
現在有了鑰匙,我們就記錄每一步所擁有鑰匙的狀態
代碼:
//#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include<string.h> #include<vector> #include<queue> #include<algorithm> #include<stdio.h> #include<math.h> #include<map> #include<stdlib.h> #include<time.h> #include<stack> #include<set> #include<deque> using namespace std; typedef long long ll; struct data {int x,y,step,zt;data (int i,int j,int s,int z){x=i,y=j,step=s,zt=z;} }; int tu[55][55][55][55],ys[55][55]; bool vis[55][55][1<<11]; int fx[4][2]= {{-1,0},{0,-1},{1,0},{0,1}}; int n,m,p; int bfs() {memset(vis,0,sizeof(vis));data tem(1,1,0,0);queue<data>q;q.push(tem);while(!q.empty()){data tt=q.front();//cout<<tt.x<<" "<<tt.y<<" "<<tt.step<<" ";//int hhhhh=tt.zt;//while(hhhhh)cout<<hhhhh%2,hhhhh/=2;cout<<endl;q.pop();if(tt.x==n&&tt.y==m)return tt.step;for(int i=0; i<4; i++){int x=tt.x+fx[i][0],y=tt.y+fx[i][1];if(x>=1&&x<=n&&y>=1&&y<=m&&tu[tt.x][tt.y][x][y]!=0){if(tu[tt.x][tt.y][x][y]==-1){if(!ys[x][y]&&!vis[x][y][tt.zt]){vis[x][y][tt.zt]=1;if(x==n&&y==m)return tt.step+1;data ttt(x,y,tt.step+1,tt.zt);q.push(ttt);}else if(ys[x][y]&&!vis[x][y][tt.zt|ys[x][y]]){vis[x][y][tt.zt|ys[x][y]]=1;if(x==n&&y==m)return tt.step+1;data ttt(x,y,tt.step+1,tt.zt|ys[x][y]);q.push(ttt);}}else if(tu[tt.x][tt.y][x][y]!=-1){int xi=(1<<(tu[tt.x][tt.y][x][y]-1));if(xi&tt.zt){if(!ys[x][y]&&!vis[x][y][tt.zt]){if(x==n&&y==m)return tt.step+1;vis[x][y][tt.zt]=1;data ttt(x,y,tt.step+1,tt.zt);q.push(ttt);}else if(ys[x][y]&&!vis[x][y][tt.zt|ys[x][y]]){if(x==n&&y==m)return tt.step+1;vis[x][y][tt.zt|ys[x][y]]=1;data ttt(x,y,tt.step+1,tt.zt|ys[x][y]);q.push(ttt);}}}}}}return -1; }int main() {while(~scanf("%d%d%d",&n,&m,&p)){memset(tu,-1,sizeof(tu));memset(ys,0,sizeof(ys));int k;scanf("%d",&k);while(k--){int x1,x2,y1,y2,type;scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&type);tu[x1][y1][x2][y2]=tu[x2][y2][x1][y1]=type;}int s;scanf("%d",&s);while(s--){int xx,yy,type;scanf("%d%d%d",&xx,&yy,&type);ys[xx][yy]|=(1<<(type-1));}printf("%d\n",bfs());}return 0; }總結
以上是生活随笔為你收集整理的hdu 5094 Maze的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 铺地蜈蚣的功效与作用、禁忌和食用方法
- 下一篇: 蒲桃叶的功效与作用、禁忌和食用方法