hdu 2312 Cliff Climbing (pfs)
一条很暴力,有点恶心的搜索。题意其实很简单,主要是pfs的时候拓展结点会有种麻烦的感觉。注意的是,这里的n和m跟平常见到的有所不同,交换过来了。我的代码就是在因为这个长宽的问题wa了一次。
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cctype> using namespace std; typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;
typedef pair<int, PPP> PPPI; #define FI first
#define SE second
#define PRIQ priority_queue const int R = ;
const int C = ;
char mat[R][C];
bool vis[R][C][R][C];
int n, m; inline int dis(int a, int b, int c, int d) { return abs(a - c) + abs(b - d);}
inline bool inmat(int a, int b) { return <= a && a < n && <= b && b < m;} int work() {
PRIQ<PPPI> Q;
int nx, ny, cx, cy;
memset(vis, , sizeof(vis));
while (!Q.empty()) Q.pop();
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if (mat[i][j] != 'S') continue;
for (int dx = -; dx <= ; dx++) {
nx = i + dx;
for (int dy = ; dy <= ; dy++) {
ny = j + dy;
if (inmat(nx, ny) && dis(i, j, nx, ny) <= && mat[nx][ny] != 'X' && !vis[i][j][nx][ny]) {
if (isdigit(mat[nx][ny])) Q.push(PPPI((int) '' - mat[nx][ny], PPP(PII(i, j), PII(nx, ny))));
else Q.push(PPPI(, PPP(PII(i, j), PII(nx, ny))));
vis[i][j][nx][ny] = true;
// cout << i << ' ' << j << ' ' << nx << ' ' << ny << endl;
}
ny = j - dy;
if (inmat(nx, ny) && dis(nx, ny, i, j) <= && mat[nx][ny] != 'X' && !vis[nx][ny][i][j]) {
if (isdigit(mat[nx][ny])) Q.push(PPPI((int) '' - mat[nx][ny], PPP(PII(nx, ny), PII(i, j))));
else Q.push(PPPI(, PPP(PII(nx, ny), PII(i, j))));
vis[nx][ny][i][j] = true;
// cout << nx << ' ' << ny << ' ' << i << ' ' << j << endl;
}
}
}
}
}
// cout << "find S" << endl;
while (!Q.empty()) {
PPPI cur = Q.top();
Q.pop();
int v = cur.FI, a = cur.SE.FI.FI, b = cur.SE.FI.SE, c = cur.SE.SE.FI, d = cur.SE.SE.SE;
// cout << v << ' ' << a << ' ' << b << ' ' << c << ' ' << d << endl;
if (mat[a][b] == 'T' || mat[c][d] == 'T') return -v;
for (int dx = -; dx <= ; dx++) {
nx = a + dx;
cx = c + dx;
for (int dy = ; dy <= ; dy++) {
ny = b + dy;
if (inmat(nx, ny) && dis(a, b, nx, ny) <= && mat[nx][ny] != 'X' && !vis[a][b][nx][ny]) {
if (isdigit(mat[nx][ny])) Q.push(PPPI((int) '' - mat[nx][ny] + v, PPP(PII(a, b), PII(nx, ny))));
else Q.push(PPPI(v, PPP(PII(a, b), PII(nx, ny))));
vis[a][b][nx][ny] = true;
// cout << nx << ' ' << ny << endl;
}
cy = d - dy;
if (inmat(cx, cy) && dis(cx, cy, c, d) <= && mat[cx][cy] != 'X' && !vis[cx][cy][c][d]) {
if (isdigit(mat[cx][cy])) Q.push(PPPI((int) '' - mat[cx][cy] + v, PPP(PII(cx, cy), PII(c, d))));
else Q.push(PPPI(v, PPP(PII(cx, cy), PII(c, d))));
vis[cx][cy][c][d] = true;
// cout << cx << ' ' << cy << endl;
}
}
}
}
return -;
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
while (cin >> m >> n && (n || m)) {
char buf[];
for (int i = ; i < n; i++) for (int j = ; j < m; j++) cin >> buf, mat[i][j] = buf[];
cout << work() << endl;
}
return ;
}
——written by Lyon
hdu 2312 Cliff Climbing (pfs)的更多相关文章
- 【hdu 4315】Climbing the Hill
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- HDU 4315:Climbing the Hill(阶梯博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=4315 题意:有n个人要往坐标为0的地方移动,他们分别有一个位置a[i],其中最靠近0的第k个人是king,移动的 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- 最短路&查分约束
[HDU] 1548 A strange lift 根蒂根基最短路(或bfs)★ 2544 最短路 根蒂根基最短路★ 3790 最短路径题目 根蒂根基最短路★ 2066 一小我的观光 根蒂根基最短路( ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
随机推荐
- 引入样式表(css)的四种方式
一.使用style属性: 将style属性直接加在html标签里. <标签 style="属性1: 设定值1; 属性2: 设定值2; "> 例如: <td sty ...
- 解决VC编译的DLL要安装对应运行环境否则无法使用的问题
编译时候改成静态链接,再通过使用Depends软件查看依赖即可.具体操作步骤:项目->属性->配置属性->C/C++-代码生成->运行库->多线程(/MT)说明:这种方法 ...
- pycharm最新激活码2017
最新的2017激活码 BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZW ...
- Js 克隆
1.浅表克隆 调用concate() 或者slice() 方法,可以创建数组的浅表副本,在浅表副本中,如果原始数组的元素是复杂数据类型,则元素值指向对象的引用而非对象本身, 与原始数组一样,浅表副本的 ...
- CSS Tools: Reset CSS
样式初始化 /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ ...
- Windows下更改pip镜像源
其实学习是一个逐步探索的过程.今天因为把带有中文的Python安装路径给改了,结果带来很大的麻烦,导致在命令行输入vietualenv和其他一些第三方模块都出现Fatal error in launc ...
- perfcurve.m
function [X,Y,T,auc,optrocpt,subY,subYnames] = ... perfcurve(labels,scores,posClass,varargin) %PERFC ...
- 微信小程序之上拉加载更多
loadmore 加载更多(分页加载) 当用户打开一个页面时,假设后台数据量庞大时,一次性地返回所有数据给客户端,页面的打开速度就会有所下降,而且用户只看上面的内容而不需要看后面的内容时,也浪费用户流 ...
- WPF学习(8)数据绑定 https://www.cnblogs.com/jellochen/p/3541197.html
说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...
- systemd管理nginx
首先安装nginx,此处不做赘述. 保存以下内容到/lib/systemd/system/nginx.service文件. [Unit] Description=The NGINX HTTP and ...