POJ 3083 Children of the Candy Corn

传送门:http://poj.org/problem?id=3083

  

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
 
 
此题比较麻烦,要贴墙走,起初没有思路,后来仔细查看题意后,明白有几种状态:
下面以贴左墙壁为例:
首先肯定有四个方向,假设向上为1,向左为2,向下为3,向左为4;
刚进入口时肯定有方向,即若入口在最下面一排,则初人脸方向肯定是向左,(下面方向为人脸朝向方向的那面)
人的左面有下面几种情况:(1).墙或入口 (2).路 (3).出口
(1):判断人的前面是什么,若是墙,则肯定要转向,即方向+1(若大于4则方向为1)。若是路,则肯定接着向前走,若是出口,嘿嘿,那就到头了。
(2):若人的左面是路,则肯定要转向,即方向-1(若小于1则方向为4),并向前走一步,
(3):若人的左面是出口,则直接向左一步走,
 
 
 
 
以上就是分析过程,	
  
  
  
  
001. #include<stdio.h>
002. #include<string.h>
003. #include<queue>
004. using namespace std;
005. int x1,y1,fx[5][2]= {0,0,-1,0,  0,1,  1,0,  0,-1};
006. char map[50][50];
007. struct stu
008. {
009. int x,y,step;
010. } d;
011. void shun(int x,int y,int c,int s)
012. {
013. int c1=(c>=4?1:c+1);
014. int w=(c<=1?4:c-1);
015. if(map[x+fx[w][0]][y+fx[w][1]]=='#')
016. {
017. if(map[x+fx[c][0]][y+fx[c][1]]=='.')
018. shun(x+fx[c][0],y+fx[c][1],c,s+1);
019. if(map[x+fx[c][0]][y+fx[c][1]]=='#')
020. shun(x,y,c1,s);
021. if(map[x+fx[c][0]][y+fx[c][1]]=='E')
022. printf("%d ",s+1);
023. }
024. if(map[x+fx[w][0]][y+fx[w][1]]=='.')
025. shun(x+fx[w][0],y+fx[w][1],w,s+1);
026. if(map[x+fx[w][0]][y+fx[w][1]]=='E')
027. printf("%d ",s+1);
028. }
029. void ni(int x,int y,int c,int s)
030. {
031. int c1=(c>=4?1:c+1);
032. int w=(c<=1?4:c-1);
033. if(map[x+fx[c1][0]][y+fx[c1][1]]=='#')
034. {
035. if(map[x+fx[c][0]][y+fx[c][1]]=='.')
036. ni(x+fx[c][0],y+fx[c][1],c,s+1);
037. if(map[x+fx[c][0]][y+fx[c][1]]=='#')
038. ni(x,y,w,s);
039. if(map[x+fx[c][0]][y+fx[c][1]]=='E')
040. printf("%d ",s+1);
041. }
042. if(map[x+fx[c1][0]][y+fx[c1][1]]=='.')
043. ni(x+fx[c1][0],y+fx[c1][1],c1,s+1);
044. if(map[x+fx[c1][0]][y+fx[c1][1]]=='E')
045. printf("%d ",s+1);
046. }
047. int bfs(stu s)
048. {
049. queue<stu>q;
050. q.push(s);
051. stu t;
052. while(!q.empty())
053. {
054. int i;
055. s=q.front();
056. q.pop();
057. if(map[s.x][s.y]=='E')
058. return s.step;
059. else
060. for(i=1; i<=4; i++)
061. {
062. t.x=s.x+fx[i][0];
063. t.y=s.y+fx[i][1];
064. if(map[t.x][t.y]=='E')
065. {
066. t.step=s.step+1;
067. q.push(t);
068. }
069. if(map[t.x][t.y]=='.')
070. {
071. t.step=s.step+1;
072. map[t.x][t.y]='#';
073. q.push(t);
074. }
075. }
076. }
077. }
078. int main()
079. {
080. int N;
081. scanf("%d",&N);
082. while(N--)
083. {
084. memset(map,0,sizeof(map));
085. int i,j;
086. scanf("%d%d",&y1,&x1);
087. for(i=1; i<=x1; i++)
088. {
089. getchar();
090. for(j=1; j<=y1; j++)
091. {
092. scanf("%c",&map[i][j]);
093. if(map[i][j]=='S')
094. d.x=i,d.y=j;
095. }
096. }
097. int xx,c,yy,cc;
098. if(d.x==1) c=2,xx=2,yy=d.y,cc=4;
099. if(d.y==y1)  c=3,xx=d.x,yy=d.y-1,cc=1;
100. if(d.x==x1)  c=4,xx=d.x-1,yy=d.y,cc=2;
101. if(d.y==1)   c=1,xx=d.x,yy=2,cc=3;
102. map[d.x][d.y]='#';
103. shun(xx,yy,c,2);
104. ni(xx,yy,cc,2);
105. d.step=1;
106. printf("%d\n",bfs(d));
107. }
108. }
不知道为什么在poj G++交无限wa,c++AC
/(ㄒoㄒ)/~~

你可能感兴趣的:(队列,poj,广搜)