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): 18847 Accepted Submission(s): 6090
Special Judge
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.
/* Name: hdu--1026--Ignatius and the Princess I Copyright: ©2017 日天大帝 Author: 日天大帝 Date: 21/04/17 17:30 Description: bfs搜索路径,dfs打印路径 */ #include<cstring> #include<queue> #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; struct node{ int x,y,steps; bool operator<(const node&a)const{ return steps>a.steps; } }; int bfs(); void dfs(int,int); ; int map[MAX][MAX]; int to[MAX][MAX]; int cnt[MAX][MAX]; int n,m,ct; ][] = {{,},{,-},{,},{-,}}; int main(){ // freopen("in.txt","r",stdin); ios::sync_with_stdio(false); while(cin>>n>>m){ memset(map,,sizeof(map)); memset(to,,sizeof(to)); memset(cnt,,sizeof(cnt)); ; i<n; ++i){ ; j<m; ++j){ char ch;cin>>ch; if(ch == '.'){ map[i][j] = ; }; '; } } int ans = bfs(); if(ans){ cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl; ct = ; dfs(n-,m-); }else printf("God please help our poor hero.\n"); cout<<"FINISH\n";//G++WA无数次,hdu C++ AC了 } ; } void dfs(int x,int y){ if(!to[x][y])return ; int i,j; i = x - dir[to[x][y]-][];//剪枝 j = y - dir[to[x][y]-][];//剪枝 dfs(i,j); printf("%ds:(%d,%d)->(%d,%d)\n",ct++,i,j,x,y); while(cnt[x][y]--) { printf("%ds:FIGHT AT (%d,%d)\n",ct++,x,y); } } int bfs(){ node start; start.x = ; start.y = ; start.steps = ; map[][] = -; priority_queue<node> q; q.push(start); while(!q.empty()){ node a,temp = q.top();q.pop(); && temp.y == m-)return temp.steps; ; i<; ++i){//i<4(n) a.x = temp.x + dir[i][]; a.y = temp.y + dir[i][]; ||a.y< || map[a.x][a.y] == -)continue; a.steps = temp.steps + map[a.x][a.y] + ; map[a.x][a.y] = -; to[a.x][a.y] = i+;//剪枝 q.push(a); } } ; }
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 ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
随机推荐
- test_markdown
add modifications 非科学计数法显示数字 citation[^ref1] format bank% do not use scientific expression format lo ...
- 关于ArcGIS Android的在x86和x64系统中兼容性的问题与解决方案
我们都知道,在配置ArcGIS Android SDK时,需要在jniLibs目录下放置三个文件夹,分别是armeabi.armeabi-v7a.x86三个文件夹,ArcGIS Android针对目标 ...
- C#实现手机发送验证码
之前不怎么了解这个,一直以为做起来很复杂. 直到前两天公司要求要做这个功能. 做了之后才发现 这不过就是一个POST请求就能实现的东西.现在给大家分享一下,有不足之处还请多多指教. 废话不多说 直接上 ...
- cURL的运用,文字替换
<?php /** * 实例描述:在网络上下载一个网页并把内容中的“百度”替换为“谷歌”之后输出 */ $curl_obj = curl_init(); // 初始化 curl_setopt($ ...
- 7.如何发布vue项目到服务器
1.确保程序是可运行的,即npm run dev可以运行 2.把index.js修改 3.运行npm命令npm run build 4.生成的dist文件为 直接点击index.html就能运行,部署 ...
- 把本地git仓库中的项目引入到码云上
一.安装git软件和TortoiseGit客户端(git需配置环境变量,但安装时已经配置好,无需考虑) 二.生成公钥和私钥(建立与码云的连接) 三.在码云上新建项目(建议在组织的基础上) 四.在码 ...
- 正确、安全地停止SpringBoot应用服务
引言 Spring Boot,作为Spring框架对"约定优先于配置(Convention Over Configuration)"理念的最佳实践的产物,它能帮助我们很快捷的创建出 ...
- Java自学手记——servlet3.0新特性
servlet3.0出来已经很久了,但市场上尚未普遍应用,servlet3.0有三个比较重要的新特性:使用注解来代替配置文件,异步处理以及上传组件支持. 支持servlet3.0的要求:MyEclip ...
- 使用C#创建简单的WCF服务
一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Information Services(II ...
- 常见MD5加密解密值及免费解密网站
常用的MD5解密 MD5(admin,16) = 7a57a5a743 MD5(admin,16) = 7a57a5a743894a0e MD5(admin888,16) = 469e ...