zoukankan      html  css  js  c++  java
  • HDU 1242 Rescus(记忆化搜索)

                  Rescue

    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
     
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define inf 99999
     5 using namespace std;
     6 
     7 char graph[205][205];
     8 bool visit[205][205];
     9 int dp[205][205];
    10 int m,n,cnt;
    11 int ex,ey,sx,sy;
    12 int d[4][2]={1,0,0,1,0,-1,-1,0};
    13 
    14 void dfs(int x,int y)
    15 {
    16     if(dp[x][y]>=dp[ex][ey])return;
    17     for(int i=0;i<4;i++)
    18     {
    19         int xx=x+d[i][0];
    20         int yy=y+d[i][1];
    21         if(xx<0||xx>=m||yy<0||yy>=n||graph[xx][yy]=='#'||visit[xx][yy]==false)continue;
    22         if(graph[xx][yy]=='x')
    23         {
    24             if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+2)
    25             dp[xx][yy]=dp[x][y]+2;
    26             else continue;
    27         }
    28         else if(graph[xx][yy]=='a')
    29         {
    30             if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+1)
    31             dp[xx][yy]=dp[x][y]+1;
    32             return;
    33         }
    34         else if(graph[xx][yy]=='.')
    35         {
    36             if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+1)
    37             dp[xx][yy]=dp[x][y]+1;
    38             else continue;
    39         }
    40         visit[xx][yy]=false;
    41         dfs(xx,yy);
    42         visit[xx][yy]=true;
    43     }
    44 }
    45 
    46 int main()
    47 {
    48 //    freopen("in.txt","r",stdin);
    49     int i,j;
    50     while(scanf("%d%d",&m,&n)!=EOF)
    51     {
    52         ex=ey=-1;
    53         memset(visit,1,sizeof(visit));
    54         for(i=0;i<m;i++)
    55         scanf("%s",graph[i]);
    56         for(i=0;i<m;i++)
    57         for(j=0;j<n;j++)
    58         {
    59             dp[i][j]=-1;
    60             if(graph[i][j]=='r')
    61             sx=i,sy=j;
    62             else if(graph[i][j]=='a')
    63             dp[i][j]=inf,ex=i,ey=j;
    64             else continue;
    65         }
    66         if(ex==-1&&ey==-1)
    67         {
    68             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    69             continue;
    70         }
    71         dp[sx][sy]=0;
    72         dfs(sx,sy);
    73         if(dp[ex][ey]!=inf)
    74         printf("%d
    ",dp[ex][ey]);
    75         else
    76         printf("Poor ANGEL has to stay in the prison all his life.
    ");
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    spring注解事务管理
    Spring切入点表达式常用写法
    JPA和事务管理
    maven仓库
    struts2拦截器
    js中 转义字符
    jquery中变量加$和不加$有什么区别!
    spring大乱炖
    第一章 java多线程
    CS:APP 05 笔记
  • 原文地址:https://www.cnblogs.com/homura/p/4668723.html
Copyright © 2011-2022 走看看