【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 ...
随机推荐
- Visual Studio 调试技巧 -- 为 Lambda 表达式设置中断
如果我说 .NET 世界上最厉害的开发平台(语言),资深的同仁肯定不会往下看了,因为这将是一个无休止的争论,到头来搞不好还是人身攻击.然而,如果我说 Visual Studio 是世界上最友好最强大的 ...
- Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)
How do I increase the maximum number of open files under CentOS Linux? How do I open more file descr ...
- (tomcat访问不了的两种解决方法)Bad Request(Invalid Hostname)
显示这个页面的时候一般有几中解决方法: 第一种就是如下图所示的方法: 具体步骤是: 1.也就是左下角win的“运行”中输入cmd进入doc窗口中 2.输入代码:netstat -ano 3.找到占用8 ...
- 实训第二天早上--hibernate之配置文件映射和注解
hibernate 逐步优化第一步 只按照步骤来提取的jre包导入错误第二步 继续封装,把增删改查提取出来,同时进行代码的封装HQL语句 be stranger in the code .be fo ...
- C#使用Process类调用外部程序(转)
在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...
- 与数据库打交道的Adapter----SimpleCursorAdapter
http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...
- android 利用隐式Intent打开图片
实现功能 点击"查看图片"时能够跳出提示,选择系统图库打开还是自己编写的应用打开,并且对于下载好的图片也有效. 1.我将 qiaoba.jpg 放在 res/drawable ...
- 动软代码生成器三层用于winform
DBUtility项目中的DbHelperSQL.cs (找自己对应的数据库类型) 修改前20行中的数据库连接字符串获取方式为: //数据库连接字符串(web.config来配置),多数据库可使用Db ...
- Xcode 7:Storyboard Reference、Strong IBOutlet以及Scene Dock
本文由CocoaChina译者小袋子(博客)翻译原文:Storyboard Reference, Strong IBOutlet, Scene Dock in iOS 9 在这个教程中,我想要聊一些有 ...
- IOS开发效率之为Xcode添加常用的代码片段
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...