Lightoj 1057 DP
来源:互联网 发布:java web源码 编辑:程序博客网 时间:2024/06/05 08:25
一开始还以为是BFS+DP,最后发现距离是可以直接算的。。。。
然后有一点要注意的是 如果没有g直接输出0......
我用的是状态压缩DP
AC代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;#define MAX 0x3f3f3f3ftypedef struct{int x, y;}Point;char maps[21][21];int M, N;Point point[20];int point_number;int distances[20][20];int dp[1<<16][17];int calculate_distance( Point p1, Point p2 ){int a = abs( p1.x - p2.x );int b = abs( p1.y - p2.y );return a > b ? a : b;}int main(){int T, Case = 1;cin >> T;while( T-- ){cin >> M >> N;point_number = 1;for( int i = 0; i < M; i++ ){cin >> maps[i];for( int j = 0; j < N; j++ ){if( maps[i][j] == 'x' ){point[0].x = i;point[0].y = j;}else if( maps[i][j] == 'g' ){point[point_number].x = i;point[point_number++].y = j;}}}point_number--;if( point_number == 0 ){// 如果没有g 则直接输出0 注意!cout << "Case " << Case++ << ": " << 0 << endl;continue;}for( int i = 0; i <= point_number; i++ ){for( int j = i + 1; j <= point_number; j++ ){distances[i][j] = distances[j][i] = calculate_distance( point[i], point[j] );}}memset( dp, -1, sizeof( dp ) );for( int i = 1; i <= point_number; i++ ){dp[1<<(i-1)][i] = distances[0][i];}for( int i = 1; i <= ( 1 << point_number ) - 1; i++ ){for( int j = 1; j <= point_number; j++ ){if( ( i & ( 1 << ( j - 1 ) ) ) == 0 ){continue;}for( int k = 1; k <= point_number; k++ ){if( i & ( 1 << ( k - 1 ) ) ){continue;}if( dp[i|(1<<(k-1))][k] == -1 || dp[i|(1<<(k-1))][k] > dp[i][j] + distances[j][k] ){dp[i|(1<<(k-1))][k] = dp[i][j] + distances[j][k];}}}}int ans = MAX;for( int i = 1; i <= point_number; i++ ){ans = min( ans, dp[(1<<point_number)-1][i] + distances[i][0] );}cout << "Case " << Case++ << ": " << ans << endl;}return 0;}
- Lightoj 1057 DP
- LightOJ - 1057 Collecting Gold(状压DP)
- LightOJ 1057 - Collecting Gold(dp)
- LightOJ 1057 - Collecting Gold (状压dp)
- LightOJ 1057 - Collecting Gold(状压DP)
- LightOJ-1057-状压dp,记忆化
- LightOJ 1057Collecting Gold(状压DP)
- lightoj 1140 数位DP
- lightoj 1032 数位DP
- lightoj 1068 数位DP
- lightoj 1205 数位DP
- lightoj 1422 区间DP
- Lightoj 1004 DP
- Lightoj 1013 DP
- Lightoj 1017 DP
- Lightoj 1018 DP
- Lightoj 1021 数位DP
- Lightoj 1025 DP
- VC/MFC中的CComboBox控件使用详解
- Hash函数以及解决冲突的方法
- poj 3281 网络流
- 大偏移量下Redis、MongoDB分页/排名性能比较
- 《具体数学》 递归 热身题
- Lightoj 1057 DP
- FPGA学习资源
- 如何查看IIS并发连接数
- 使用grep 过滤logcat输出
- Oracle 查看 表, 存储过程, 触发器, 函数, 等对象定义语句
- 学习多线程同步之智能指针巧用
- NDK Java C 数据类型转换
- 机器学习_adaboost 算法
- 黑马程序员_源自梦想 Exception--异常