Problem - 2312

  一条很暴力,有点恶心的搜索。题意其实很简单,主要是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)的更多相关文章

  1. 【hdu 4315】Climbing the Hill

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. HDU 4315:Climbing the Hill(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=4315 题意:有n个人要往坐标为0的地方移动,他们分别有一个位置a[i],其中最靠近0的第k个人是king,移动的 ...

  3. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  4. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  5. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  6. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  7. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  8. 最短路&查分约束

    [HDU] 1548 A strange lift 根蒂根基最短路(或bfs)★ 2544 最短路 根蒂根基最短路★ 3790 最短路径题目 根蒂根基最短路★ 2066 一小我的观光 根蒂根基最短路( ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. [jnhs]教训之jsp页面无法用jstl取值的坑.真他妈的奇葩,实体类的属性名不能用大写

    结果页面永远都是空 调试发现,数据正常的塞进去了 问题解决: https://zhidao.baidu.com/question/570584436.html 实体类的属性名,首字母不能大写,改成小写 ...

  2. 学习JDK1.8集合源码之--WeakHashMap

    1. WeakHashMap简介 WeakHashMap继承自AbstractMap,实现了Map接口. 和HashMap一样,WeakHashMap也是一种以key-value键值对的形式进行数据的 ...

  3. Lab2 新增的细节

    entry.S 新增加了这个入口函数 bootloader 加载完成后 将执行 kern_entry 而非lab1 中的kern_init defs.h 使用了 ({})宏定义的方式,并且执行了一行定 ...

  4. 【模板】倍增LCA [2017年5月计划 清北学堂51精英班 Day3]

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  5. 关于rss的内容(转载)

    转载自: https://blog.csdn.net/zhao1949/article/details/52806123 (本文对读者有帮助的话请移步支持原作者) 内容记录: 在C++技术网开通了RS ...

  6. vi中如何跳转到指定行数

    输入:n,代表跳转到第n行,如:100,就跳转到第100行.

  7. win下在虚拟机安装CentOS 7 Linux系统

    准备: CentOS 7下载地址(我下的是everthing版本):https://www.centos.org/download/ 一.首先下载安装虚拟机VMware 地址官网下载即可. 二.安装操 ...

  8. python3.7 安装gensim使用word2Vec库

    应用的文章(个人试验过,完全正确):https://radimrehurek.com/gensim/index.html#install

  9. Python科学计算生态圈

  10. 【如花美眷】初探weex

    我想我更喜欢weex的原因,应该是weex可以直接运行在浏览器中,而不是像react-native需要运行在模拟设备中. 我想这个原因足以让我使用vue而不是RN. 初探就是稍微运行一下,来看步骤 可 ...