lightoj 1057 - Collecting Gold(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057
题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差。然后只有15个藏金点状压一下加dfs就行了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define inf 0X3f3f3f3f
using namespace std;
char mmp[30][30];
struct TnT {
int x , y;
}Node[20];
int getlen(int x , int y) {
return max(abs(Node[x].x - Node[y].x) , abs(Node[x].y - Node[y].y));
}
int dp[17][1 << 17] , cnt;
void dfs(int pos , int state) {
if(state == (1 << cnt) - 1) return ;
for(int i = 0 ; i < cnt ; i++) {
if(state & (1 << i)) continue;
else {
if(dp[i][state | (1 << i)] > dp[pos][state] + getlen(pos , i)) {
dp[i][state | (1 << i)] = dp[pos][state] + getlen(pos , i);
dfs(i , state | (1 << i));
}
}
}
}
int main() {
int t;
scanf("%d" , &t);
int n , m , Case = 0;
while(t--) {
scanf("%d%d" , &n , &m);
int count = 0;
for(int i = 0 ; i < n ; i++) {
scanf("%s" , mmp[i]);
for(int j = 0 ; j < m ; j++) {
if(mmp[i][j] == 'x') Node[0].x = i , Node[0].y = j;
if(mmp[i][j] == 'g') Node[++count].x = i , Node[count].y = j;
}
}
for(int i = 0 ; i <= count ; i++) {
for(int j = 0 ; j < (1 << count + 1) ; j++) {
dp[i][j] = inf;
}
}
cnt = count + 1;
dp[0][0] = 0;
dfs(0 , 0);
int ans = inf;
for(int i = 1 ; i <= count ; i++) {
ans = min(ans , dp[i][(1 << cnt) - 1] + getlen(0 , i));
}
printf("Case %d: %d\n" , ++Case , ans == inf ? 0 : ans);
}
return 0;
}
lightoj 1057 - Collecting Gold(状压dp)的更多相关文章
- [LightOJ 1018]Brush (IV)[状压DP]
题目链接:http://lightoj.com/volume_showproblem.php? problem=1018 题意分析:平面上有不超过N个点,如今能够随意方向划直线将它们划去,问:最少要划 ...
- LightOJ1057 Collecting Gold(状压DP)
这道题可以想到几点: 整个行程可以看作一次次的行走,每次行走都是用最短的路程从某一非空点到达另外一非空点: 两点间最少的步数是二者x和y坐标差的最大值: 返回原点这个过程,肯定是取完最后一个黄金后直接 ...
- lightoj 1119 - Pimp My Ride(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1119 题解:状压dp存一下车有没有被搞过的状态就行. #include < ...
- lightoj 1037 - Agent 47(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1037 #include <iostream> #include & ...
- 1057 - Collecting Gold
1057 - Collecting Gold PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- poj3254 Corn Fields (状压DP)
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...
- K - Painful Bases 状压dp
Painful Bases LightOJ - 1021 这个题目一开始看,感觉有点像数位dp,但是因为是最多有16进制,因为限制了每一个数字都不同最多就有16个数. 所以可以用状压dp,看网上题解是 ...
- 简单状压dp的思考 - 最大独立集问题和最大团问题 - 壹
本文参考:CPH ,USACO Guide (大佬请越过,这是初学笔记,不要吐槽内容) 前置知识:位运算基础,动态规划基础 介绍 状态是元素的子集的动态规划算法,可以用位运算来高效的优化. 那么第一道 ...
随机推荐
- 【iOS】libc++abi.dylib: terminate_handler unexpectedly threw an exception
用 ShareSDK 做第三方分享的时候遇到了这个问题…… 联系了客服,后来在他的指导下,发现是数组的问题,该问题不知道是否具有通用性,暂且记下.
- MyBatis 框架 基础应用
1.ORM的概念和优势 概念: 对象关系映射(Object Relational Mapping,简称ORM)是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据 ...
- Hive映射HBase表的几种方式
1.Hive内部表,语句如下 CREATE TABLE ods.s01_buyer_calllogs_info_ts( key string comment "hbase rowkey&qu ...
- 精准营销、批量提取QQ群成员号码
有时我们在做精准营销时,需要从社群里提取群成员的QQ号,群发邮件,常规的做法是手工一个个复制粘贴,这样的效率无疑是很低的,下面我来分享一个批量获取社群的QQ号方法. 需要具备以下工具: 1.大量精准Q ...
- word 文档导出 (freemaker+jacob)--java开发
工作中终于遇到了 需要导出word文旦的需求了.由于以前没有操作过,所以就先百度下了,基本上是:博客园,简书,CDSN,这几大机构的相关帖子比较多,然后花了2周时间 才初步弄懂. 学习顺序: 第一阶 ...
- 【Java笔记】【Java核心技术卷1】chapter3 D2注释
package chapter3; /** * 文档注释 *@author lp *@version 1 **/ public class D2注释 { //单行注释 /* 长注释 */ }
- Spring Security (CORS)跨域资源访问配置
1.CORS介绍 CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源(协议 + 域名 + 端口)服务 ...
- 谈谈你对java平台的理解?
问题:谈谈你对java平台的理解?java是解释执行,这句话对吗? 典型回答:java本身是一种面向对象的语言,具有很好的跨平台的能力,能够做到“write once ,run anywhere”.另 ...
- GOF23-单列模式
1.什么是单例模式 一个类只有一个实列,并且提供一个对外访问该实例的全局访问点. 常见应用场景:数据库连接池,项目中读取配置文件的类,servlet也是单列,Spring中的Bean默认也是单列 2. ...
- SQL 分组后,获取每组中的最大值对应的数据
select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num, ...