poj 3083 搜索

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8062 Accepted: 3529

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
这是一道很有意思的搜索题。首先这个题是放在深搜的类型里的所以一上来关于向左和向右两种方式的搜索是用深搜的,但是再求最短路的思路是要用bfs。如果还是用深搜就错了。
这个题的关键就是方向,
我在网上看到的大牛 的定义是这样的,这里借鉴一下;
 
 

左转、右转优先搜索时必须标记当前位置时的方向,我定义的方向是

                             0

                             |

                             |

                      1--------当前--------3

                              |

                              |

                              2

     最初的方向由起点S确定,而下一步的方向则由前一步的走向决定

例如 左边优先搜索:

当前位置的方向指向 1(向左),(这同时说明前一步是在第3的位置走过来的)

那么走下一步时,就要根据2103的顺序,先逐格确定当前位置周边的四格是否可行

若第一次确认2可行,就走到2,在位置2时的方向为2(向下)

2不可行,则再确定1,若1可行,就走到1,在位置1时的方向为1(向左)

  若1也不可行,则再确定0,若0可行,就走到0,在位置0时的方向为0(向上)

  若0也不可行,说明进入了迷宫的死胡同,要从原路返回,走回3

右边优先搜索也同理。

根据我定义的方向,设当前位置为d,那么

左转,用数学式子表达就是  d=(d+1)%4

右转,用数学式子表达就是  d=(d+3)%4     

这里有一点必须要注意的:

左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

下面是代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct node{
    int x,y;
    int step;
}a[1600];

char map[60][60];
int visit[60][60];
int fx[]={0,1,0,-1};
int fy[]={1,0,-1,0};
int fr[]={1,0,3,2};
int fl[]={3,0,1,2};//一定要注意这些方向数组,我写的时候快被整疯了
int dir[4][2]={1,0,-1,0,0,1,0,-1};
char sx,sy,ex,ey;
int w,h;
int count,d;

void dfsleft(int d,int x,int y){
     int tx,ty;
     if(x==ex&&y==ey){//到达终点
           printf("%d ",count);
           return;
     }
     count++;
     for(int j=0;j<4;j++){
            int i=(d+fl[j])%4;
            tx=x+fx[i];
            ty=y+fy[i];
            if(tx>=0&&tx<h&&ty>=0&&ty<w&&map[tx][ty]!='#'){
                 dfsleft(i,tx,ty);
                 return;
            }
     }
}

void dfsright(int d,int x,int y){
     int tx,ty;
     if(x==ex&&y==ey){
           printf("%d ",count);
           return;
     }
     for(int j=0;j<4;j++){
            int i=(d+fr[j])%4;
            tx=x+fx[i];
            ty=y+fy[i];
            if(tx>=0&&tx<h&&ty>=0&&ty<w&&map[tx][ty]!='#'){
                 count++;
                 dfsright(i,tx,ty);
                 return;
            }
     }
}

void bfs(int sx,int sy){
     a[0].x=sx;
     a[0].y=sy;
     a[0].step=0;
     int front=0;
     int rear=1;
     int tx,ty;
     memset(visit,0,sizeof(visit));
     visit[sx][sy]=1;
     while(front<rear){
          node cur,exchange;
          cur=a[front++];
          if(cur.x==ex&&cur.y==ey){
               printf("%d\n",cur.step+1);
          }
          for(int i=0;i<4;i++){
              tx=cur.x+dir[i][0];
              ty=cur.y+dir[i][1];
              if(tx>=0&&tx<h&&ty>=0&&ty<w&&visit[tx][ty]==0&&map[tx][ty]!='#'){
                          visit[tx][ty]=1;
                          exchange.x=tx;
                          exchange.y=ty;
                          exchange.step=cur.step+1;
                          a[rear++]=exchange;
              }
          }
     }
}

int main(){
    int T;
    cin>>T;
    while(T--){
         cin>>w>>h;
         for(int i=0;i<h;i++){
                scanf("%s",map[i]);
             for(int j=0;j<w;j++){
                 if(map[i][j]=='S'){
                       sx=i;
                       sy=j;
                 }
                 if(map[i][j]=='E'){
                       ex=i;
                       ey=j;
                 }
             }
         }
         if(sx==0) d=0;
         if(sx==h-1) d=2;
         if(sy==0) d=1;
         if(sy==w-1) d=3;
         count=1;
         dfsleft(d,sx,sy);
         count=1;
         dfsright(d,sx,sy);
         bfs(sx,sy);
    }
    return 0;
}


 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值