poj 1691 状态压缩DP
2014-04-02 16:03
260 查看
WA了一次。。。。
dp[statu][i] 表示状态statu中的块都已经涂了且最后一次涂得i所需要的最少拿起刷子次数
然后转移就是枚举所有的块,判断能否刷,能刷就更新
这题有一点要注意的是:只要是在该方块的上方有一部分接触就算邻接方块!!!
妈蛋!我开始还以为要全部邻接才行
AC代码如下:
dp[statu][i] 表示状态statu中的块都已经涂了且最后一次涂得i所需要的最少拿起刷子次数
然后转移就是枚举所有的块,判断能否刷,能刷就更新
这题有一点要注意的是:只要是在该方块的上方有一部分接触就算邻接方块!!!
妈蛋!我开始还以为要全部邻接才行
AC代码如下:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; #define MAX 0x3f3f3f3f struct Node{ int x[2], y[2]; int c; }; Node node[20]; int N; int dp[1<<16][16]; bool judge( int statu, int i ){ bool mark[20]; memset( mark, false, sizeof( mark ) ); for( int k = 0; k < N; k++ ){ if( k != i && node[k].x[1] == node[i].x[0] ){ if( node[k].y[1] <= node[i].y[1] && node[k].y[0] >= node[i].y[0] ){ mark[k] = true; } if( node[k].y[1] <= node[i].y[1] && node[k].y[1] > node[i].y[0] ){ mark[k] = true; } if( node[k].y[0] >= node[i].y[0] && node[k].y[0] < node[i].y[1] ){ mark[k] = true; } } } for( int k = 0; k < N; k++ ){ if( mark[k] ){ if( !( statu & ( 1 << k ) ) ){ return false; } } } return true; } int main(){ int T; scanf( "%d", &T ); while( T-- ){ scanf( "%d", &N ); for( int i = 0; i < N; i++ ){ scanf( "%d%d%d%d%d", &node[i].x[0], &node[i].y[0], &node[i].x[1], &node[i].y[1], &node[i].c ); } memset( dp, -1, sizeof( dp ) ); for( int i = 0; i < N; i++ ){ if( node[i].x[0] == 0 ){ dp[1<<i][i] = 1; } } for( int i = 0; i < ( 1 << N ); i++ ){ for( int j = 0; j < N; j++ ){ if( dp[i][j] != -1 ){ for( int k = 0; k < N; k++ ){ if( i & ( 1 << k ) ){ continue; } if( judge( i, k ) ){ if( node[j].c == node[k].c ){ if( dp[i|(1<<k)][k] == -1 ){ dp[i|(1<<k)][k] = dp[i][j]; }else{ dp[i|(1<<k)][k] = min( dp[i|(1<<k)][k], dp[i][j] ); } }else{ if( dp[i|(1<<k)][k] == -1 ){ dp[i|(1<<k)][k] = dp[i][j] + 1; }else{ dp[i|(1<<k)][k] = min( dp[i|(1<<k)][k], dp[i][j] + 1 ); } } } } } } } int ans = MAX; for( int i = 0; i < N; i++ ){ if( dp[(1<<N)-1][i] != -1 ){ ans = min( ans, dp[(1<<N)-1][i] ); } } cout << ans << endl; } return 0; }
相关文章推荐
- POJ 1691 Painting a Board(状态压缩DP)
- poj 1691 Painting a Board(状态压缩DP)
- poj - 1691 - Painting A Board(状态压缩dp)
- POJ 3254 Corn Fields(状态压缩DP)
- poj 1185 炮兵阵地 【状态压缩dp】
- POJ 2411 Mondriaan's Dream 状态压缩dp
- POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)
- POJ 3254 Corn Fields(状态压缩dp)
- poj 3254 Corn Fields (状态压缩dp)
- poj 3740 Easy Finding//Dancing Links 或 状态压缩Dp
- poj 2411 Mondriaan's Dream(状态压缩dp)
- 【汉密尔顿、DP|状态压缩】POJ-2288 Islands and Bridges
- poj 2836 Rectangular Covering(状态压缩dp)
- poj 1185 状态压缩dp-炮兵阵地
- poj 3254 corn fields (状态压缩dp~)
- POJ3311——Hie with the Pie(状态压缩DP)
- poj 2411 Mondriaan's Dream_状态压缩dp
- POJ 3311 Hie with the Pie(状态压缩dp)
- poj 2411/hdu 1400 Mondriaan's Dream 状态压缩dp
- POJ 1038 Bugs Integrated(状态压缩DP)