练习二1012
2016-04-20 23:34
309 查看
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 22
[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.<br><br>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.<br><br>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.)<br>
[align=left]Input[/align]
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the
file.<br>
[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." <br>
[align=left]Sample Input[/align]
7 8<br>#.#####.<br>#.a#..r.<br>#..#x...<br>..#..#.#<br>#...##..<br>.#......<br>........<br>
[align=left]Sample Output[/align]
13<br>
[align=left]Author[/align]
CHEN, Xue
[align=left]Source[/align]
ZOJ Monthly, October 2003
Statistic |
Submit |
题意:
在一张地图中,a被困住,r去救a,r不止一个,x代表敌人,r每走一步用时为1,杀掉敌人要格外加1,问最短时间。
思路:
是一个优先队列+bfs,以时间为标准优先,搜索时可以从a开始去搜索r,因为a只有一个,r有多个。没看答案,自己写了好多遍终于过了。注意下面写的注释。
代码:
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}},n,m,sx,sy;
char map[201][201];
bool vis[201][201];
bool inmap(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m)
return true;
return false;
}
struct point
{
int x,y,tim;
friend bool operator<(point a,point b)
{
return a.tim>b.tim;
}
}p1,p2;
int bfs(int a,int b,int c)
{
p1.x=a;
p1.y=b;
p1.tim=c;
priority_queue<point>q;
q.push(p1);
while(!q.empty())
{
p1=q.top();
q.pop();
if(map[p1.x][p1.y]=='r')
return p1.tim;
for(int i=0;i<4;i++)
{
int n_x=p1.x+dir[i][0];
int n_y=p1.y+dir[i][1];
if(!inmap(n_x,n_y)||vis[n_x][n_y]||map[n_x][n_y]=='#')
continue;
if(map[n_x][n_y]=='x')
{
vis[n_x][n_y]=1;
p2.x=n_x;
p2.y=n_y;
p2.tim=p1.tim+2;
q.push(p2);
}
if(map[n_x][n_y]=='r'||map[n_x][n_y]=='.'){
vis[n_x][n_y]=1;
p2.x=n_x;
p2.y=n_y;
p2.tim=p1.tim+1;
q.push(p2);
}
}
}
return -1;
}
int main()
{
while(cin>>n>>m)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>map[i][j];
}
}
for(int k=1;k<=n;k++){
for(int j=1;j<=m;j++){
if(map[k][j]=='a')
{sx=k;sy=j;}
}
}
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
int cnt=bfs(sx,sy,0); //有返回值的函数一定要注意第一次调用后再调用就不对了。(if(bfs(....)) cout<<bfs(....)...)就不
if(cnt==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; // 对。
else cout<<cnt<<endl;
}
return 0;
}
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 22
[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.<br><br>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.<br><br>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.)<br>
[align=left]Input[/align]
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the
file.<br>
[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." <br>
[align=left]Sample Input[/align]
7 8<br>#.#####.<br>#.a#..r.<br>#..#x...<br>..#..#.#<br>#...##..<br>.#......<br>........<br>
[align=left]Sample Output[/align]
13<br>
[align=left]Author[/align]
CHEN, Xue
[align=left]Source[/align]
ZOJ Monthly, October 2003
Statistic |
Submit |
题意:
在一张地图中,a被困住,r去救a,r不止一个,x代表敌人,r每走一步用时为1,杀掉敌人要格外加1,问最短时间。
思路:
是一个优先队列+bfs,以时间为标准优先,搜索时可以从a开始去搜索r,因为a只有一个,r有多个。没看答案,自己写了好多遍终于过了。注意下面写的注释。
代码:
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}},n,m,sx,sy;
char map[201][201];
bool vis[201][201];
bool inmap(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m)
return true;
return false;
}
struct point
{
int x,y,tim;
friend bool operator<(point a,point b)
{
return a.tim>b.tim;
}
}p1,p2;
int bfs(int a,int b,int c)
{
p1.x=a;
p1.y=b;
p1.tim=c;
priority_queue<point>q;
q.push(p1);
while(!q.empty())
{
p1=q.top();
q.pop();
if(map[p1.x][p1.y]=='r')
return p1.tim;
for(int i=0;i<4;i++)
{
int n_x=p1.x+dir[i][0];
int n_y=p1.y+dir[i][1];
if(!inmap(n_x,n_y)||vis[n_x][n_y]||map[n_x][n_y]=='#')
continue;
if(map[n_x][n_y]=='x')
{
vis[n_x][n_y]=1;
p2.x=n_x;
p2.y=n_y;
p2.tim=p1.tim+2;
q.push(p2);
}
if(map[n_x][n_y]=='r'||map[n_x][n_y]=='.'){
vis[n_x][n_y]=1;
p2.x=n_x;
p2.y=n_y;
p2.tim=p1.tim+1;
q.push(p2);
}
}
}
return -1;
}
int main()
{
while(cin>>n>>m)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>map[i][j];
}
}
for(int k=1;k<=n;k++){
for(int j=1;j<=m;j++){
if(map[k][j]=='a')
{sx=k;sy=j;}
}
}
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
int cnt=bfs(sx,sy,0); //有返回值的函数一定要注意第一次调用后再调用就不对了。(if(bfs(....)) cout<<bfs(....)...)就不
if(cnt==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; // 对。
else cout<<cnt<<endl;
}
return 0;
}
相关文章推荐
- 【编程练习】最近准备开始找工作,这篇文章作为一个code练手题目的总结吧
- 找规律类博弈(4)
- java中volatile关键字的含义
- 【编程练习】最近准备开始找工作,这篇文章作为一个code练手题目的总结吧
- linux上nginx的安装启动以及配合php-fpm的使用
- fragment简单有用的一些细节
- fzu 1896 神奇的魔法数字(数位dp)
- 专题二 第四道题
- Assembly——一个简单的程序
- 搭建SpringMVC+MyBatis开发框架六
- HTML5新特性之WebRTC
- 1.spark概述
- Spring AOP概述
- 华为机试注意事项
- 24.Creating Customer Groups
- 用java写程序打印出100到200之间所有的素数
- rdesktop使用参数详解(翻译)及举例
- mapreduce(一)
- 用css控制段落
- 全局获取Context