Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9157 | Accepted: 3969 |
Description
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
Sample Input
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
Source
题意就是 一个迷宫。从S到E。先依照 优先走转左的方案走,再依照 优先转右的方案走,最后 求最短路。
打印三种答案。
宽搜和深搜的初等题目。不难但就是坑,我让这4个方向,一天弄的晕头转向的,方向感不好的渣渣啊
也不会调递归的程序,坑
深搜在本题,不再是传统深度搜索了,而是左优先搜索、 右优先搜索,本题的亮点,可是本题的坑
左优先搜索 是 当前面向的方向 先走左手处。再按顺时针走向依次訪问
右优先搜索 是 当前面向的方向 先走右手处,再按逆时针走向依次訪问
以左搜为例:
如果面向(上 Up),那么首先訪问的点是( Left),->左手边点
面向 (右Right),那么首先訪问的点是(上Up)->左手边点
这个题没什么特别的写法,看了网上题解都差点儿相同。200来行
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define left 1 // 此处建议最好用英语单词来代表方向 #define up 2 //由于这个题的方向太繁琐了。用数字一会就蒙了 #define right 3 #define down 4 const int INF = 65535; const int Size = 1<<15; const int N = 45; using namespace std; int mp[N][N],vis[N][N]; int sx,sy,w,h,le,ri; int flag1,flag2,sum; struct node { int x,y,ans; }q[Size]; int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; char b[N][N]; void DFS1(int x,int y,int d,int to) { if(flag1 == 1) //刚開始没写这个!!!DFS停不了 return; if(b[x][y]=='E') { flag1 = 1; le = d; return ; } if(to == up) { if(mp[x][y-1]) // 左 DFS1(x,y-1,d+1,left); if(mp[x-1][y]) // 上 DFS1(x-1,y,d+1,up); if(mp[x][y+1]) // 右 DFS1(x,y+1,d+1,right); if(mp[x+1][y]) //下 DFS1(x+1,y,d+1,down); } else if(to == left) { if(mp[x+1][y]) //面向左 。可是左手边是 下,所以先走下 DFS1(x+1,y,d+1,down); if(mp[x][y-1]) DFS1(x,y-1,d+1,left); if(mp[x-1][y]) DFS1(x-1,y,d+1,up); if(mp[x][y+1]) DFS1(x,y+1,d+1,right); } else if(to == right) { if(mp[x-1][y]) DFS1(x-1,y,d+1,up); if(mp[x][y+1]) DFS1(x,y+1,d+1,right); if(mp[x+1][y]) DFS1(x+1,y,d+1,down); if(mp[x][y-1]) DFS1(x,y-1,d+1,left); } else { if(mp[x][y+1]) DFS1(x,y+1,d+1,right); if(mp[x+1][y]) DFS1(x+1,y,d+1,down); if(mp[x][y-1]) DFS1(x,y-1,d+1,left); if(mp[x-1][y]) DFS1(x-1,y,d+1,up); } } void DFS2(int x,int y,int d,int to)//右优先搜索 { if(flag2 == 1) return; if(b[x][y]=='E') { flag2 = 1; ri = d; return; } if(to == up) { if(mp[x][y+1]) DFS2(x,y+1,d+1,right); if(mp[x-1][y]) DFS2(x-1,y,d+1,up); if(mp[x][y-1]) DFS2(x,y-1,d+1,left); if(mp[x+1][y]) DFS2(x+1,y,d+1,down); }else if(to == left) { if(mp[x-1][y]) DFS2(x-1,y,d+1,up); if(mp[x][y-1]) DFS2(x,y-1,d+1,left); if(mp[x+1][y]) DFS2(x+1,y,d+1,down); if(mp[x][y+1]) DFS2(x,y+1,d+1,right); } else if(to == right) { if(mp[x+1][y]) DFS2(x+1,y,d+1,down); if(mp[x][y+1]) DFS2(x,y+1,d+1,right); if(mp[x-1][y]) DFS2(x-1,y,d+1,up); if(mp[x][y-1]) DFS2(x,y-1,d+1,left); } else { if(mp[x][y-1]) DFS2(x,y-1,d+1,left); if(mp[x+1][y]) DFS2(x+1,y,d+1,down); if(mp[x][y+1]) DFS2(x,y+1,d+1,right); if(mp[x-1][y]) DFS2(x-1,y,d+1,up); } } void BFS(int x,int y)//宽搜,注意手残 { int s=0,e=0; node t,f; f.x = x; f.y = y; f.ans = 0; vis[f.x][f.y] = 1; q[e++] = f; while(s < e) { t = q[s++]; if(b[t.x][t.y]=='E') { sum = t.ans; return ; } for(int i = 0;i<4;i++) { f.x = t.x + mv[i][0]; f.y = t.y + mv[i][1]; if(!vis[f.x][f.y] && 0<=f.x && f.x<h && 0<=f.y && f.y<w && mp[f.x][f.y]!=0) { f.ans = t.ans + 1; q[e++] = f; vis[f.x][f.y] = 1; } } } } void init() { flag1 = 0; flag2 = 0; memset(mp,0,sizeof(mp)); memset(vis,0,sizeof(vis)); } int main() { int t; char a[N]; scanf("%d",&t); while(t--) { init(); scanf("%d%d",&w,&h); for(int i = 0;i < h;i++) { scanf("%s",a); strcpy(b[i],a); for(int j = 0;j < w;j++) { if(a[j] == '.' || a[j] == 'E') mp[i][j] = 1; if(a[j] == 'S') { mp[i][j] = 1; sx = i; sy = j; } } } DFS1(sx,sy,0,up); // 如果初始化 都面向上,初始化任意,爱面向哪面向哪 DFS2(sx,sy,0,up); BFS(sx,sy); printf("%d %d %d ",le+1,ri+1,sum+1); } return 0; }