现在的位置: 首页 > 综合 > 正文

2013 – ECJTU 暑期训练赛第三场-problem-J

2018年05月02日 ⁄ 综合 ⁄ 共 4626字 ⁄ 字号 评论关闭
J -
J

Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
这道题是最短路问题:思路—>优先队列+BFS(广度深搜)
AC代码:
#include<iostream> #include<queue> #include<stack> const int MAX=101; int fangxiang[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; using namespace std; typedef struct dian { int i; int j; int time; }; bool operator <(const dian &a,const dian &b)//优先队列按时间从小到大排列 { return a.time>b.time?true:false; } typedef struct point { int i; int j; }; char map[MAX][MAX];//记录地图上每个节点 bool mark[MAX][MAX];//看节点是否被标记,即是否已搜过 point result[MAX][MAX];//记录达到某一点的前一个位置,通过回溯找到路径  int n; int m; int BFS() { int i,j; priority_queue<dian>que;//优先队列 dian start; start.i=0; start.j=0; start.time=0; point dian_before;
dian_before.i=-1;//把起点即(0,0)之前的点记为(-1,-1) dian_before.j=-1; result[0][0]=dian_before;//赋给result[0][0]; mark[0][0]=true; que.push(start); while(!que.empty())//开始搜索 { dian dian_now=que.top(); que.pop(); for(i=0;i<4;i++) { int next_i,next_j; next_i=dian_now.i+fangxiang[i][0]; next_j=dian_now.j+fangxiang[i][1]; if(next_i>=0&&next_i<n&&next_j<m&&next_j>=0&&!mark[next_i][next_j]&&map[next_i][next_j]!='X') { dian dian_next; dian_next.i=next_i; dian_next.j=next_j; if(map[next_i][next_j]=='.') dian_next.time=dian_now.time+1; else dian_next.time=dian_now.time+map[next_i][next_j]-'0'+1; mark[next_i][next_j]=true; point p; p.i=dian_now.i; p.j=dian_now.j; result[next_i][next_j]=p; if(next_i==n-1&&next_j==m-1) return dian_next.time; que.push(dian_next); } } } return -1; } int main() { int i,j,time,t; while(cin>>n>>m) { for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>map[i][j]; mark[i][j]=false; } } t=BFS(); if(t==-1) { cout<<"God please help our poor hero."<<endl; cout<<"FINISH"<<endl; } else { cout<<"It takes "<<t<<" seconds to reach the target position, let me show you the way."<<endl; stack<point>que; point p; p.i=n-1; p.j=m-1; que.push(p);//把终点先入栈,输出时就是从起点开始输出 while(result[p.i][p.j].i!=-1&&result[p.i][p.j].j!=-1)//依次入栈 { que.push(result[p.i][p.j]); p=result[p.i][p.j]; } time=1; p=que.top(); que.pop(); while(!que.empty()) { cout<<time<<"s:("<<p.i<<","<<p.j<<")->("<<que.top().i<<","<<que.top().j<<")"<<endl; time+=1; if(map[que.top().i][que.top().j]!='.') { int tt; tt=map[que.top().i][que.top().j]-'0'; for(j=0;j<tt;j++) { cout<<time<<"s:FIGHT AT ("<<que.top().i<<","<<que.top().j<<")"<<endl; time+=1; } } p=que.top(); que.pop(); } cout<<"FINISH"<<endl; } } return 0; }

抱歉!评论已关闭.