lightoj 1061 状态压缩DP
来源:互联网 发布:电子数据交换应用流程 编辑:程序博客网 时间:2024/06/08 10:13
题意:八皇后的改编,将所给棋盘上分布的棋子变成非攻击型的分布所需要的最小步数
思路:首先深搜出所有可能的分布,然后用状态压缩对每种分布求最小值,最后求总的最小值
ps:这里有个假设,就是如果匹配完之后,一定可以有个顺序使得每个皇后都可以顺利走到匹配的位置而不被其他皇后挡住。我的理解是这样:如果要走的一个匹配被其他某个皇后挡住了,那么会分为两种情况:1 挡道的皇后是移动之后的 解决这个就是交换他们的移动顺序。 2 挡道的皇后是移动之前 依然是交换他们的移动顺序。并且这里不会出现A移动之前挡住B,移动之后依然挡住B的情况,否则就交换他们的目标位置(不会增加步数,画图可以看出)。
AC代码如下:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define MAX 0x3f3f3f3fstruct Node{ int x, y;};Node node[100][10], start[10], tt[10];char s[10];int cnt;int dp[10][1<<10];bool judge( int x, int y ){ for( int i = 0; i < x; i++ ){ if( tt[i].y == y || abs( i - x ) == abs( tt[i].y - y ) ){ return false; } } return true;}int getdis( int a, int b, int id ){ int ans = 0; if( start[a].x == node[id][b].x && start[a].y == node[id][b].y ){ ans = 0; }else if( start[a].x == node[id][b].x || start[a].y == node[id][b].y || abs( start[a].x - node[id][b].x ) == abs( start[a].y - node[id][b].y ) ){ ans = 1; }else{ ans = 2; }/* int t1=abs(start[a].y - node[id][b].y); int t2=abs(start[a].x - node[id][b].x); int res=0; if(min(t1,t2)) res++; if(abs(t1-t2)) res++;*/ return ans;}int DP( int id, int pos, int st ){ if( pos < 0 ){ return 0; } if( dp[pos][st] != -1 ){ return dp[pos][st]; } int ans = MAX; for( int i = 0; i < 8; i++ ){ if( ( 1 << i ) & st ){ ans = min( ans, DP( id, pos - 1, st - ( 1 << i ) ) + getdis( pos, i, id ) ); } } return dp[pos][st] = ans;}void DFS( int deep ){ if( deep >= 8 ){ for( int i = 0; i < 8; i++ ){ node[cnt][i].x = tt[i].x; node[cnt][i].y = tt[i].y; } cnt++; return; } tt[deep].x = deep; for( int j = 0; j < 8; j++ ){ if( judge( deep, j ) ){ tt[deep].y = j; DFS( deep + 1 ); } }}int main(){ int T, Case = 1; cnt = 0; DFS( 0 ); scanf( "%d", &T ); while( T-- ){ int temp = 0; for( int i = 0; i < 8; i++ ){ scanf( "%s", s ); for( int j = 0; j < 8; j++ ){ if( s[j] == 'q' ){ start[temp].x = i; start[temp++].y = j; } } } int ans = MAX; for( int i = 0; i < cnt; i++ ){ memset( dp, -1, sizeof( dp ) ); ans = min( ans, DP( i, 7, ( 1 << 8 ) - 1 ) ); } printf( "Case %d: %d\n", Case++, ans ); } return 0;}/*3qqq..........qqq........qq......................................qq.....q...............q........q..q......q..............q....................q..............q...q......q..q......qqq...........665*/
0 0
- lightoj 1061 状态压缩DP
- lightoj 1086 状态压缩DP
- lightoj 1119 状态压缩DP
- lightoj 1092 状态压缩DP
- lightoj 1158 状态压缩+数位DP
- lightoj 1011 (状态压缩dp)
- lightoj 1018 (状态压缩DP)
- LightOJ - 1018 Brush (IV)(状态压缩DP)
- lightoj 1011 - Marriage Ceremonies(状态压缩dp)
- LightOJ 1018 Brush (IV)(状态压缩DP)
- LightOJ-1011-Marriage Ceremonies [状态压缩][DP]
- lightoj 1011 Marriage Ceremonies (状态压缩dp~)
- lightoj 1037 Agent-47(状态压缩dp~)
- 状态压缩dp
- pku1038状态压缩dp
- 状态压缩DP 入门
- HDU1074 状态压缩DP
- Poj3254 状态压缩DP
- unity3D游戏开发实战原创视频讲座系列11之相扑游戏开发并发布到Win\WP8
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- HDU1084--What Is Your Grade?
- 30岁后你会站在哪里?
- OpenCMS安装
- lightoj 1061 状态压缩DP
- centos6.x下让redis以服务方式运行
- 探索高效jQuery的奥秘
- “什么黑白”上线 APP STORE后的心得(模仿别踩白块、钢琴2)
- 获取datagridview选中行的某咧的值
- java实现快速排序
- php的yii框架
- java synchronized详解
- 拓扑排序学习http://blog.csdn.net/hcbbt/article/details/9305675