zoukankan      html  css  js  c++  java
  • POJ 3083 Children of the Candy Corn

    Children of the Candy Corn

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3083
    64-bit integer IO format: %lld      Java class name: Main
     
    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

     
    解题:搜索
     
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 char mp[50][50];
    18 int n,m,sx,sy,vis[50][50];
    19 int dir[4][2] = {0,-1,-1,0,0,1,1,0};
    20 int dir2[4][2] = {0,1,-1,0,0,-1,1,0};
    21 queue< pii >q;
    22 int bfs() {
    23     memset(vis,0,sizeof(vis));
    24     while(!q.empty()) q.pop();
    25     vis[sx][sy] = 1;
    26     q.push(make_pair(sx,sy));
    27     while(!q.empty()) {
    28         int x = q.front().first;
    29         int y = q.front().second;
    30         q.pop();
    31         for(int i = 0; i < 4; i++) {
    32             int tx = dir[i][0]+x;
    33             int ty = dir[i][1]+y;
    34             if(mp[tx][ty] == '#' || vis[tx][ty]) continue;
    35             vis[tx][ty] = vis[x][y]+1;
    36             if(mp[tx][ty] == 'E') return vis[tx][ty];
    37             q.push(make_pair(tx,ty));
    38         }
    39     }
    40     return -1;
    41 }
    42 int dfs(int x,int y,int v,int step,int (*dir)[2]) {
    43     if(mp[x][y] == 'E') return step;
    44     for(int i = 0; i < 4; i++) {
    45         int tx = dir[(v+i)%4][0]+x;
    46         int ty = dir[(v+i)%4][1]+y;
    47         if(mp[tx][ty] != '#') return dfs(tx,ty,(v+i+3)%4,step+1,dir);
    48     }
    49     return -1;
    50 }
    51 int main() {
    52     int t,i,j;
    53     scanf("%d",&t);
    54     while(t--) {
    55         scanf("%d %d",&m,&n);
    56         getchar();
    57         memset(mp,'#',sizeof(mp));
    58         for(i = 1; i <= n; i++) {
    59             for(j = 1; j <= m; j++) {
    60                 mp[i][j] = getchar();
    61                 if(mp[i][j] == 'S') {
    62                     sx = i;
    63                     sy = j;
    64                 }
    65             }
    66             getchar();
    67         }
    68         printf("%d %d %d
    ",dfs(sx,sy,0,1,dir),dfs(sx,sy,0,1,dir2),bfs());
    69     }
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    【bzoj3083】遥远的国度 树链剖分+线段树
    【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
    xml、json的序列化与反序列化
    什么是安全证书,访问者到底是怎么校验安全证书的,服务端返回安全证书后,客户端再向谁验证呢?
    查看发票组代码后的总结和有感
    网址的正则表达式、常用正则表达式、在线正则表达式检测
    XamlParseException异常
    委托,lambda,匿名方法
    windows中断与共享的连接(samba)
    linux ubuntu 11.04 samba 服务器设置
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3960908.html
Copyright © 2011-2022 走看看