在一個n*m的棋盤上,你有一個white king,然後還有一些(<15)個黑子,每個黑子有一定的攻擊範圍並且他們不會移動。求吃掉所有黑子的最少步數。
黑子的個數很少,所以用狀態壓縮來表示棋盤上還剩餘哪些黑子。在bfs前先要初始化所有黑子狀態下的受攻擊的點,這個很噁心。。。初始化完了基本就是無腦搜了吧?
#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<cstdlib>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define MP make_pair
using namespace std;
int dwx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int dwy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dkx[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int dky[8] = {2, 1, -1, -2, -2, -1, 1, 2};
const int SET = (1<<15);
const int maxn = 20;
//can[sta][i][j] = 1 : 當前剩餘sta中的黑子 點<i, j>不可走
//vis[sta][i][j] = 1 :當前剩餘sta中的黑子時,點<i, j>已走過
bool can[SET][maxn][maxn], vis[SET][maxn][maxn];
char g[maxn][maxn];
//id[x][y] : 點<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<<id[x][y]));
}
struct Node
{
int x, y, st, steps;
Node(){}
Node(int a, int b, int c, int d):x(a), y(b), st(c), steps(d){}
};
void bfs()
{
queue<Node> 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;
//如果當前狀態 包含點<tx, ty>的黑子
if(has(T.st, tx, ty))
{
int sta = T.st&(tot-(1<<id[tx][ty]));
if(!vis[sta][tx][ty] && !can[sta][tx][ty])
//如果可以吃掉它
{
q.push(Node(tx, ty, sta, T.steps+1));
vis[sta][tx][ty] = 1;
}
}
else if(!can[T.st][tx][ty])
{
int sta = T.st;
if(!vis[sta][tx][ty])
{
q.push(Node(tx, ty, sta, T.steps+1));
vis[sta][tx][ty] = 1;
}
}
}
}
puts("-1");
return ;
}
//初始化所有狀態下 會被攻擊的點
void init()
{
FF(s, 1, tot+1)
{
//如果s狀態下的黑子中 包含g[i][j] 那麼更新g[i][j]的攻擊範圍
REP(i, n) REP(j, m) if(isalpha(g[i][j]) && (s & (1<<id[i][j])))
{
if(g[i][j] == 'K')
{
REP(k, 8)
{
int tx = i + dkx[k], ty = j + dky[k];
if(outside(tx, ty) || has(s, tx, ty)) continue;
can[s][tx][ty] = 1;
}
}
else if(g[i][j] == 'B')
{
int tx = i, ty = j;
while(true)
{
tx++, ty++;
if(outside(tx, ty) || has(s, tx, ty)) break;
can[s][tx][ty] = 1;
}
tx = i, ty = j;
while(true)
{
tx--, ty--;
if(outside(tx, ty) || has(s, tx, ty)) break;
can[s][tx][ty] = 1;
}
tx = i, ty = j;
while(true)
{
tx++, ty--;
if(outside(tx, ty) || has(s, tx, ty)) break;
can[s][tx][ty] = 1;
}
tx = i, ty = j;
while(true)
{
tx--, ty++;
if(outside(tx, ty) || has(s, tx, ty)) break;
can[s][tx][ty] = 1;
}
}
else
{
int tx = i, ty = j;
while(true)
{
tx++;
if(tx >= 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<<tot)-1;
init();
bfs();
return 0;
}