【HDOJ】3442 Three Kingdoms
bfs+状态压缩。初始化数组的曼哈顿距离条件写错了,改了一下午。
/* 3442 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 55 typedef struct node_t {
int x, y;
int s, v;
node_t() {}
node_t(int xx, int yy, int ss, int vv) {
x = xx; y = yy; s = ss; v = vv;
}
friend bool operator <(const node_t &a, const node_t &b) {
return a.v > b.v;
}
} node_t; char map[MAXN][MAXN];
char hurt[MAXN][MAXN][];
bool visit[MAXN][MAXN][];
int ls[] = {, , , , };
int bx, by, ex, ey;
int dir[][] = {
{-,},{,},{,-},{,}
};
int n, m; int abs(int x) {
return x< ? -x:x;
} bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} bool invalid(char ch) {
return !(ch=='$' || ch=='!' || ch=='C' || ch=='.');
} int Manh(int x0, int y0, int x1, int y1) {
return abs(x0-x1) + abs(y0-y1);
} void init() {
int i, j, k, h, l;
int x, y, z; memset(hurt, , sizeof(hurt));
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
if (map[i][j] == '$') {
bx = i;
by = j;
} else if (map[i][j] == '!') {
ex = i;
ey = j;
} else if (map[i][j]=='#' || map[i][j]=='.') {
continue;
} else if (map[i][j]>='A' && map[i][j]<='E'){
z = map[i][j] - 'A';
h = z + ;
l = ls[z];
for (x=i-l; x<=i+l; ++x) {
for (y=j-l; y<=j+l; ++y) {
if (check(x, y) || Manh(x,y,i,j)>l)
continue;
hurt[x][y][z] = h;
}
}
}
}
}
} int bfs() {
int i, j, k;
int x=bx, y=by, s=, v=;
node_t nd;
priority_queue<node_t> Q; memset(visit, false, sizeof(visit));
visit[x][y][s] = true;
Q.push(node_t(x, y, s, v)); while (!Q.empty()) {
nd = Q.top();
if (nd.x==ex && nd.y==ey)
return nd.v;
Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
s = nd.s;
v = nd.v;
if (check(x, y) || invalid(map[x][y]) || visit[x][y][s])
continue;
visit[x][y][s] = true;
for (j=; j<; ++j) {
k = <<j;
if (hurt[x][y][j] && (s&k)==) {
v += hurt[x][y][j];
s |= k;
}
}
Q.push(node_t(x, y, s, v));
}
} return -;
} int main() {
int t, tt;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif scanf("%d", &t);
for (tt=; tt<=t; ++tt) {
scanf("%d %d", &n, &m);
for (i=; i<n; ++i)
scanf("%s", map[i]);
init();
k = bfs();
printf("Case %d: %d\n", tt, k);
} return ;
}
【HDOJ】3442 Three Kingdoms的更多相关文章
- 【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 ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- SystemTap----常用变量、宏、函数和技巧
http://blog.csdn.net/moonvs2010/article/category/1570309
- windows 配置host
windows 下如何配置host文件, 只需修改: C:\Windows\System32\drivers\etc\hosts文件即可 默认文件内容如下: # Copyright (c) 1993- ...
- 关于oracle的函数,存储过程,触发器,序列,视图,左右连接一些的应用 带案例
CREATE TABLE STUDENT( --创建学生表 ID NUMBER(10) PRIMARY KEY, --主键ID NAME VARCHAR2(20), CLASSNAME VA ...
- ZOJ 3898 - Stean 积分
有一个陶罐,陶罐是由函数Y=2+cosX,截取x=Z1到x=Z2段后,形成的旋转体,陶罐只有底x=Z1,没有盖子. 问陶罐能乘多少的水(体积),以及它的表面积 体积还是比较好求的,直接用旋转体体积公式 ...
- codevs1380 没有丧尸的舞会
/* 树形DP 而然我并不知道树在哪(....) f[x][0]表示x节点不参加舞会 以x为根的子树的最优解 f[x][1]表示x节点参加舞会 以x为根的子树的最优解 方程为:(so为x的儿子 so要 ...
- 基于Android_volley的Get、Post的方法
用Android_volley加载网络信息有Get,post两种方式,通过一个例子来说明,在Activity中设置两个Button,分别测试Get.post方法 一般分为三步, 1. 创建一个Requ ...
- nopi导入导出
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Android学习笔记(广播机制)
1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...
- 十一、C# 泛型
为了促进代码重用,尤其是算法的重用,C#支持一个名为泛型的特性. 泛型与模块类相似. 泛型使算法和模式只需要实现一交.而不必为每个类型都实现一次.在实例化的时候,传入相应的数据类型便可. 注:可空值类 ...
- Direct 2D实现界面库 (1)
大学时尝试过很多次写一个UI库, 初次使用 GDI 绘图, 当时水平很低, GDI功能太弱, 以失败而告终. 之后使用 GDI+ 绘图, 当时水平依旧很低, GDI功能很强, 但效率实在太慢, 以失败 ...