日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 3038 Children of the Candy Corn bfs dfs

發布時間:2025/5/22 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 3038 Children of the Candy Corn bfs dfs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://poj.org/problem?id=3083 入口S,出口E,分別求由入口 到出口靠左走,靠右走,和最短路三種走法各自的步數。入口和出口在邊界處,并且不會在四個角上,入口和出口至少隔著一個阻礙。 本來是想找一個廣搜和深搜結合的題目,百度一下找到的這題,后來發現這題所謂的廣搜加深搜只是用深搜找一條路徑,而用廣搜找另外一條路徑,。根本不是我希望的廣搜和深搜的結合。 不過既然題目已經看了,還是做一做吧。求最短路的沒什么好說的,用一下廣搜就可以解決問題了。主要是靠左走和靠右走的部分,這里用到了兩個數組,dirx[],diry[],共同來確定方向。dirx[],diry[],數組的下標增1對4求余代表順時針方向,來處理向左走。同樣下標增3對4求余代表逆時針方向,來處理向右走。 #include <stdio.h> #include <string.h> #include<stdlib.h> #include <iostream> #include<queue> using namespace std; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int dirx[4]={0,-1,0,1}; int diry[4]={-1,0,1,0};//dirx,diry數組下標:0左,1上,2右,3下; int co,ro,ansr,ansl,ansm; char map[45][45]; struct coor { int x,y,z; int time; }; bool visited[45][45]; void dfsl(int,int,int,int); void dfsr(int,int,int,int); void dfsl(int a,int b,int time,int c) { int i,x,y; if(map[a][b]=='E') { ansl=time; return; } c=(c+3)%4; ?//靠左走;這樣c總是原來方向的左面一個方向, for(i=0;i<4;i++) { x=a+dirx[c]; y=b+diry[c]; if(!(x<0||y<0||x>=co||y>=ro||map[x][y]=='#')) { dfsl(x,y,time+1,c); break; } c=(c+1)%4; //順時針方向 } } void dfsr(int a,int b,int time,int c) { int i,x,y; if(map[a][b]=='E') { ansr=time; return; } c=(c+1)%4;// 靠右走;這樣c總是原來方向的右面一個方向。 for(i=0;i<4;i++) { x=a+dirx[c]; y=b+diry[c]; if(!(x<0||y<0||x>=co||y>=ro||map[x][y]=='#')) { dfsr(x,y,time+1,c); break; } c=(c+3)%4; //逆時針方向 } } bool check(int x,int y) { if(x<0 || y<0 || x>=co || y>=ro) return false; if(map[x][y]=='#' || visited[x][y]) return false; return true; } void bfs(int a,int b) { int i,j,k,l,front=-1,rear=-1; coor out,in; queue <coor> que; in.x=a; in.y=b; in.time=1; que.push(in); visited[a][b]=true; while(!que.empty()) { out=que.front(); que.pop(); if(map[out.x][out.y]=='E') { ansm=out.time; return; } for(l=0;l<4;l++) { i=out.x+dir[l][0]; j=out.y+dir[l][1]; if(!check(i,j)) continue; visited[i][j]=true; in.x=i; in.y=j; in.time=out.time+1; que.push(in); } } } int main() { int cas,i,j,a,b,c; scanf("%d",&cas); while(cas--) { scanf("%d%d",&ro,&co); //getchar(); for(i=0;i<co;i++) scanf("%s",map[i]); for(i=0;i<co;i++) for(j=0;j<ro;j++) if(map[i][j]=='S') { a=i; b=j; } map[a][b]='#'; if(b==ro-1) c=0; else if(a==co-1) c=1; else if(b==0) c=2; else if(a==0) c=3; dfsl(a,b,1,c); dfsr(a,b,1,c); memset(visited,false,sizeof(visited)); bfs(a,b); printf("%d %d %d\n",ansl,ansr,ansm); } system("pause"); return 0; }

轉載于:https://www.cnblogs.com/zxj015/archive/2011/04/01/2740246.html

總結

以上是生活随笔為你收集整理的poj 3038 Children of the Candy Corn bfs dfs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。