【记忆化DFS】HDOJ1242 Rescue

17 篇文章 0 订阅
1 篇文章 0 订阅

【题目】

Rescue

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


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
 

Recommend
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值