poj3083Children of the Candy Corn
来源:互联网 发布:手机摇骰子软件 编辑:程序博客网 时间:2024/05/17 08:03
http://poj.org/problem?id=3083
#include <stdio.h>#include <string.h>#define Max 50int w,h;char maze[Max][Max];int start_x,start_y,end_x,end_y;int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};//左上右下int visited[Max][Max];typedef struct node{ int x,y,dis;}node;node now,temp,queue[100000];void initial(){ int i,j; scanf("%d %d",&w,&h); for(i=0;i<h;i++) { getchar(); for(j=0;j<w;j++) { scanf("%c",&maze[i][j]); if(maze[i][j]=='S') { start_x=i; start_y=j; } if(maze[i][j]=='E') { end_x=i; end_y=j; } } }}int Depth_FirstSearch(int x1,int y1,int d,int x2,int y2){ int new_x,new_y,i,count=1; if(x1==x2&&y1==y2) return count; d=(d+3)%4; i=d; for(i=d;i<d+4;i++) { new_x=x1+dir[i%4][0]; new_y=x2+dir[i%4][1]; if(new_x>=0&&new_x<h&&new_y>=0&&new_y<w&&maze[new_x][new_y]!='#') { count++; d=i;//记录当前方向 Depth_FirstSearch(new_x,new_y,d,x2,y2); } }}int Breadth_FirstSearch(){ int front,rear,i; front=rear=0; now.x=start_x; now.y=start_y; now.dis=1; queue[rear++]=now; while(front<rear) { now=queue[front++];//取对头元素now for(i=0;i<4;i++) { temp.x=now.x+dir[i][0]; temp.y=now.y+dir[i][1]; temp.dis=now.dis+1; if(temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&maze[temp.x][temp.y]!='#'&&!visited[temp.x][temp.y]) { if(maze[temp.x][temp.y]=='E') return temp.dis; visited[temp.x][temp.y]=1; queue[rear++]=temp; } } }}int main(){ int n,count1,count2,count3; scanf("%d",&n); while(n--) { initial(); count1=Depth_FirstSearch(start_x,start_y,0,end_x,end_y); count2=Depth_FirstSearch(end_x,end_y,0,start_x,start_y); count3=Breadth_FirstSearch(); printf("%d %d %d\n",count1,count2,count3); } return 0;}
0 0
- poj3083Children of the Candy Corn
- poj3083Children of the Candy Corn
- poj3083Children of the Candy Corn
- poj3083Children of the Candy Corn(AC)
- poj3083Children of the Candy Corn(dfs)
- poj3083Children of the Candy Corn(bfs+dfs)
- (转)poj3083Children of the Candy Corn(BFS+DFS)
- POJ_3083_Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- poj3083_Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- vnc和putty
- 毕向东Java视频学习笔记【day01-day07】
- css3第二课时!css3新增属性选择器!
- 张氏标定法简述
- SQL加强
- poj3083Children of the Candy Corn
- linux中环境搭建部署
- 前端基础-黑马视频开始学习-1
- 浙江省流动人口居住登记条例
- 线程操作,比 handler更简单的AsyncTask 使用详解-基础java线程
- Service
- Vert.x module概念
- java io流
- OpenCV 学习日记(二)---牛刀小试:输入输出图像和视频