SGU 536 Berland Chess(状态压缩 + bfs)

2014-11-24 00:33:31 · 作者: · 浏览: 3
在一个n*m的棋盘上,你有一个white king,然后还有一些(<15)个黑子,每个黑子有一定的攻击范围并且他们不会移动。求吃掉所有黑子的最少步数。
黑子的个数很少,所以用状态压缩来表示棋盘上还剩余哪些黑子。在bfs前先要初始化所有黑子状态下的受攻击的点,这个很恶心。。。初始化完了基本就是无脑搜了吧?
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#define FF(i, a, b) for(int i=a; i=b; i--)  
#define REP(i, n) for(int i=0; i不可走  
//vis[sta][i][j] = 1 :当前剩余sta中的黑子时,点已走过  
bool can[SET][maxn][maxn], vis[SET][maxn][maxn];  
char g[maxn][maxn];  
//id[x][y] : 点的黑子对应的标号  
int n, m, sx, sy, tot, id[maxn][maxn];  
  
inline bool outside(int x, int y)  
{  
    return x<0 || x>=n || y<0 || y>=m;  
}  
//当前剩余sta中的黑子 点x y是否在sta中  
inline bool has(int sta, int x, int y)  
{  
    return isalpha(g[x][y]) && (sta & (1< q; q.push(Node(sx, sy, tot, 0));  
    vis[tot][sx][sy] = 1;  
    while(!q.empty())  
    {  
        Node T = q.front(); q.pop();  
        if(T.st == 0)  
        {  
            printf("%d\n", T.steps);  
            return ;  
        }  
        REP(i, 8)  
        {  
            int tx = T.x + dwx[i], ty = T.y + dwy[i];  
            if(outside(tx, ty)) continue;  
            //如果当前状态 包含点的黑子   
            if(has(T.st, tx, ty))  
            {  
                int sta =  T.st&(tot-(1<
= n || has(s, tx, ty)) break; can[s][tx][ty] = 1; } tx = i, ty = j; while(true) { tx--; if(tx < 0 || has(s, tx, ty)) break; can[s][tx][ty] = 1; } tx = i, ty = j; while(true) { ty++; if(ty>=m || has(s, tx, ty)) break; can[s][tx][ty] = 1; } tx = i, ty = j; while(true) { ty--; if(ty<0 || has(s, tx, ty)) break; can[s][tx][ty] = 1; } } } } } int main() { scanf("%d%d", &n, &m); tot = 0; REP(i, n) { scanf("%s", g[i]); REP(j, m) { if(g[i][j] == '*') sx = i, sy = j; else if(isalpha(g[i][j])) id[i][j] = tot++; } } if(tot == 0) { puts("0"); return 0; } tot = (1<