【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)

题干:

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

解题报告:

    其实记录路径的方法有很多,这里用的两个二维数组是其中一种,然而用栈表示其实也是大材小用了,只要dfs的时候多传两个参数就可以解决了 。还有一种方法就是定义一个结构体,并且可以顺便结构体中int x,y;char c;(或者int c)  这样可以顺便把这个地方的字符是‘ . ’  还是0-9中的数字 可以表示出来了。

    其实第一遍写还是很容易错的。但是比较良心的就是,特殊情况样例都给的比较清楚了,比如在(n,m)那个点有怪物的情况。这样我们依据样例就可以修正代码了。

AC代码:(这代码我保证我都不敢再看第二遍了。。。)

#include<bits/stdc++.h>using namespace std;
int n,m,curtime;
bool vis[105][105];
char maze[105][105];
int pathx[105][105];
int pathy[105][105];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
stack<int> xx,yy;
struct Node {int x,y;int time;Node(){}Node(int x,int y,int time):x(x),y(y),time(time){}bool operator < (const Node b) const{return time > b.time;} 
};
int bfs(int sx,int sy) {memset(vis,0,sizeof vis);priority_queue<Node> pq;pq.push(Node(sx,sy,0));while(!pq.empty()) {Node cur = pq.top();pq.pop();int tx,ty;if(cur.x == n && cur.y == m) return cur.time;for(int k = 0; k<4; k++) {tx = cur.x + nx[k];ty = cur.y + ny[k];if(tx < 1 || tx > n || ty < 1 || ty > m) continue;if(maze[tx][ty] == 'X' || vis[tx][ty] == 1) continue;vis[tx][ty]=1;if(maze[tx][ty] == '.') {pq.push(Node(tx,ty,cur.time + 1));pathx[tx][ty] = cur.x;pathy[tx][ty] = cur.y;}else {pq.push(Node(tx,ty,cur.time + 1 + maze[tx][ty] - '0'));pathx[tx][ty] = cur.x;pathy[tx][ty] = cur.y;}}}return -1;
}
void dfs(int x,int y) {if(x == 1 && y == 1) {printf("%ds:(0,0)->(%d,%d)\n",++curtime,xx.top()-1,yy.top()-1);xx.pop();yy.pop();return ;}dfs(pathx[x][y],pathy[x][y]);if(x == n && y == m) {if(maze[x][y]!='.'){int all = maze[x][y] - '0';while(all--)printf("%ds:FIGHT AT (%d,%d)\n",++curtime,x-1,y-1);}return ;}if(maze[x][y] == '.') {printf("%ds:(%d,%d)->(%d,%d)\n",++curtime,x-1,y-1,xx.top()-1,yy.top()-1);xx.pop();yy.pop();}else {int all = maze[x][y] - '0';while(all--)printf("%ds:FIGHT AT (%d,%d)\n",++curtime,x-1,y-1);printf("%ds:(%d,%d)->(%d,%d)\n",++curtime,x-1,y-1,xx.top()-1,yy.top()-1);xx.pop();yy.pop();}
}int main()
{while(cin>>n>>m) {while(xx.size()) xx.pop();while(yy.size()) yy.pop();for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}int ans = bfs(1,1);if(ans == -1) {printf("God please help our poor hero.\nFINISH\n");continue;}printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);curtime = 0;int i=n,j=m;while(i!=1 || j!=1) {//不能是&& !!!或者 while(!(i==1&&j==1)) xx.push(i);yy.push(j);//所以path用结构体存多好。。。 int tmpx = pathx[i][j];int tmpy = pathy[i][j];i=tmpx,j=tmpy;}
//		while(xx.size()){
//			printf("%d ",xx.top());xx.pop();
//		}
//		printf("\n");
//		while(yy.size()){
//			printf("%d ",yy.top());yy.pop();
//		}dfs(n,m);printf("FINISH\n");}return 0 ;
}
//23:31 - 24:10

总结:

  写记录路径的时候的几个错误点:

   第一:刚开始没有写56行左右的那个if(x==n && y==m)     后果:后来发现如果不写这个,也进入下面的printf,那么就会栈空的情况下xx.top(),肯定是会炸的。

   第二 :刚开始改的时候直接if(x==n && y==m)  return;   但是发现第二个样例不对,因为我的curtime还没到我的ans,所以我还需要判断是否在x==n && y==m这一点打架了,即这一点是否有怪物。而且还不能直接让他进入下面的else,因为我并不想让他输出printf("%ds:(%d,%d)->(%d,%d)\n",++curtime,x-1,y-1,xx.top()-1,yy.top()-1);这一句。所以无奈只能在if(x==n && y==m)  return; 这里面做判断输出、

其实还有一种办法就是把FIGNT那个输出跟在“->”那种输出的后面,而不是先“->”输出这个 ,再输出FIGNT那个。类似于这种处理

int flag=bfs();if(flag!=-1){printf("It takes %d seconds to reach the target position, let me show you the way.\n",flag);sec=1,x=y=0;while(sec!=flag+1){printf("%ds:(%d,%d)->(%d,%d)\n",sec++,x,y,map[x][y].nx,map[x][y].ny);for(i=0;i<fight[map[x][y].nx][map[x][y].ny];i++)printf("%ds:FIGHT AT (%d,%d)\n",sec++,map[x][y].nx,map[x][y].ny);tx=map[x][y].nx;ty=map[x][y].ny;x=tx;y=ty;}    }   elseprintf("God please help our poor hero.\n");printf("FINISH\n");

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441697.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php多线程模拟请求,浅谈php使用curl模拟多线程发送请求

每个PHP文件的执行是单线程的&#xff0c;但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程&#xff0c;这里用curl模拟多线程发送请求。php的curl多线程是通过不断调用curl_multi_exec来获取内容&#xff0c;这里举一个demo来模拟一次curl多线程并发操作。//设…

【HDU - 1455】Sticks (dfs + 剪枝)

题干&#xff1a; George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original…

java找不到符号类socket,编译报错+解决方法:错误: 找不到符号

public class ServerPlainTest { // 包内包外可见public static void main(String[] args) {try {ServerSocket ss new ServerSocket(8189);System.out.println("the server has startuped, waiting for connections.");while (true) { // accept multiple clients …

php hbase thrift,PHP使用Thrift操作Hbase

系统架构图HBase 启动 Thrift服务hbase启动thrift服务// 进入安装的hbase bin目录下// 执行hbase-daemon.sh start thrift2需要注意的是&#xff0c;这里启动的是thrift2服务&#xff0c;如果需要启动thrift服务只需要将thrift2改为thrift就可以了&#xff0c;具体thrift和thri…

【CodeForces - 761D 】Dasha and Very Difficult Problem (构造,思维)

题干&#xff1a; Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci  bi -…

php excel下载打不开了,php下载excel无法打开的解决方法

php下载excel文件,1、在下载的过程中不要 输出任何非文件信息&#xff0c;比如 echo log信息。 否则下载后的文件无法打开&#xff0c;提示格式错误或者文件被破坏。2、 输出的excel格式一定要和后缀名保存一直&#xff0c;否也会提示格式错误或者文件被破坏if (file_exists(CA…

【CodeForces - 761B】Dasha and friends (思维,模拟,构造)

题干&#xff1a; Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation: The track is the circle with length L, in distinct points of which there…

php字符串变量,PHP 字符串变量

PHP 字符串变量字符串变量用于存储并处理文本。PHP 中的字符串变量字符串变量用于包含有字符的值。在创建字符串之后&#xff0c;我们就可以对它进行操作了。您可以直接在函数中使用字符串&#xff0c;或者把它存储在变量中。在下面的实例中&#xff0c;我们创建一个名为 txt 的…

【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)

题干&#xff1a; After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements: There is at least one digit in the string,There is a…

php4和php5的区别,什么是PHP 4和PHP 5之间的区别是什么-php是什么文件

&#xff1f;尽管PHP 5是故意设计成兼容尽可能与以前的版本&#xff0c;也有一些显著的变化。 其中的一些变化包括&#xff1a; 一个新的OOP模型基础上&#xff0c;Zend引擎2.0 改进MySQL支持的一个新推广 内置SQLite的原生支持 一个新的错误报告不断&#xff0c; E_STRICT &am…

【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)

题干&#xff1a; Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farme…

php 向公众号发送消息,微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。即客户要求&#xff0c;他在后台编辑好文章后要主动给每个用户都发送消息&#xff0c;并可以让用户点击直接进入文章页面。于是乎&#xff0c;当时脑子一热&#xff0c;想着没什么大的问题&#xff0c;so easy。模板消息不就得了。…

【CodeForces - 764A】Taymyr is calling you (找规律,水题)

题干&#xff1a; Comrade Dujikov is busy choosing artists for Timofeys birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, …

ecshop php升级,升级-安装与升级- ECShop帮助

ECShop V2.6.2版本有GBK和UFT-8两种编码格式的程序&#xff0c;而低于ECShop V2.6.0 的版本只有UTF-8 编码的(这些版本包括 ECShop 2.5.1、ECShop 2.5.0、ECShop 2.1.5、ECShop 2.1.2b等)&#xff0c; 这些版本升级到ECShop V2.6.2均可以使用本程序升级。相同版本的升级只需要覆…

【CodeForces - 764B 】Timofey and cubes (模拟)

题干&#xff1a; Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Tim…

php页面转发,php如何实现页面路由转发

php实现页面路由转发的方法&#xff1a;首先配置nginx服务器&#xff0c;在【.htaccess】中写上nginx的语法&#xff1b;然后打开根目录的【index.php】&#xff0c;编写文件路由即可。php实现页面路由转发的方法&#xff1a;1、配置nginx服务器nginx服务器不会自动读取.htacce…

【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

题干&#xff1a; One of Timofeys birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, bu…

php整站防注入程序,一个不错的php通用防注入程序

代码如下:function jk1986_checksql(){$bad_str "and|select|update||delete|insert|*";$bad_Array explode("|",$bad_str);/** 过滤Get参数 **/foreach ($bad_Array as $bad_a){foreach ($_GET as $g){if (substr_count(strtolower($g),$bad_a) > 0)…

【HDU - 5056】Boring count (尺取法)

题干&#xff1a; You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input In the first line there is an integer T , ind…

java图片上传被旋转,在其他大牛那看到的java手机图片上传旋转问题的解决方法...

// 将图片文件转化为字节数组字符串&#xff0c;并对其进行Base64编码处理//Base64所用包&#xff1a;org.apache.commons.codec.binary.Base64public static String encodeImgageToBase64(File imageFile) {byte[] data null;// 读取图片字节数组try {InputStream in new Fi…