hdu1242 Rescue DFS(路径探索题)
生活随笔
收集整理的這篇文章主要介紹了
hdu1242 Rescue DFS(路径探索题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
hdu1242 Rescue DFS(路徑探索題)
Total Submission(s): 15884????Accepted Submission(s): 5759
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
這里我定義的路徑探索題指 找某路能夠到達目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走過的狀態都還原掉
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15884????Accepted Submission(s): 5759
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
大意:天使 a被關 ?朋友r(可以有多個)要去救天使 r要繞過#(障礙物)和x(守衛)去尋找天使a 不能穿過障礙物 要走守衛那一步必須打敗守衛 時間為2,其他每步時間為1;
問找到天使的最短時間;
思路:用搜索做,由于r可以有多個,所以從天使a出發,每次都有四個方向可以走 用visit[x][y]記錄該點是否被走過,如往一個方向走但是另一個點被visit過就不會重復走了
往其中一個方向走后繼續進行dfs直到找到r或者無路可走 注意由于是探索性的走,走完進行DFS后記得往后退,狀態還原;
代碼如下:
#include<iostream> using namespace std; #include<string.h> #define max 205 char map[max][max]; long a[100000],step,sum,n,m,visited[max][max]; long directions[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//DFS 不用隊列,不用結構體 /**7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........ output<<13*/ void DFS(int x,int y) {int i,mx,my;if(map[x][y]=='r')a[sum++]=step;else if(map[x][y]!='#'){for(i=0;i<4;i++){mx=x+directions[i][0];my=y+directions[i][1];if(map[mx][my]!='#'&&mx>=1&&mx<=n&&my>=1&&my<=m&&!visited[mx][my])//不是墻并且沒走過{if(map[x][y]=='x')step++;step++;visited[mx][my]=1;DFS(mx,my); //所以關鍵要得到值的是遞歸的這一步 推導的時候讓這個的下一個DFS就是到達朋友那點比較好理解為什么后面要還原visited[mx][my]=0;//這一步的原因,上面DFS進行完后要將狀態還原step--; //下面這些都要還原if(map[x][y]=='x')step--;}}} }int main() {long i,j,x,y,min;while(cin>>n>>m){memset(visited,0,sizeof(visited));sum=0;step=0;min=max;for(i=1;i<=n;i++){for(j=1;j<=m;j++){cin>>map[i][j];if(map[i][j]=='a')//記錄天使的地址{x=i;y=j;}}}visited[x][y]=1;DFS(x,y);if(sum==0)cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;else{for(i=0;i<sum;i++)if(a[i]<min)min=a[i];cout<<min<<endl;}}return 0; }版權聲明:本文為博主原創文章,未經博主允許不得轉載。
posted on 2014-07-29 21:23 france 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/france/p/4808704.html
總結
以上是生活随笔為你收集整理的hdu1242 Rescue DFS(路径探索题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu下使用openocd+jli
- 下一篇: 数据结构基础(16) --树与二叉树