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 ...
随机推荐
- Docker搭建ELK的javaweb应用日志收集存储分析系统
1.启动elasticsearch docker run -d --name myes -p 9200:9200 elasticsearch:2.3 2.启动kibana docker run --n ...
- oracle常用的时间格式转换
1:取得当前日期是本月的第几周 select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'YY') se ...
- JavaScript实现无缝滚动 原理详细讲解
先了解一下对象的几个的属性: innerHTML: 设置或获取位于对象起始和结束标签内的 HTML scrollHeight: 获取对象的滚动高度. scrollLeft: 设置或获取位于对象左边界和 ...
- 定位真机运行能用但是打包成apk就不能用的解决方法
打包apk的SHA1,与key的SHA1(这是多人开发的通病不同电脑共同开发一个app的常见错误之一)不一致.解决方法: 今天虽然离职了,但是今天遇到的是,当我在用高德地图开发的时候,在Android ...
- 001. 注释过的boot.s
从网上搜罗一个很详细注释的boot.s版本,加了小小一点点自己的理解,不太多. 用 as86, ld86 可以编译, ubuntu下可以通过 apt install bin86 来安装好像. ; ...
- Android原生调用mui里面的js如何实现
遍历所有运行中的webview页面,采用自带的SDK方法进行获取所有的IWebview.MUI中自带的webview是一个IWebviewArrayList<IWebview> webli ...
- go struct 继承
- C# event 和delegate的区别
其实说实话 event和delegate 并没有什么特别大的区别,event其实就是特殊化的delegate 1.调用上event只能+=和-=,这样做的意义是为了防止,其余的方法覆盖,但是deleg ...
- Spring Boot:Boot2.0版本整合Neo4j
前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大. 场景还是电影人员关系 Boot 2.0主要变化 GraphRepository在Boot2.0下不支持了,调 ...
- HTML5小知识汇总
1.关于<!DOCTYPE HTML> H5只需要<!DOCTYPE HTML>这样简单的声明,不用之前一长串代码,因为H5不是基于SGML,所以不需要对DTD引用,但是需要D ...