zoukankan      html  css  js  c++  java
  • Rescue--hdu1242

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 21431    Accepted Submission(s): 7641


    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     
    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

    Process to the end of the file.
     
    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
     
    Sample Input
    7 8
    #.#####.
    #.a#..r.
    #..#x...
    ..#..#.#
    #...##..
    .#......
    ........
     
    Sample Output
    13
     

    这个题是用广搜写的!但是这个题是有点特殊的,因为遇到X的时候时间是要再加一的!所以可以用优先队列来解决,但是还是可以用普通队列来解决的,方法就是遇到X先加一,然后标记走过,再后来再遇到X直接加一,不再往四周搜索!等于走了两步!详情如下

    普通队列版:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 #define ma 210
     6 using namespace std;
     7 char map[ma][ma];
     8 int v[ma][ma],m,n;
     9 int mov[4][2]={1,0,-1,0,0,1,0,-1};
    10 struct node
    11 {
    12     int x,y,step,flag;
    13 };
    14 bool can(node x)
    15 {
    16     if(x.x>=0&&x.x<m&&x.y>=0&&x.y<n&&(map[x.x][x.y]!='#')&&!v[x.x][x.y])
    17     return true ;
    18     return false;
    19 }
    20 int bfs(int x,int y)
    21 {
    22     int i;
    23     memset(v,0,sizeof(v));
    24     queue<node>q;
    25     node ta,tb;
    26     ta.x=x;
    27     ta.y=y;
    28     ta.step=0;
    29     ta.flag=0;
    30     q.push(ta);
    31     while(!q.empty())
    32     {
    33         ta=q.front();
    34         q.pop();
    35         if(ta.flag==1)
    36         {
    37             ta.step++;
    38             ta.flag=0;
    39             q.push(ta);
    40             continue;
    41         }//第二次遇到时,flag已经等于1,这时候不能走了,步数要加一,而且flag要重新标记为0
    42         if(map[ta.x][ta.y]=='a')
    43             return ta.step;
    44         for(i=0;i<4;i++)
    45         {
    46             tb.x=ta.x+mov[i][0];
    47             tb.y=ta.y+mov[i][1];
    48             tb.step=ta.step+1;
    49             if(can(tb))
    50             {
    51 
    52                 if(map[tb.x][tb.y]=='x')
    53                 tb.flag=1;
    54                 else
    55                 tb.flag=0;//第一次遇到X就标记为flag=1,否则为0!
    56                 v[tb.x][tb.y]=1;
    57                 q.push(tb);
    58             }
    59         }
    60     }
    61     return -1;    
    62 }
    63 int main()
    64 {
    65     int i,j,a,b;
    66     while(scanf("%d%d",&m,&n)!=EOF)
    67     {
    68         getchar();
    69         for(i=0;i<m;i++)
    70         {
    71             for(j=0;j<n;j++)
    72             {
    73                 scanf("%c",&map[i][j]);
    74                 if(map[i][j]=='r')
    75                 {
    76                     a=i;
    77                     b=j;
    78                 }
    79             }
    80             getchar();
    81         }
    82         v[a][b]=1;
    83         int rr=bfs(a,b);
    84         if(rr==-1)
    85         printf("Poor ANGEL has to stay in the prison all his life.
    ");
    86         else
    87         printf("%d
    ",rr);
    88     }
    89     return 0;
    90  } 

    优先队列版:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 #define maxn 210
     6 using namespace std;
     7 int m,n,v[maxn][maxn],mov[4][2]={1,0,-1,0,0,1,0,-1};
     8 char map[maxn][maxn];
     9 
    10 struct node
    11 {
    12     int x,y,step;
    13     friend bool operator <(node x,node y)
    14     {
    15         return x.step>y.step;
    16     }
    17 };
    18 priority_queue<node>q;
    19 bool can(node x)
    20 {
    21     if(!v[x.x][x.y]&&x.x>=0&&x.x<m&&x.y>=0&&x.y<n&&map[x.x][x.y]!='#')//(map[x.x][x.y]=='.'||map[x.x][x.y]=='x'))
    22     return  true;
    23     return false;
    24  } 
    25 int bfs(int x,int y)
    26 {
    27     int i;
    28     node ta,tb;
    29     ta.x=x;
    30     ta.y=y;
    31     ta.step=0;
    32     q.push(ta);
    33     while(!q.empty())
    34     {
    35         ta=q.top();
    36         q.pop();
    37         if(map[ta.x][ta.y]=='a')
    38             return ta.step;//到终点就返回队首,用的优先队列所以队首的步数最少!
    39         for(i=0;i<4;i++)
    40         {
    41             tb.x=ta.x+mov[i][0];
    42             tb.y=ta.y+mov[i][1];
    43             if(can(tb))
    44             {
    45                 if(map[tb.x][tb.y]=='x')
    46                 tb.step=ta.step+2;//遇到X一定要加2
    47                 else
    48                 tb.step=ta.step+1;//其他加1
    49                 v[tb.x][tb.y]=1;
    50                 q.push(tb);
    51             }
    52 
    53         }
    54     }
    55     return -1;//找不到就返回-1
    56 }
    57 int main()
    58 {
    59     int i,j,a,b;
    60     while(scanf("%d%d",&m,&n)!=EOF)
    61     {
    62         getchar();
    63         memset(v,0,sizeof(v));
    64         for(i=0;i<m;i++)
    65         {
    66             for(j=0;j<n;j++)
    67             {
    68                 scanf("%c",&map[i][j]);
    69                 if(map[i][j]=='r')
    70                 a=i,b=j;
    71             }
    72             getchar();
    73         }
    74         v[a][b]=1;
    75         int rr=bfs(a,b);
    76         if(rr==-1)
    77         printf("Poor ANGEL has to stay in the prison all his life.
    ");
    78         else
    79         printf("%d
    ",rr);
    80     }
    81     return 0;
    82 } 
  • 相关阅读:
    Volume serial number could associate file existence on certain volume
    分区表收集统计信息
    数据泵导入外键表数据报错
    Oracle备份优化开启块改变跟踪
    Oracle asm lib存储扩容及测试
    ASM_Lib_linux_redhat6.9添加asm磁盘扩容
    ogg清理无法自动清理导致占用大量空间处理
    应用复制进程abend,报错OGG-01163字段列长度不够
    历史备份过多使用delete obsolete方式找不到过期备份信息???
    测试使用块跟踪文件
  • 原文地址:https://www.cnblogs.com/Eric-keke/p/4715390.html
Copyright © 2011-2022 走看看