博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1026
阅读量:4538 次
发布时间:2019-06-08

本文共 2131 字,大约阅读时间需要 7 分钟。

地址:

题意:n*m迷宫,求从(0,0)到(n-1,m-1)的最少时间。'X'是墙,'.'是空地,'1'-'9'表示有怪物,消灭之需要数字对应的时间。

mark:最近一直在写搜索题。搜索题有的很费劲,但是写多也就觉得嗯,就那么回事。这题也没啥可说的,直接BFS。和最简单的BFS不同的是,判断一个过去到过的点是否可走,需要多开一个数组来记录,然后判断过去到达的时间和现在到达的时间的大小。(其实最好的方法是优先队列么。。。懒得写而已。)然后因为要求路径,还要再开一个数组记录是从哪儿转移过来的就是了。路径输出的时候用递归~。

代码:

# include 
# include
int dp[110][110], prt[110][110] ; char graph[110][110] ; int q[110*110*20][2] ; int n , m ; int tab[4][2] = {
0,1,0,-1,1,0,-1,0} ; void bfs() {
int front = 0, rear = 1, parent ; int i, x, y, xx, yy, tt ; q[0][0] = 0, q[0][1] = 0 ; dp[0][0] = 0 ; while (front != rear) {
x = q[front][0], y = q[front][1] ; parent = front ; front ++ ; for (i = 0 ; i < 4 ; i++) {
xx = x + tab[i][0] ; yy = y + tab[i][1] ; if (xx < 0 || xx >= n) continue ; if (yy < 0 || yy >= m) continue ; if (graph[xx][yy] == 'X') continue ; tt = dp[x][y] + 1 ; if (graph[xx][yy] != '.') tt += graph[xx][yy]-'0' ; if (dp[xx][yy] != -1 && dp[xx][yy] <= tt) continue ; prt[xx][yy] = parent ; dp[xx][yy] = tt ; q[rear][0] = xx, q[rear][1] = yy ; rear++ ; } } } int output(int x, int y) {
int i, t, idx = prt[x][y] ; if (idx == -1) return 0 ; t = output(q[idx][0], q[idx][1]) ; printf ("%ds:(%d,%d)->(%d,%d)\n", ++t, q[idx][0], q[idx][1],x,y) ; if (graph[x][y] != '.') for (i = 0 ; i < graph[x][y]-'0' ; i++) printf ("%ds:FIGHT AT (%d,%d)\n", ++t, x, y) ; return t ; } int main () {
int i ; while (~scanf ("%d %d%*c", &n, &m)) {
for (i = 0 ; i < n ; i++) scanf ("%s%*c", graph[i]) ; memset (dp, -1, sizeof(dp)) ; memset (prt, -1, sizeof(prt)) ; bfs() ; if (dp[n-1][m-1] == -1) puts ("God please help our poor hero.") ; else {
printf ("It takes %d seconds to ""reach the target " "position, let me show you the way.\n", dp[n-1][m-1]) ; output(n-1, m-1) ; } puts ("FINISH") ; } return 0 ; }

转载于:https://www.cnblogs.com/lzsz1212/archive/2012/01/17/2324248.html

你可能感兴趣的文章
leetcode--Algorithm--Array_Part 1 Easy- 566 Reshape the Matrix
查看>>
AC自动机算法详解 (转载)
查看>>
python3-day5(模块)
查看>>
Linux配置JDK
查看>>
qt 读取xml文件
查看>>
python3之正则表达式
查看>>
Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
查看>>
Java 时间总结
查看>>
jQuery EasyUI 拖放 – 基本的拖动和放置
查看>>
这些年正Android - 母亲
查看>>
[工具] BurpSuite--XssValidator插件
查看>>
LPC1788系统时钟初始化
查看>>
channel vs mutex
查看>>
页面布局(--FlowLayout,--BorderLayout,--GridLayout)
查看>>
实验吧--web--你真的会php吗
查看>>
vue组件化学习第二天
查看>>
网络枚举工具推荐
查看>>
003LeetCode--LongestSubstring
查看>>
quarzt(官方)---给自己看的文档(SchedulerListeners)-8
查看>>
Linux-慕课网学习笔记-3-1命令格式
查看>>