【HDOJ】1242 Rescue
BFS+优先级队列。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXNUM 205 typedef struct node_st {
int x, y ,t;
node_st() {}
node_st(int xx, int yy, int tt) {
x = xx; y = yy; t = tt;
}
friend bool operator < (node_st a, node_st b) {
return a.t > b.t;
}
} node_st; int n, m;
char map[MAXNUM][MAXNUM];
char visit[MAXNUM][MAXNUM];
int direct[][] = {{,},{-,},{,-},{,}}; int bfs(int x, int y) {
priority_queue<node_st> que;
node_st node;
int i, t; que.push(node_st(x, y, ));
memset(visit, , sizeof(visit));
visit[x][y] = ; while ( !que.empty() ) {
node = que.top();
if (map[node.x][node.y] == 'a')
return node.t;
que.pop();
t = node.t + ;
for (i=; i<; ++i) {
x = node.x + direct[i][];
y = node.y + direct[i][];
if (x< || x>=n || y< || y>=m)
continue;
if (visit[x][y] || map[x][y]=='#')
continue;
if (map[x][y] == 'x')
que.push(node_st(x,y,t+));
else
que.push(node_st(x,y,t));
visit[x][y] = ;
}
} return -;
} int main() {
int i, j, t;
int begx, begy; while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'r') {
begx = i;
begy = j;
}
}
}
t = bfs(begx, begy);
if (t == -)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", t);
}
return ;
}
【HDOJ】1242 Rescue的更多相关文章
- 【HDOJ】4057 Rescue the Rabbit
挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- Chrome资源加载被Cancel的问题
好个表久没写文章了. 做为一个砖业的砖工只能天天搬砖做些没有营养没有技术难度的活儿. 最近折腾个网站.发现一个很奇怪的问题- 各种图片的请求被取消了状态为Canceled. 顿时Chrome变成一个更 ...
- MVC中如何在controller的action中输出JS到页面上
MVC中如何在controller的action中输出JS到页面上 可以通过Http上下文对象(httpContext)就可以了,在Action中的HttpContext就是这个Action所指向的页 ...
- (SQL Analyzer services)定义链接维度
链接文档是在一个 Analysis Services 数据库中存在.但在相同版本和兼容级别的另一个 Analysis Services 数据库中重复使用的一种文档. 链接文档是只读的. 如果您想要修改 ...
- LIBPNG使用小结(二)
之前写的LIBPNG库学习小结介绍了怎么样自定义LIBPNG库的write.read.flush函数,而不使用LIBPNG库提供的默认函数. 上一篇讲述的都是在单线程的情况下,今天将程序升级,放在多线 ...
- ubuntu 安装 fcitx
安装fcitx (1)添加ppa源 sudo add-apt-repository ppa:fcitx-team/nightly 或 sudo add-apt-repository ppa:fcitx ...
- ABP手机端调用API时的CORS
这个问题其实很早就考虑了,当时因为也没有特别着急去解决这个问题,就一直拖着.... 好吧,拖延症是不好的,所有不懒得做的,终将会逼着你去再很短的时间内去解决问题...实现项目 改写一个已有的webfo ...
- js+jquery检测用户浏览器型号(包括对360浏览器的检测)
做网站,js检测用户浏览器的版本,是经常要使用到,今天自己写了一个js,完成了对于一些常见浏览器的检测,但是,偏偏对于360浏览器的检测没有任 何办法,研究了一会儿,无果.无论是360安全浏览器,还是 ...
- [記錄用]python py2app 檔案批次重新命名
demo.py 主要作用為 將同目錄下 *.mp4 檔案批次重新命名 例如: aaa001.mp4 ---重新命名為--> 001.mp4 aaa002.mp4 ---重新命名為--> 0 ...
- poco异步等待ActiveResult
#include "Poco/ActiveMethod.h"#include "Poco/ActiveResult.h"#include <utility ...
- POJ 3349 Snowflake Snow Snowflakes Hash
题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...