标签:des style blog http color os strong io
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9528 | Accepted: 4126 |
Description
Input
Output
Sample Input
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
题解:题目大意,对于迷宫来说,如果我们的方向一直向左走或者向右走,我们一定能够找到出口,本题的意思就是说,给你一个迷宫,起点是 ‘S’ ,终点是 ‘E’,
接下来一行每行输入三个数,第一个数表示方向一直向左走,走出迷宫所需要的步数,第二个表示,方向一字向右走,走出迷宫所需要的步数,第三个就是最小的步数;
对于前两个来说,我们可以用两个 DFS 来求出,走出迷宫所需要的步数,第三种情况就需要用到,BFS 了,直接求出来最小的步数即可;
以下图解:
初始化一开始的朝向都是1,当向左转时,她的第一个左边是4,第二个是1,第三个是2、、、、顺时针的
当向右时是逆时针的、、、、
以下是AC代码:
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 const int N = 50; 8 char Map[N][N]; 9 int vis[N][N]; 10 int m,n,sx,sy,ex,ey,flag; 11 int ans1,ans2; 12 int dir[4][2] = {-1,0,1,0,0,1,0,-1}; 13 struct T 14 { 15 int x,y,step; 16 }now,eed; 17 bool judge(int xx,int yy) 18 { 19 if(xx>=1 && xx<=m && yy>=1 && yy<=n && vis[xx][yy]==0 && Map[xx][yy]!=‘#‘) 20 return true; 21 return false; 22 } 23 //face表示的是当前的朝向,step表示的是当前的步数 24 void dfs1(int x,int y,int step ,int face)//向左走,进行的是顺序的,递归 25 { 26 if(flag == 1) 27 return ; 28 if(x==ex && y==ey) 29 { 30 flag=1; 31 ans1 = step; 32 return ; 33 } 34 //根据上一个的朝向判断下一个应该向那转弯 35 if(face == 1) 36 { 37 if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 38 if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 39 if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 40 if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 41 } 42 else if(face == 2) 43 { 44 if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 45 if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 46 if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 47 if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 48 } 49 else if(face == 3) 50 { 51 if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 52 if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 53 if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 54 if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 55 } 56 else 57 { 58 if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 59 if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 60 if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 61 if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 62 } 63 } 64 void dfs2(int x,int y,int step,int face)//向右走,进行的是逆序的,递归 65 { 66 if(flag == 1) 67 return ; 68 if(x==ex && y==ey) 69 { 70 flag=1; 71 ans2 = step; 72 return ; 73 } 74 if(face == 1) 75 { 76 if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 77 if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 78 if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 79 if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 80 } 81 else if(face == 2) 82 { 83 if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 84 if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 85 if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 86 if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 87 } 88 else if(face == 3) 89 { 90 if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 91 if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 92 if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 93 if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 94 } 95 else 96 { 97 if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 98 if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 99 if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 100 if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 101 } 102 } 103 int bfs() 104 { 105 queue< T > ma; 106 while(!ma.empty()) ma.pop(); 107 now.x=sx,now.y=sy,now.step=1; 108 ma.push(now); 109 vis[sx][sy]=1; 110 while(!ma.empty()) 111 { 112 now = ma.front(); 113 ma.pop(); 114 if(now.x == ex && now.y == ey) 115 return now.step; 116 for(int i=0; i<4; i++) 117 { 118 eed.x=now.x+dir[i][0]; 119 eed.y=now.y+dir[i][1]; 120 if(judge(eed.x,eed.y)) 121 { 122 eed.step=now.step+1; vis[eed.x][eed.y]=1; 123 ma.push(eed); 124 } 125 } 126 } 127 return 0; 128 } 129 int main() 130 { 131 int T; 132 cin>>T; 133 while(T--) 134 { 135 memset(vis,0,sizeof(vis)); 136 cin>>n>>m; 137 for(int i=1; i<=m; i++) 138 for(int j=1; j<=n; j++) 139 { 140 scanf(" %c",&Map[i][j]); 141 if(Map[i][j] == ‘S‘) 142 { 143 sx=i; sy=j; 144 } 145 if(Map[i][j] == ‘E‘) 146 { 147 ex=i; ey=j; 148 } 149 } 150 flag=0;ans1=0; 151 dfs1(sx,sy,1,1);//一直向左走 152 flag=0;ans2=0; 153 dfs2(sx,sy,1,1);//一直向右走 154 int ans3=bfs(); 155 printf("%d %d %d\n",ans1,ans2,ans3); 156 } 157 return 0; 158 }
poj 3083 Children of the Candy Corn,布布扣,bubuko.com
poj 3083 Children of the Candy Corn
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/lovychen/p/3879044.html