Children of the Candy Corn(简单深搜+简单广搜)

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

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到E按顺时针优先走的距离,S到E的最短距离*/
/*分析:由于顺逆时针走的时候会有死胡同有回溯,所以不能用BFS实现,因为BFS有标记数组,但是也有两种方法可以解决,一种是DFS,一种是巧妙一点儿的暴力。*/
 
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const LL Max = 1005;
const double esp = 1e-6;
//const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
using namespace std;
char arr[45][45];
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};
int ans,flag,w,h;
bool vis[45][45];
struct node{
    int x,y,z;
}ant,cnt;
void cal(int sx,int sy,int ex,int ey,int dis){ //DFS找
    if(sx == ex && sy == ey){
        flag = 0;
        return ;
    }
    dis = (dis + 3) % 4;
    for(int i=dis;i<dis+4;i++){
        int nowx = sx + dx[i%4];
        int nowy = sy + dy[i%4];
        if(nowx >= 0 && nowy >= 0 && nowx < h && nowy < w && arr[nowx][nowy]!='#'){
            ans += 1;
            cal(nowx,nowy,ex,ey,i);
            if(!flag)
                return ;
        }
    }
}
//void find(int d, int t)  //循环找  
//{  
//    int r = sr, c = sc, s = 1;  
//    while(r != er || c != ec)  
//    {  
//        d += 4 -t;  
//        while(map[r+dr[d%4]][c+dc[d%4]] == '#')  
//        {  
//            d += t;  
//        }  
//        r += dr[d%4];  
//        c += dc[d%4];  
//        s++;  
//    }  
//    printf("%d ", s);  
//}  
int fal(int sx,int sy,int ex,int ey){
    memset(vis,true,sizeof(vis));
    queue<node>Q;
    ant.x = sx;ant.y = sy;ant.z = 1;
    Q.push(ant);
    vis[sx][sy] = false;
    while(!Q.empty()){
        ant = Q.front();Q.pop();
        if(ant.x == ex && ant.y == ey)
            return ant.z;
        for(int i=0;i<4;i++){
            int nowx = ant.x + dx[i];
            int nowy = ant.y + dy[i];
            if(nowx >= 0 && nowy >= 0 && nowx < h && nowy < w && arr[nowx][nowy]!='#' && vis[nowx][nowy]){
                vis[nowx][nowy] = false;
                cnt.x = nowx;cnt.y = nowy;cnt.z = ant.z + 1;
                Q.push(cnt);
            }
        }
    }
}
int main(){
    int t,sx,sy,ex,ey;
    while(~scanf("%d",&t)){
        while(t-- && scanf("%d %d",&w,&h)){
            for(int i=0;i<h;i++){
                scanf("%s",arr[i]);
                for(int j=0;j<w;j++){
                    if(arr[i][j] == 'S'){
                        sx = i;sy = j;
                    }
                    if(arr[i][j] == 'E'){
                        ex = i;ey = j;
                    }
                }
            }
            ans = flag = 1;
            cal(sx,sy,ex,ey,0);
            printf("%d",ans);
            ans = flag = 1;
            cal(ex,ey,sx,sy,0);
            printf(" %d %d\n",ans,fal(sx,sy,ex,ey));
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值