【记忆化DFS】HDOJ1242 Rescue
2015-08-25 17:22
411 查看
【题目】
Rescue
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21820 Accepted Submission(s): 7774
[/b]
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
[align=left]Sample Output[/align]
13
[align=left]Author[/align]
CHEN, Xue
[align=left]Source[/align]
ZOJ Monthly, October 2003
[align=left]Recommend[/align]
Eddy | We have carefully selected several similar problems for you: 1240 1016 1241 1372 1175
Statistic | Submit | Discuss |Note
【思路】
一开始看数据范围不大,想直接爆搜,写了一个裸DFS,TLE,见优化无望,又不想改成BFS,上网搜了相关的记忆化来抄了一下。对记忆化的认识更深入。
特点是
1.主程对每一个位置都展开搜索。
2.dp数组保存当前位置最好的答案,并不断更新。
3.如果dp数组答案比当前好,那么剪枝,否则更新。
【代码】
#include <iostream> #include <iomanip> #include <cstring> using namespace std; #define INF 1e9 int n,m,dp[205][205],ex,ey,ans; char ma[205][205]; bool vis[205][205]; const int dx[4]={0,0,1,-1}; const int dy[4]={1,-1,0,0}; void dfs(int x,int y) { if(dp[x][y]>=dp[ex][ey]) return; for(int i=0;i<4;++i) { int xx=x+dx[i]; int yy=y+dy[i]; if(ma[xx][yy]=='#'||vis[xx][yy]) continue;//这里错写成break了调了半天T_T if(ma[xx][yy]=='x') { if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+2) dp[xx][yy]=dp[x][y]+2; else continue; } else if(ma[xx][yy]=='a') { if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+1) dp[xx][yy]=dp[x][y]+1; return; } else if(ma[xx][yy]=='.') { if(dp[xx][yy]==-1||dp[xx][yy]>dp[x][y]+1) dp[xx][yy]=dp[x][y]+1; else continue; } vis[xx][yy]=true; dfs(xx,yy); vis[xx][yy]=false; } } int main() { ios::sync_with_stdio(false); int sx,sy; while(cin>>n>>m) { memset(ma,'#',sizeof(ma)); memset(vis,0,sizeof(vis)); ex=ey=-1; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) { dp[i][j]=-1; cin>>ma[i][j]; if(ma[i][j]=='r') { sx=i; sy=j; } if(ma[i][j]=='a') { ex=i; ey=j; dp[i][j]=INF; } } if(ex==-01&&ey==-1) { cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; continue; } dp[sx][sy]=0; dfs(sx,sy); if(dp[ex][ey]!=INF) cout<<dp[ex][ey]<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } //system("pause"); return 0; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- 简单的四则运算
- 数的奇偶性
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- 矩阵的乘法操作
- C 语言实现迷宫 DFS算法
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置