【BFS】HDU1026Ignatius and the Princess I
2016-05-09 15:20
225 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026
Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional
array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here
is some rules:
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated
by the end of file. More details in the Sample Input.
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero
the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
Sample Input
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char Map[150][150];
int vis[150][150];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct NODE
{
int x,y;
}NODE;
struct node
{
int s;
NODE now;
NODE pre;
friend bool operator <(node a,node b)
{
return a.s>b.s;
}
}a[150][150];
void init()
{
memset(vis,0,sizeof(vis));
}
bool check(int x,int y)
{
return !vis[x][y]&&x>=0&&y>=0&&x<n&&y<m&&(Map[x][y]!='X');
}
void print(node st)
{
cout<<"It takes "<<st.s<<" seconds to reach the target position, let me show you the way."<<endl;
NODE route[10050];
route[0]=st.now;
int cnt=1;
while(st.now.x!=0||st.now.y!=0){
st=a[st.pre.x][st.pre.y];
route[cnt++]=st.now;
}
int t=1;
for(int i=cnt-1;i>0;i--){
if(Map[route[i].x][route[i].y]!='.'){
char tt=Map[route[i].x][route[i].y];
while(tt!='0'){
cout<<t++<<"s:FIGHT AT ("<<route[i].x<<','<<route[i].y<<")"<<endl;
tt--;
}
}
cout<<t++<<"s:("<<route[i].x<<','<<route[i].y<<")->("<<route[i-1].x<<','<<route[i-1].y<<')'<<endl;
}
if(Map[route[0].x][route[0].y]!='.'){
char tt=Map[route[0].x][route[0].y];
while(tt!='0'){
cout<<t++<<"s:FIGHT AT ("<<route[0].x<<','<<route[0].y<<")"<<endl;
tt--;
}
}
return;
}
void bfs()
{
priority_queue<node>q;
node st;
st.s=0;
st.now.x=0;
st.now.y=0;
st.pre.x=0;
st.pre.y=0;
a[0][0]=st;
vis[0][0]=1;
q.push(st);
while(!q.empty()){
st=q.top();
q.pop();
if(st.now.x==n-1&&st.now.y==m-1){
print(st);
//cout<<st.s<<endl;
return;
}
for(int i=0;i<4;i++){
node nt=st;
nt.pre.x=st.now.x;
nt.pre.y=st.now.y;
nt.now.x+=dir[i][0];
nt.now.y+=dir[i][1];
if(check(nt.now.x,nt.now.y)){
vis[nt.now.x][nt.now.y]=1;
a[nt.now.x][nt.now.y]=nt;
int t=1;
if(Map[nt.now.x][nt.now.y]!='.'){
t+=Map[nt.now.x][nt.now.y]-'0';
}
nt.s+=t;
q.push(nt);
}
}
}
cout<<"God please help our poor hero."<<endl;
}
int main()
{
cin.sync_with_stdio(false);
while(cin>>n>>m){
init();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>Map[i][j];
}
}
bfs();
cout<<"FINISH"<<endl;
}
return 0;
}
Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional
array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here
is some rules:
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated
by the end of file. More details in the Sample Input.
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero
the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
Sample Input
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char Map[150][150];
int vis[150][150];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct NODE
{
int x,y;
}NODE;
struct node
{
int s;
NODE now;
NODE pre;
friend bool operator <(node a,node b)
{
return a.s>b.s;
}
}a[150][150];
void init()
{
memset(vis,0,sizeof(vis));
}
bool check(int x,int y)
{
return !vis[x][y]&&x>=0&&y>=0&&x<n&&y<m&&(Map[x][y]!='X');
}
void print(node st)
{
cout<<"It takes "<<st.s<<" seconds to reach the target position, let me show you the way."<<endl;
NODE route[10050];
route[0]=st.now;
int cnt=1;
while(st.now.x!=0||st.now.y!=0){
st=a[st.pre.x][st.pre.y];
route[cnt++]=st.now;
}
int t=1;
for(int i=cnt-1;i>0;i--){
if(Map[route[i].x][route[i].y]!='.'){
char tt=Map[route[i].x][route[i].y];
while(tt!='0'){
cout<<t++<<"s:FIGHT AT ("<<route[i].x<<','<<route[i].y<<")"<<endl;
tt--;
}
}
cout<<t++<<"s:("<<route[i].x<<','<<route[i].y<<")->("<<route[i-1].x<<','<<route[i-1].y<<')'<<endl;
}
if(Map[route[0].x][route[0].y]!='.'){
char tt=Map[route[0].x][route[0].y];
while(tt!='0'){
cout<<t++<<"s:FIGHT AT ("<<route[0].x<<','<<route[0].y<<")"<<endl;
tt--;
}
}
return;
}
void bfs()
{
priority_queue<node>q;
node st;
st.s=0;
st.now.x=0;
st.now.y=0;
st.pre.x=0;
st.pre.y=0;
a[0][0]=st;
vis[0][0]=1;
q.push(st);
while(!q.empty()){
st=q.top();
q.pop();
if(st.now.x==n-1&&st.now.y==m-1){
print(st);
//cout<<st.s<<endl;
return;
}
for(int i=0;i<4;i++){
node nt=st;
nt.pre.x=st.now.x;
nt.pre.y=st.now.y;
nt.now.x+=dir[i][0];
nt.now.y+=dir[i][1];
if(check(nt.now.x,nt.now.y)){
vis[nt.now.x][nt.now.y]=1;
a[nt.now.x][nt.now.y]=nt;
int t=1;
if(Map[nt.now.x][nt.now.y]!='.'){
t+=Map[nt.now.x][nt.now.y]-'0';
}
nt.s+=t;
q.push(nt);
}
}
}
cout<<"God please help our poor hero."<<endl;
}
int main()
{
cin.sync_with_stdio(false);
while(cin>>n>>m){
init();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>Map[i][j];
}
}
bfs();
cout<<"FINISH"<<endl;
}
return 0;
}
相关文章推荐
- 6-15位字母加数字密码验证
- 2.1.常用位操作符
- Quartz2D(4)
- C#从非第一元素传递实参数组
- Mybatis github 资源记录
- LinkedList源码解析
- oracle 数据库,无效的SCN号错误
- C++程序设计语言练习4.2 茴字的几种写法
- Android 搜索到的关键字改变颜色
- 项目中导入导出两个关联的库
- thinkphp3.2自定义success及error跳转页面
- 设计模式读书笔记-----中介者模式
- hibernate+quartz,oracle报错ora-12520
- Could NOT find OpenMP (missing: OpenMP_CXX_FLAGS)
- jQuery动画
- Android动画效果
- cocos2dx lua 动画
- iOS极光推送 点击推送消息跳转页面
- Android string %1$s %1$d 通配字符串例子
- 预备知识