POJ 3083 C - Children of the Candy Corn

4 篇文章 0 订阅


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

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


这道题bfs求最短距离还是蛮简单的,就是求左旋转有旋转的步数想不通怎么旋转的话可能会有点难弄,我的思路是定义顺时针的方向数组,左旋的话直接从起点出发按照方向数组模拟搜索终点,而右旋的话是从终点出发也按照顺时针搜索起点(就等同于从起点出发按照逆时针方向数组模拟搜索终点= - = ),还有一点就是代码dfs中的方向选择,也就是int j=(i+d+3)%4;,这句话有点难理解,具体的方向变化如下:

                                                                                             当前方向     检索顺序
                                                                                                  ↑ :        ← ↑ → ↓
                                                                                                 → :          ↑ → ↓ ← 

                                                                                                   ↓ :       → ↓ ← ↑ 

                                                                                                  ← :         ↓ ← ↑ → 

所以方向就变成了(i+d+3)%4。
代码如下:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int sx, sy, ex, ey, d1,d2, n, m;
int next[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //方向数组,这里是顺时针
char s[40+5][40+5];
struct node {
    int x, y, step;
};

void bfs() { //bfs求最短距离
    bool vis[40+5][40+5];
    memset(vis,0,sizeof(vis));
    node q[10000+5];
    int head=0, tail=0;
    q[tail].x=sx, q[tail].y=sy;
    q[tail++].step=1;
    vis[sx][sy]=1;
    while(head<tail) {
        for(int i=0;i<4;i++) {
            int tx=q[head].x;
            int ty=q[head].y;
            tx+=next[i][0];
            ty+=next[i][1];
            if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]&&s[tx][ty]!='#') {
                vis[tx][ty]=1;
                q[tail].x=tx;
                q[tail].y=ty;
                q[tail++].step=q[head].step+1;
            }
            if(q[tail-1].x==ex&&q[tail-1].y==ey) {cout<<q[tail-1].step<<endl;return ;}
        }
        head++;
    }
}

void dfs(int x, int y, int d, int step, bool flag) { //dfs求左旋右旋步数,标识变量flag记录左旋(0)右旋(1)
    for(int i=0;i<n;i++) {
        int j=(i+d+3)%4; 
        int tx=x+next[j][0];
        int ty=y+next[j][1];
        if(flag&&s[tx][ty]=='S') {cout<<step+1<<' ';return ;}
        if(!flag&&s[tx][ty]=='E') {cout<<step+1<<' ';return ;}
        if(tx>=0&&tx<n&&ty>=0&&ty<m&&s[tx][ty]!='#') {
            return dfs(tx,ty,j,step+1,flag);
        }
    }
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        cin>>m>>n;
        for(int i=0;i<n;i++)
            cin>>s[i];
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) {
            if(s[i][j]=='S'){
                sx=i;
                sy=j;
                if(i==0) d1=2;
                else if(i==n-1) d1=0;//d1记录左旋初始方向
                if(j==0) d1=1;
                else if(j==m-1) d1=3;
            }
            else if(s[i][j]=='E') {
                ex=i;
                ey=j;
                if(i==0) d2=2;
                else if(i==n-1) d2=0;//d2记录右旋初始方向
                if(j==0) d2=1;
                else if(j==m-1) d2=3;
            }
        }
    dfs(sx,sy,d1,1,0); 
    dfs(ex,ey,d2,1,1); 
    bfs();
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值