您的位置:首页 > 产品设计 > UI/UE

【记忆化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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM HDOJ DFS