您的位置:首页 > 其它

poj 3083 Children of the Candy Corn

2014-04-24 23:20 447 查看
Children of the Candy Corn

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 8986Accepted: 3919
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

dfs+bfs

代码虽然长,但其实都是复制粘贴,稍微加些改动就行了。向左和向右优先时,每一步都只需记下上下左右四个方向的一个即可。最后用广搜 +优先队列 解决最短路径。

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 45
char map

;
int ei,ej,w,h;
int a1,a2,a3;
int f1,f2,f3;
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int mark

;
struct node
{
int x,y,step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
};
int judge(int x,int y)
{
if(x>=0&&x<h&&y>=0&&y<w&&map[x][y]!='#')
return 1;
return 0;
}
void dfs_left(int x,int y,int ff,int cnt)
{
int di,dj;
if(f1)
return ;
if(x==ei&&y==ej)
{
a1=cnt;
f1=1;
return ;
}
if(ff==1)
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_left(di,dj,4,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,1,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_left(di,dj,2,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,3,cnt+1);
}
}
}
}
else if(ff==2)
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,1,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_left(di,dj,2,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,3,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_left(di,dj,4,cnt+1);
}
}
}
}
else if(ff==3)
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_left(di,dj,2,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,3,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_left(di,dj,4,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,1,cnt+1);
}
}
}
}
else if(ff==4)
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,3,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_left(di,dj,4,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_left(di,dj,1,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_left(di,dj,2,cnt+1);
}
}
}
}
return ;
}
void dfs_right(int x,int y,int ff,int cnt)
{
int di,dj;
if(f2)
return ;
if(x==ei&&y==ej)
{
a2=cnt;
f2=1;
return ;
}
if(ff==1)
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_right(di,dj,2,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,1,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_right(di,dj,4,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,3,cnt+1);
}
}
}
}
else if(ff==2)
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,3,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_right(di,dj,2,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,1,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_right(di,dj,4,cnt+1);
}
}
}
}
else if(ff==3)
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_right(di,dj,4,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,3,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_right(di,dj,2,cnt+1);
else
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,1,cnt+1);
}
}
}
}
else if(ff==4)
{
di=x-1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,1,cnt+1);
else
{
di=x;
dj=y-1;
if(judge(di,dj))
dfs_right(di,dj,4,cnt+1);
else
{
di=x+1;
dj=y;
if(judge(di,dj))
dfs_right(di,dj,3,cnt+1);
else
{
di=x;
dj=y+1;
if(judge(di,dj))
dfs_right(di,dj,2,cnt+1);
}
}
}
}
return ;
}
int bfs(int x,int y,int cnt)
{
int i,di,dj;
priority_queue<node>q;
node cur,next;
cur.step=cnt;
cur.x=x;
cur.y=y;
mark[x][y]=1;
q.push(cur);
while(!q.empty())
{
cur=q.top();
if(cur.x==ei&&cur.y==ej)
return cur.step;
q.pop();
for(i=0;i<4;i++)
{
next.x=di=cur.x+dir[i][0];
next.y=dj=cur.y+dir[i][1];
next.step=cur.step+1;
if(judge(di,dj)&&!mark[di][dj])
{
mark[di][dj]=1;
q.push(next);
}
}
}
return 0;
}
int main()
{
int i,j,T;
int si,sj,ff;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&h);
for(i=0;i<h;i++)
scanf("%s",map[i]);
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
if(map[i][j]=='E')
{
ei=i;
ej=j;
}
if(map[i][j]=='S')
{
si=i;
sj=j;
if(i==0)       //从上面往下搜
ff=3;
else if(i==h-1)     //从下面往上搜
ff=1;
else if(j==0)   //从左面往右搜
ff=2;
else            //从右面往左搜
ff=4;
}
}
f1=f2=f3=0;
a1=a2=h*w;
dfs_left(si,sj,ff,1);
dfs_right(si,sj,ff,1);
memset(mark,0,sizeof(mark));
a3=bfs(si,sj,1);
printf("%d %d %d\n",a1,a2,a3);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: