LightOJ - 1057 Collecting Gold(状压DP)
2015-10-28 23:02
323 查看
题目大意:给出一张地图,刚开始在X这个位置,问走到所有的g后返回原点的最小移动次数
解题思路:用dp[i][j]表示走的状态是j,当前停在i需要移动的次数
接着转移就可以了
解题思路:用dp[i][j]表示走的状态是j,当前停在i需要移动的次数
接着转移就可以了
#include <cstdio> #include <cstring> #include <queue> #include <cstdlib> using namespace std; const int N = 20; const int S = (1 << 16) + 10; struct Node { int pos, s; Node() {} Node(int pos, int s): pos(pos), s(s) {} }; int dp [S]; int cnt, n, m, cas = 1; int x , y ; char map ; void init() { cnt = 0; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", map); for (int j = 0; j < m; j++) { if (map[j] == 'x') { x[0] = i; y[0] = j; } else if (map[j] == 'g') { x[++cnt] = i; y[cnt] = j; } } } } int dis(int pos, int i) { int row = abs(x[pos] - x[i]); int col = abs(y[pos] - y[i]); return max(row, col); } void solve() { memset(dp, 0x3f, sizeof(dp)); queue<Node> Q; Q.push(Node(0, 1)); dp[0][1] = 0; int All = (1 << (cnt + 1)) - 1; while (!Q.empty()) { int pos = Q.front().pos; int s = Q.front().s; Q.pop(); if (s == All) { dp[0][All] = min(dp[0][All], dp[pos][All] + dis(pos, 0)); continue; } for (int i = 1; i <= cnt; i++) { if (s & (1 << i)) continue; if (dp[i][s | (1 << i)] > dp[pos][s] + dis(i, pos)) { dp[i][s | (1 << i)] = dp[pos][s] + dis(i, pos); Q.push(Node(i, s | (1 << i))); } } } printf("Case %d: %d\n", cas++, dp[0][All]); } int main() { int test; scanf("%d", &test); while (test--) { init(); solve(); } return 0; }
相关文章推荐
- LightOJ - 1051 Good or Bad(扫描)
- 好用的类别Category
- django下video播放时间不能超过5s并且视频有时无法显示问题的解决方法
- 用Lingo求解线性规划问题
- Automated CMS category, version identification (CMS vulnerability detection)
- 最短路-SPFA(Shortest Path Faster Algorithm)
- 【CodeForces Yandex Algorithm 2011 D】【莫队分块】区间所有数值乘其数个数的平方
- 《转》---google面经
- django log问题
- 在Mac OS上安装Go语言编译器的方法
- Plugin(插件)和Goals(目标)
- lcd驱动。。解决开发板小企鹅logo跑偏问题
- 举例详解Go语言中os库的常用函数用法
- Django基础(2)编写你的第一个Django应用
- Algorithms—295.Find Median from Data Stream
- django自学
- Golang的os标准库中常用函数的整理介绍
- USB-OTG(on-the-go)
- 深入解析Go语言编程中slice切片结构
- boost之-lboost_system