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. Vue--通过button跳转到其他组件并携带id参数

    一.创建vue文件 ’ <template> <div> goodsCommon<br/> goodsCommon<br/> goodsCommon&l ...

  2. 基于遗传算法(Genetic Algorithm)的TSP问题求解(C)

    基于遗传算法的TSP问题求解(C) TSP问题: TSP(Travelling salesman problem): 译作“旅行商问题”, 一个商人由于业务的需要,要到n个城市,每个城市之间都有一条路 ...

  3. python变量、变量属性

    1.简述执行Python程序的两种方式以及他们的优缺点 答:交互型优点:马上就能看到结果,排错方便.交互型缺点:代码无法保存,断电即消失 命令型优点:代码永久保存.命令型缺点:不易排错 2.简述Pyt ...

  4. phpcms 允许英文目录有空格

    大家都用过phpcm添加栏目吧,在添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文 ...

  5. 关于JSON的个人理解

    1.比xml更易于解析的数据存储方式 2.主要是用键值对的方式进行存储 3.可以用来存储对象或者是对象数组 个人感觉W3C上给的教程很好

  6. Spark day03

    补充算子 transformations mapPartitionWithIndex 类似于mapPartitions,除此之外还会携带分区的索引值. repartition 增加或减少分区.会产生s ...

  7. DAY1-作业

    Python-day1-------> 本节内容: Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据 ...

  8. Person Re-identification 系列论文笔记(六):AlignedReID

    AlignedReID Zhang X, Luo H, Fan X, et al. AlignedReID: Surpassing Human-Level Performance in Person ...

  9. Inno setup 卸载时删除程序文件夹(文件)

    Inno setup 卸载时删除程序文件夹(文件) //删除所有配置文件以达到干净卸载的目的 procedure CurUninstallStepChanged(CurUninstallStep: T ...

  10. python中Sting字符串

    字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d'] content = '' content = ''.join(a) print content 方法2: 用字 ...