POJ3083——Children of the Candy Corn

Children of the Candy Corn

Description
The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.
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
Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks (‘#‘), empty space by periods (‘.‘), the start by an ‘S‘ and the exit by an ‘E‘.
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
For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S‘ and ‘E‘) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9

题目大意:

    一个迷宫,‘.‘,‘S‘,‘E‘为可以到达的点,‘#‘为墙。 (S点保证在迷宫边缘,且只有一个方向可以走)

    规定了三种行动方式(计算从S->N的路程)

    1)优先走当前方向的左方,前方,右方,后方。

    2)优先走当前方向的右方,前方,左方,后方。

    3)S->E的最短路径。

结题思路:

    用DFS求前两个行动方式的解。

    使用0123来表示当前所在位置的方向。 0前 1右 2后 3左

    再通过当前的方向来确定递归的优先级。

    BFS求最短路径。

Code:

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<string>
  4 #include<cstring>
  5 #include<memory.h>
  6 #include<algorithm>
  7 #define MAXN 41
  8 using namespace std;
  9 struct qu
 10 {
 11     int x,y;
 12 }q[2000];
 13 int N,M,end_i,end_j;
 14 bool vis[MAXN+10][MAXN+10],flag[MAXN+10][MAXN+10];
 15 int dis[2000],Lstep,Rstep;
 16 void dfs_left(int x1,int y1,int d)
 17 {
 18     Lstep++;
 19     if (x1==end_i&&y1==end_j) return ;
 20     if (d==0)
 21     {
 22         if (flag[x1][y1-1]) dfs_left(x1,y1-1,3);
 23         else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0);
 24         else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1);
 25         else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2);
 26     }
 27     if (d==1)
 28     {
 29         if (flag[x1-1][y1]) dfs_left(x1-1,y1,0);
 30         else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1);
 31         else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2);
 32         else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3);
 33     }
 34     if (d==2)
 35     {
 36         if (flag[x1][y1+1]) dfs_left(x1,y1+1,1);
 37         else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2);
 38         else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3);
 39         else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0);
 40     }
 41     if (d==3)
 42     {
 43         if (flag[x1+1][y1]) dfs_left(x1+1,y1,2);
 44         else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3);
 45         else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0);
 46         else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1);
 47     }
 48 }
 49 void dfs_right(int x1,int y1,int d)
 50 {
 51     Rstep++;
 52     if (x1==end_i&&y1==end_j) return ;
 53     if (d==0)
 54     {
 55         if (flag[x1][y1+1]) dfs_right(x1,y1+1,1);
 56         else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0);
 57         else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3);
 58         else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2);
 59     }
 60     if (d==1)
 61     {
 62         if (flag[x1+1][y1]) dfs_right(x1+1,y1,2);
 63         else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1);
 64         else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0);
 65         else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3);
 66     }
 67     if (d==2)
 68     {
 69         if (flag[x1][y1-1]) dfs_right(x1,y1-1,3);
 70         else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2);
 71         else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1);
 72         else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0);
 73     }
 74     if (d==3)
 75     {
 76         if (flag[x1-1][y1]) dfs_right(x1-1,y1,0);
 77         else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3);
 78         else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2);
 79         else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1);
 80     }
 81 }
 82 int bfs(int x1,int y1)
 83 {
 84     int front=0,rear=1;
 85     dis[front]=1;
 86     q[front].x=x1,q[front].y=y1;
 87     while (front<rear)
 88     {
 89         int x=q[front].x,y=q[front].y;
 90         if (x==end_i&&y==end_j) break;
 91         int a[4]={x+1,x-1,x,x},b[4]={y,y,y+1,y-1};
 92         for (int i=0;i<=3;i++)
 93         {
 94             if (!(a[i]<=0||a[i]>=M+1||b[i]<=0||b[i]>=N+1)&&vis[a[i]][b[i]]==0){
 95                 q[rear].x=a[i],q[rear].y=b[i];
 96                 dis[rear]=dis[front]+1;
 97                 vis[a[i]][b[i]]=1;
 98                 rear++;
 99             }
100         }
101         front++;
102     }
103     return dis[front];
104
105 }
106 int main()
107 {
108     int T,start_i,start_j;
109     cin>>T;
110     while (T--)
111     {
112         Lstep=Rstep=0;
113         memset(vis,0,sizeof(vis));
114         memset(dis,0,sizeof(dis));
115         memset(q,0,sizeof(q));
116         int d;
117         cin>>N>>M;
118         getchar();
119         for (int i=1; i<=M; i++)
120         {
121             for (int j=1; j<=N; j++)
122             {
123                 char tmp;
124                 scanf("%c",&tmp);
125                 flag[i][j]=1;
126                 if (tmp==‘#‘) vis[i][j]=1,flag[i][j]=0;
127                 if (tmp==‘E‘) end_i=i,end_j=j;
128                 if (tmp==‘S‘) start_i=i,start_j=j;
129             }
130             getchar();
131         }
132         if (start_i==1) d=2;
133         else if (start_i==M) d=0;
134         else if (start_j==1) d=1;
135         else if (start_j==N) d=3;
136         dfs_left(start_i,start_j,d);
137         dfs_right(start_i,start_j,d);
138         int step1=bfs(start_i,start_j);
139         printf("%d %d %d\n",Lstep,Rstep,step1);
140     }
141     return 0;
142 }

POJ3083——Children of the Candy Corn

时间: 2024-10-07 00:29:09

POJ3083——Children of the Candy Corn的相关文章

poj3083 Children of the Candy Corn BFS&amp;&amp;DFS

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Accepted: 4841 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

dfs/poj3083 Children of the Candy Corn

1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 5 using namespace std; 6 typedef pair<int,int>P; 7 const int dx[4]={1,0,-1,0}; 8 const int dy[4]={0,1,0,-1}; 9 const int INF=1e9; 10 int sx,sy,ex,ey,n,m; 11 char a[50][50];

poj3083 Children of the Candy Corn

这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步方向可以确定下一步应该从哪个方向开始搜.比如说,是向北走的,就必须先搜西,西不可以走,再搜北,如果北还不可以走,再搜东,最后才是南.其他方向的情况也可以这样推出来.最后走到E点完成了.广搜就是最基础的广搜.这道题做了将近10个小时.中途曾几次准备放弃,但最后还是坚持做完了. #include<ios

POJ-3083 Children of the Candy Corn (BFS+DFS)

Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. One popular maze-

POJ 3083 Children of the Candy Corn

Children of the Candy Corn Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 308364-bit integer IO format: %lld      Java class name: Main The cornfield maze is a popular Halloween treat. Visitors are shown the

K - Children of the Candy Corn

K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze fa

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

【POJ 3083】Children of the Candy Corn

POJ[3083]Children of the Candy Corn Dfs+Bfs 分别求沿左墙到达E 沿右墙到达E 还有S到E的最短步数 前两个Dfs实现 最后一个Bfs 耐心写很容易A 主要注意方向问题 dir四个方向 上右下左 刚开始我分别用下标0 1 2 3代表 开dirx diry两个移动数组 假设前一状态朝向0(上) 沿左墙移动即为3 0 1 2(左上右下<顺时针>) 沿右墙即为1 0 3 2(右上左下<逆时针>) 同理其余方向很容易遍历 略自豪的是不断精简代码从6

POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)

题目链接:POJ 3083 Children of the Candy Corn [题意]给出一个迷宫,不超过40*40,'#'代表墙,'.'代表能走,'S'是起点,'E'是终点.分别求出从起点一直沿左走,一直沿右走,走到终点所需要的步数.以及走出迷宫的最小步数. [思路]首先最小步数很简单,一个普通BFS搞定,这道题重点是一直向左走和一直向右走的DFS的方向问题,方向还和游客当时朝向有关.开始一直认为是每次都向左(右)转,直到可以走,然后就一直不对,在google了之后才知道向左走要遵循左上右