程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1242 Rescue

HDU 1242 Rescue

編輯:C++入門知識

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10194    Accepted Submission(s): 3728     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     Author CHEN, Xue     Source ZOJ Monthly, October 2003     Recommend Eddy 解題思路:記憶化的BFS,利用一個數組來維護到固定點的最小值,但是這一題有一個坑,就是主人公的朋友可能有多個,這樣我們必須從主人公出發去尋找他到所有朋友處的最小值,就是答案。 [cpp]   #include<iostream>   #include<cstring>   #include<queue>   using namespace std;   #define INF 99999   char maze[205][205];   long len[205][205];   int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//R,L,D,U   void BFS(int xa,int ya)   {       int x0,y0,j,p,q;       queue<int> x;queue<int> y;       x.push(xa);y.push(ya);       while(!x.empty()&&!y.empty())       {           x0=x.front();y0=y.front();           x.pop();y.pop();           for(j=0;j<4;j++)           {               //以下帶有一種剪枝,即只在有更短路徑的時候將這一路徑加入隊列進行搜索               p=x0+mov[j][0];q=y0+mov[j][1];               if(maze[p][q]=='.'||maze[p][q]=='r'||maze[p][q]=='x')               {                   if(maze[p][q]=='x')//直接干掉看守比繞路更快或者一出去就碰到看守則將其干掉                   {                       if(len[x0][y0]+2<len[p][q]||len[p][q]==0)                       {                           len[p][q]=len[x0][y0]+2;                           x.push(p);y.push(q);                       }                   }                   else if(len[x0][y0]+1<len[p][q]||len[p][q]==0)//如果搜索到的這一點的原先路徑比從搜索點走更遠,或者該點尚未搜索到,就更改到這一點的最短距離                   {                       len[p][q]=len[x0][y0]+1;                       x.push(p);y.push(q);                   }               }           }       }   }   int main()   {       int m,n,i,j,a,b;       long ans;       while(cin>>m>>n)       {           ans=INF;           memset(maze,'*',sizeof(maze));           memset(len,0,sizeof(len));           for(i=1;i<=m;i++)               for(j=1;j<=n;j++)               {                   cin>>maze[i][j];                   if(maze[i][j]=='a')                   {                       a=i;b=j;                   }               }           BFS(a,b);           for(i=1;i<=m;i++)               for(j=1;j<=n;j++)  www.2cto.com                 if(maze[i][j]=='r'&&ans>len[i][j]&&len[i][j])//len為0說明走不到這一點                       ans=len[i][j];           if(ans==INF)//ans未能更新說明主人公碰不到他的朋友(們)               cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;           else               cout<<ans<<endl;       }       return 0;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved