hdu---------(1026)Ignatius and the Princess I(bfs+dfs)
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12124 Accepted Submission(s): 3824
Special Judge
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 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.
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.
.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.
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
/*Problem : 1026 ( Ignatius and the Princess I ) Judge Status : Accepted
RunId : 11510650 Language : G++ Author : huifeidmeng
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/ #include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<iostream>
using namespace std;
const int maxn=;
struct node{
int x,y;
};
struct sode{
int val;
int sum;
node pre; //他一个点
void setint(int x,int y){
pre.x=x;
pre.y=y;
}
};
int n,m,cont;
sode map[maxn][maxn];
int dir[][]={{,},{-,},{,},{,-}};
void bfs(){
queue<node> anc;
node tem={,};
anc.push(tem);
while(!anc.empty()){
node sav=anc.front();
anc.pop();
for(int i=;i<;i++){
tem=(node){dir[i][]+sav.x,dir[i][]+sav.y};
if(tem.x>=&&tem.x<n&&tem.y>=&&tem.y<m&&map[tem.x][tem.y].val!=-){
if(map[tem.x][tem.y].sum==
||map[tem.x][tem.y].sum>map[sav.x][sav.y].sum+map[tem.x][tem.y].val+)
{
map[tem.x][tem.y].sum=map[sav.x][sav.y].sum+map[tem.x][tem.y].val+;
map[tem.x][tem.y].setint(sav.x,sav.y);
anc.push(tem);
}
}
}
}
}
void dfs(sode a,node cur){
if(cur.x==&&cur.y==){
cont=;
while(map[cur.x][cur.y].val-->)
printf("%ds:FIGHT AT (%d,%d)\n",cont++,cur.x,cur.y);
// printf("%ds:(%d,%d)->(%d,%d)\n",cont++,a.pre.x,a.pre.y,cur.x,cur.y);
return;
}
dfs(map[a.pre.x][a.pre.y],a.pre);
printf("%ds:(%d,%d)->(%d,%d)\n",cont++,a.pre.x,a.pre.y,cur.x,cur.y);
if(a.val>){
while(a.val-->){
printf("%ds:FIGHT AT (%d,%d)\n",cont++,cur.x,cur.y);
}
}
}
int main(){
char ss[];
//freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%s",ss);
for(int j=;j<m;j++){
map[i][j].sum=;
map[i][j].setint(,);
if(ss[j]=='.') map[i][j].val=;
else if(ss[j]=='X')map[i][j].val=-;
else{
map[i][j].val= ss[j]-'';
if(i+j==)map[i][j].sum=map[i][j].val;
}
}
}
bfs();
if(map[n-][m-].pre.x==)
printf("God please help our poor hero.\n");
else {
printf("It takes %d seconds to reach the target position, let me show you the way.\n",map[n-][m-].sum);
dfs(map[n-][m-],(node){n-,m-});
}
puts("FINISH");
}
return ;
}
hdu---------(1026)Ignatius and the Princess I(bfs+dfs)的更多相关文章
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I(bfs)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- R: NULL, NA, and NaN
NaN (“Not a Number”) means 0/0 NA (“Not Available”) is generally interpreted as a missing value and ...
- 通过数据库和EasyUI的combobox级联实现省市区三级联动
1.新建一个web项目 2.因为这里用到了数据库所以我们在lib目录导入Hibernate的jar包.fastjson.jar包及数据库jar包 3.同样导入EasyUI的组件配置,并在新建的html ...
- 8.Methods(一)
1.Instance Constructors and Classes (Reference Types) Constructors methods : 1.allow an instance of ...
- [数据结构与算法]栈Stack的多种实现
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 线程入门之yield
package com.thread; /** * <yiedl:把cpu让给其他线程> * <功能详细描述> * * @author 95Yang */ public cla ...
- yii::app(); 怎么得到module、controller、action的名字
$module = Yii::app()->controller->module->id; $controller = Yii::app()->controller->i ...
- JavaScript基础知识之——Location 对象详解
属性 描述 location.hash 设置或取得 URL 中的锚 location.host 设置或取得 URL 中主机(包括端口号) location.hostname 设置或取得 URL 中的主 ...
- 使用phpmaill发送邮件的例子
首先,要下载php_mail软件包 核心代码: index.php <?php include "mail.php"; if (!empty($_POST['to']) &a ...
- surfaceview介绍
[1]surfaceview 控件是一个重量级控件 [2]内部维护了2个线程 A 获取数据 负责显示 B 负责显示 获取数据 [3]他可以直接在子线程更新ui ...
- JavaWeb学习计划
1 HTML2 CSS3 JavaScript4 XML5 Tomcat6 Servlet7 HTTP8 Cookie Session9 JSP10 JSTL11 MySQL12 JDBC13 过滤器 ...