lightoj 1061 状态压缩DP

题意:八皇后的改编,将所给棋盘上分布的棋子变成非攻击型的分布所需要的最小步数

思路:首先深搜出所有可能的分布,然后用状态压缩对每种分布求最小值,最后求总的最小值

ps:这里有个假设,就是如果匹配完之后,一定可以有个顺序使得每个皇后都可以顺利走到匹配的位置而不被其他皇后挡住。我的理解是这样:如果要走的一个匹配被其他某个皇后挡住了,那么会分为两种情况:1 挡道的皇后是移动之后的 解决这个就是交换他们的移动顺序。  2 挡道的皇后是移动之前 依然是交换他们的移动顺序。并且这里不会出现A移动之前挡住B,移动之后依然挡住B的情况,否则就交换他们的目标位置(不会增加步数,画图可以看出)。

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX 0x3f3f3f3f

struct 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;
}
/*
3

qqq.....
.....qqq
........
qq......
........
........
........
........

qq.....q
........
.......q
........
q..q....
..q.....
........
.q......

........
......q.
........
.....q..
.q......
q..q....
..qqq...
........

6
6
5
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值