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】UIAlertView 点击跳转事件
iOS 开发中,UIAlertView 经常用到.这里记录下曾用到的点击跳转事件. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@& ...
- 基于 Autojs 的 APP、小程序自动化测试 SDK
原文:https://blog.csdn.net/laobingm/article/details/98317394 autojs sdk基于 Autojs 的 APP.小程序自动化测试 SDK,支持 ...
- Mac安装Homebrew的那些事儿
Mac安装Homebrew的那些事儿 最近小明刚换置了一个 Mac 本,想搭建一个属于自己的博客网站,需要用到 Node.js 环境,而Node.js 在 MacOS 中是由 Homebrew 进行安 ...
- android ——活动
活动(Activity)主要用于和用户进行交互,是一种可以包含用户界面的组件. 1.手动创建活动 右击com.example.administrator.exp5→New→Activity→Empty ...
- (二)对象以及变量的并发访问--synchronized的使用细节,用法
具体的记录synchronized关键的各种使用方式,注意事项.感觉一步一步跟我来都可以看懂滴 大致是按照以下思路进行书写的.黑体字可以理解为结论, 1.synchronized锁的是什么? 2.sy ...
- pythonday02基础与运算符
今日概要 1.循环 2.字符串格式化 3.运算符 4.编码 if的嵌套 score = input('请输入成绩') score_int = int(score) if score_int >= ...
- python(自用手册)导图
- (转载)分享常用的GoLang包工具
分享常用的GoLang包工具 包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/e ...
- Spark流式编程介绍 - 编程模型
来源Spark官方文档 http://spark.apache.org/docs/latest/structured-streaming-programming-guide.html#programm ...
- Greenplum数据库分享
1. 场景描述 最近做了次Greenplum数据库的分享,如下图,把第三章的的架构介绍简单提出来,分享下. 2. 解决方案 就按照ppt贴图了,部分内容稍微做了下马赛克. (这页ppt的下半部分,有实 ...