UVA 10047 - The Monocycle BFS
生活随笔
收集整理的這篇文章主要介紹了
UVA 10047 - The Monocycle BFS
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=988
題目大意:
獨(dú)輪車的車輪被分為5個(gè)扇形,分別涂上一種不同的顏色,現(xiàn)在有一個(gè)人行駛在M*N的玩?zhèn)€平面上。每個(gè)格子的大小剛好為一個(gè)扇形。有些格子有障礙,騎車的人從S出發(fā)要到達(dá)T,途中,在任何一個(gè)格子的時(shí)候他要么騎到下一個(gè)格子,要么左轉(zhuǎn)或者右轉(zhuǎn)90度,初始他面朝北,并且綠色格子貼著地面,要求到終點(diǎn)時(shí)候也是綠色格子貼著地面。
思路:
半年前覺得挺難的題目現(xiàn)在看起來(lái)好簡(jiǎn)單。哈哈哈哈哈哈哈哈哈~
BFS。。
狀態(tài)需要記錄方向和此時(shí)的顏色。
代碼略丑。主要是BFS過(guò)程中我是直接枚舉的。
#include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN=30; char map[MAXN][MAXN]; bool vis[MAXN][MAXN][5][4];//x,y,color,dir int m,n; struct state {int x,y;int step;int dir;//direction :north:0 south:1 east:2 west:3int color;//開始為0,終點(diǎn)也要為0bool operator <(const state &a)const {return step > a.step;}state(){;}state(int x,int y,int step,int dir,int color) :x(x) ,y(y),step(step), dir(dir), color(color){} }start,fin; int bfs() {memset(vis,0,sizeof(vis));priority_queue<state> q;q.push(start);vis[start.x][start.y][start.color][start.dir]=true;while(!q.empty()){state cur=q.top();q.pop();if(cur.x==fin.x && cur.y==fin.y && cur.color==0)//找到答案了!return cur.step;int x=cur.x,y=cur.y,step=cur.step,color=cur.color,dir=cur.dir;switch(cur.dir){case 0: //北if(!vis[x-1][y][(color+1) % 5][dir] && map[x-1][y]!='#'){vis[x-1][y][(color+1) % 5][dir]=1;q.push(state(x-1,y,step+1,dir,(color+1) % 5));}if(!vis[x][y][color][2]){ vis[x][y][color][2]=1; q.push(state(x,y,step+1,2,color));}if(!vis[x][y][color][3]) { vis[x][y][color][3]=1; q.push(state(x,y,step+1,3,color));} break;case 1: //南if(!vis[x+1][y][(color+1) % 5][dir] && map[x+1][y]!='#'){vis[x+1][y][(color+1) % 5][dir]=1;q.push(state(x+1,y,step+1,dir,(color+1) % 5));}if(!vis[x][y][color][2]){ vis[x][y][color][2]=1; q.push(state(x,y,step+1,2,color));}if(!vis[x][y][color][3]) { vis[x][y][color][3]=1; q.push(state(x,y,step+1,3,color));} break;case 2: //東if(!vis[x][y+1][(color+1) % 5][dir] && map[x][y+1]!='#'){vis[x][y+1][(color+1) % 5][dir] =1;q.push(state(x,y+1,step+1,dir,(color+1) % 5));}if(!vis[x][y][color][0]){ vis[x][y][color][0]=1; q.push(state(x,y,step+1,0,color));}if(!vis[x][y][color][1]){ vis[x][y][color][1]=1; q.push(state(x,y,step+1,1,color)); }break;case 3: //西if(!vis[x][y-1][(color+1) % 5][dir] && map[x][y-1]!='#'){vis[x][y-1][(color+1) % 5][dir]=1;q.push(state(x,y-1,step+1,dir,(color+1) % 5));}if(!vis[x][y][color][0]){ vis[x][y][color][0]=1; q.push(state(x,y,step+1,0,color));}if(!vis[x][y][color][1]){vis[x][y][color][1]=1; q.push(state(x,y,step+1,1,color)); }break;}}return -1; } int main() {int kase=1;while(~scanf("%d%d",&m,&n),m||n){for(int i=1;i<=m;i++)scanf("%s",map[i]+1);for(int i=0;i<=n+1;i++) //最外面加上一層圍墻,等下過(guò)程就可以減少判斷。map[0][i]=map[m+1][i]='#';for(int i=0;i<=m+1;i++)map[i][0]=map[i][n+1]='#';/*for(int i=0;i<=m+1;i++){for(int j=0;j<=n+1;j++)printf("%c",map[i][j]);puts("");}*/for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(map[i][j]=='S'){state temp(i,j,0,0,0);start=temp;map[i][j]='.';}else if(map[i][j]=='T'){state temp(i,j,0,0,0);fin=temp;map[i][j]='.';}}}int ans=bfs();if(kase!=1)printf("\n");printf("Case #%d\n",kase++);if(ans==-1)printf("destination not reachable\n");elseprintf("minimum time = %d sec\n",ans);}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/murmured/p/5004111.html
總結(jié)
以上是生活随笔為你收集整理的UVA 10047 - The Monocycle BFS的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我对 ASP.NET Web API 的
- 下一篇: IOS的一些文件操作。(沙箱) 在Doc