您的位置:首页 > 其它

poj (3083) Children of the Candy Corn

2016-07-29 08:55 309 查看

原题:

Children of the Candy Corn

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12681 Accepted: 5439
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

思路:

这个题有点麻烦,容易出错,写完之后几乎用了半天的时候Debug。。。
具体做法就是在dfs中添加参数 d 标明在当前位置走的方向。s标明走的步数。

先找到S(x,y)点,然后分情况讨论,如果S在下,则其方向d为1(上),以此类推。
比如沿着左边走的情况:
从起点开始DFS_L(x,y,d,1);
如果是1(代表往上走):那么就先往右走,走不通再往上,还走不通就往左,还是走不通就往下。
如果是2,3,4 也以此类推,递归进行。详情看代码。
写的稍微麻烦了些。可以把move数组减少到两个,不过我因为在这个题用了很长时间懒得再改了- -。。。

代码:

#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
using namespace std;

string *map;
int step[50][50], T, w, h, Step;
bool vis[50][50], flag_l, flag_r;
int Move[4][2] = {0,-1, -1,0, 0,1, 1,0};
int move_r[4][2] = {-1,0, 0,1, 1,0, 0,-1};
int move_d[4][2] = {0,1, 1,0, 0,-1, -1,0};
int move_l[4][2] = {1,0, 0,-1, -1,0, 0,1};
int move_ru[][2] = {0,1, -1,0, 0,-1, 1,0};
// 右  上  左   下   右  上  左
int move_rr[][2] = {1,0, 0,1, -1,0, 0,-1};
int move_rd[][2] = {0,-1, 1,0, 0,1, -1,0};
int move_rl[][2] = {-1,0, 0,-1, 1,0, 0,1};
void BFS(int x, int y)
{
memset(vis,false,sizeof(vis));
pair<int,int>p;
queue<pair<int,int> >q;
step[x][y] = 1;
vis[x][y] = true;
p.first = x;
p.second = y;
q.push(p);
while(!q.empty())
{
p = q.front();
q.pop();
int xx, yy;
xx = p.first, yy = p.second;
for(int i = 0;i < 4;i++)
{
x = Move[i][0] + xx;
y = Move[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
if(!vis[x][y])
{
step[x][y] = step[xx][yy] + 1;
vis[x][y] = true;
if(map[x][y]=='E')
{cout<<step[x][y]<<endl;return;}
p.first = x, p.second = y;
q.push(p);
}
}
}
}

void DFS_L(int x, int y, int d, int s)
{
if(flag_l)
return;
if(!flag_l&&map[x][y]=='E')
{
cout<<s<<" ", flag_l = true;
return;
}
int xx = x, yy = y;
if(d == 1)//上
for(int i = 0;i < 4;i++)
{
x = Move[i][0] + xx;
y = Move[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_L(x,y,4,s+1);break;
case 1:DFS_L(x,y,d,s+1);break;
case 2:DFS_L(x,y,2,s+1);break;
case 3:DFS_L(x,y,3,s+1);break;
}
}
else if(d==2)//右
for(int i = 0;i < 4;i++)
{
x = move_r[i][0] + xx;
y = move_r[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_L(x,y,1,s+1);break;
case 1:DFS_L(x,y,d,s+1);break;
case 2:DFS_L(x,y,3,s+1);break;
case 3:DFS_L(x,y,4,s+1);break;
}
}
else if(d == 3)//下
for(int i = 0;i < 4;i++)
{
x = move_d[i][0] + xx;
y = move_d[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_L(x,y,2,s+1);break;
case 1:DFS_L(x,y,d,s+1);break;
case 2:DFS_L(x,y,4,s+1);break;
case 3:DFS_L(x,y,1,s+1);break;
}
}
else
for(int i = 0;i < 4;i++)
{
x = move_l[i][0] + xx;
y = move_l[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_L(x,y,3,s+1);break;
case 1:DFS_L(x,y,d,s+1);break;
case 2:DFS_L(x,y,1,s+1);break;
case 3:DFS_L(x,y,2,s+1);break;
}
}
}

void DFS_R(int x, int y, int d, int s)
{
if(flag_r)
return;
if(!flag_r&&map[x][y]=='E')
{
cout<<s<<" ", flag_r = true;
return;
}
int xx = x, yy = y;
if(d == 1)//上
for(int i = 0;i < 4;i++)
{
x = move_ru[i][0] + xx;
y = move_ru[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_R(x,y,2,s+1);break;
case 1:DFS_R(x,y,1,s+1);break;
case 2:DFS_R(x,y,4,s+1);break;
case 3:DFS_R(x,y,3,s+1);break;
}
}
else if(d==2)//右
for(int i = 0;i < 4;i++)
{
x = move_rr[i][0] + xx;
y = move_rr[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_R(x,y,3,s+1);break;
case 1:DFS_R(x,y,d,s+1);break;
case 2:DFS_R(x,y,1,s+1);break;
case 3:DFS_R(x,y,4,s+1);break;
}
}
else if(d == 3)//下
for(int i = 0;i < 4;i++)
{
x = move_rd[i][0] + xx;
y = move_rd[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_R(x,y,4,s+1);break;
case 1:DFS_R(x,y,d,s+1);break;
case 2:DFS_R(x,y,2,s+1);break;
case 3:DFS_R(x,y,1,s+1);break;
}
}
else
for(int i = 0;i < 4;i++)
{
x = move_rl[i][0] + xx;
y = move_rl[i][1] + yy;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
continue;
switch(i)
{
case 0:DFS_R(x,y,1,s+1);break;
case 1:DFS_R(x,y,d,s+1);break;
case 2:DFS_R(x,y,3,s+1);break;
case 3:DFS_R(x,y,2,s+1);break;
}
}
}

int main()
{
cin>>T;
while(T--)
{
flag_l = false, flag_r = false;
cin>>w>>h;
map = new string[h];
for(int i = 0;i < h;i++)
cin>>map[i];
for(int i = 0;i < h;i++)
{
if(map[i][0] == 'S')
DFS_L(i,0,2,1), DFS_R(i,0,2,1), BFS(i,0);
else if(map[i][w-1]=='S')
DFS_L(i,w-1,4,1), DFS_R(i,w-1,4,1), BFS(i,w-1);
}
for(int i = 0;i < w;i++)
{
if(map[0][i]=='S')
DFS_L(0,i,3,1), DFS_R(0,i,3,1), BFS(0,i);
else if(map[h-1][i]=='S')
{
DFS_L(h-1,i,1,1);
DFS_R(h-1,i,1,1);
BFS(h-1,i);
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: