• POJ 3083 Children of the Candy Corn


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

    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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 
     6 using namespace std;
     7 
     8 int row,col;
     9 char map[50][50];
    10 int vis[50][50];
    11 
    12 int dir1[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
    13 int dir2[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    14 
    15 int DFS ( int r, int c, int way, int dir[][2] )
    16 {
    17     if ( map[r][c]=='E' )
    18         return 1;
    19     int t, a, b;
    20     for ( int i = 0; i < 4 ; i++ )
    21     {
    22         t = ( way + i ) % 4;
    23         a = r + dir[t][0];
    24         b = c + dir[t][1];
    25         if ( a <=row && a >= 1 && b <=col && b >= 1 && map[a][b] != '#' )
    26             return DFS ( a, b, (t+3) % 4, dir ) + 1;
    27     }
    28     return -1;
    29 }
    30 
    31 struct node{
    32     int x,y;
    33     int c;
    34 };
    35 
    36 int BFS(int si,int sj){
    37     queue<node> q;
    38     while(!q.empty())
    39         q.pop();
    40     node cur,next;
    41     memset(vis,0,sizeof(vis));
    42     cur.x=si,cur.y=sj,cur.c=1;
    43     vis[si][sj]=1;
    44     q.push(cur);
    45     while(!q.empty()){
    46         cur=q.front();
    47         q.pop();
    48         for(int i=0;i<4;i++){
    49             next.x=cur.x+dir1[i][0];
    50             next.y=cur.y+dir1[i][1];
    51             next.c=cur.c+1;
    52             if(next.x<1 || next.x>row || next.y<1 || next.y>col || map[next.x][next.y]=='#')
    53                 continue;
    54             if(map[next.x][next.y]=='E')
    55                 return next.c;
    56             if(!vis[next.x][next.y]){
    57                 vis[next.x][next.y]=1;
    58                 q.push(next);
    59             }
    60         }
    61     }
    62     return 0;
    63 }
    64 
    65 int main(){
    66 
    67     freopen("input.txt","r",stdin);
    68 
    69     int t,way;
    70     scanf("%d",&t);
    71     int si,sj;
    72     while(t--){
    73         cin>>col>>row;
    74         for(int i=1;i<=row;i++){
    75             for(int j=1;j<=col;j++){
    76                 cin>>map[i][j];
    77                 if(map[i][j]=='S'){
    78                     si=i,sj=j;
    79                 }
    80             }
    81         }
    82 
    83         if(si==1)
    84             way=0;
    85         else if(sj==1)
    86             way=1;
    87         else if(si==row)
    88             way=2;
    89         else if(sj==col)
    90             way=3;
    91 
    92         printf("%d %d %d\n",DFS(si,sj,way,dir1),DFS(si,sj,way,dir2),BFS(si,sj));
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    c# vs2010 excel 上传oracle数据
    Viola-Jones人脸检測
    apache commons-configuration包读取配置文件
    linux 读取文件
    linux 统计某个文件的行数
    linux 判空处理
    linux 查看某个目录下文件的数量
    nginx 配置文件正确性测试
    使用postman上传excel文件测试导入excel
    java 反射获取字段为List类型中的泛型类型
  • 原文地址:https://www.cnblogs.com/jackge/p/3024077.html
Copyright © 2020-2023  润新知