棋盘上白子只有一个国王  黑子给出

各子遵从国际象棋的走法

黑子不动,白子不能走进黑子的攻击范围以内

问白字能不能吃掉所有的黑子

直接搜索就好了,各子状态用二进制表示

不过每个子被吃之后攻击范围会改变

所以重点是预处理每种剩余棋子状态的攻击范围

比较麻烦,注意白子吃掉一颗子之后所在的位置也可能是危险位置

//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define CPY(a, b) memcpy(a, b, sizeof(a))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 100000000;
const double eps = 1e-10;
const int maxn = 16;
const LL MOD = 1e9 + 7; const int k = 1, b = 2, r = 3; ////表示棋子种类
int n, m, sx, sy, cnt, all;
bool danger[maxn][maxn][1 << 15], vis[maxn][maxn][1 << 15];
int dirk[][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1},
{2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
int dir[][2] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1},
{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct node{
int x, y, type;
}pie[maxn];
struct state{
short int x, y, key;
int step;
state(int a, int b, int c, int d) : x(a), y(b), key(c), step(d){}
}; char mat[maxn][maxn];
int num[maxn][maxn]; int bfs()
{
int tot = all - 1;
CLR(vis, 0);
vis[sx][sy][0] = 1;
queue<state> Q;
state t(sx, sy, 0, 0);
Q.push(t);
while (!Q.empty())
{
t = Q.front(); Q.pop();
if (t.key == tot)
return t.step;
REP(i, 8)
{
int nx = t.x + dir[i][0], ny = t.y + dir[i][1], key = t.key;
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (num[nx][ny] != -1) ///若这个位置是棋子
key |= (1 << num[nx][ny]);
if (danger[nx][ny][key]) continue; ///key要是更新后的状态
if (!vis[nx][ny][key])
{
vis[nx][ny][key] = 1;
state heh(nx, ny, key, t.step + 1);
Q.push(heh);
}
}
}
return -1;
} void solve()
{
all = 1 << cnt;
CLR(danger, 0);
int x, y;
REP(i, cnt)
{
int s0 = 1 << i;
if (pie[i].type == k)
{
REP(j, 8)
{
x = pie[i].x + dirk[j][0], y = pie[i].y + dirk[j][1];
if (x < 0 || x >= n || y < 0 || y >= m) continue;
REP(s0, all)
if ((s0 & (1 << i)) == 0)
danger[x][y][s0] = 1;
}
}
else if (pie[i].type == b)
{
REP(j, 4)
{
int st = 1;
while (1)
{
x = pie[i].x + dir[j][0] * st, y = pie[i].y + dir[j][1] * st;
if (x < 0 || x >= n || y < 0 || y >= m) break;
REP(s0, all)
if ((s0 & (1 << i)) == 0)
danger[x][y][s0] = 1;
st++;
if (mat[x][y] != '.') break; ///这里注意,先是标记这个点,再结束
}
}
}
else if (pie[i].type == r)
{
FF(j, 4, 8)
{
int st = 1;
while (1)
{
x = pie[i].x + dir[j][0] * st, y = pie[i].y + dir[j][1] * st;
if (x < 0 || x >= n || y < 0 || y >= m) break;
REP(s0, all)
if ((s0 & (1 << i)) == 0)
danger[x][y][s0] = 1;
st++;
if (mat[x][y] != '.') break;
}
} }
}
WI(bfs());
} int main()
{
while (~RII(n, m))
{
cnt = 0;
CLR(num, -1);
REP(i, n)
{
RS(mat[i]);
REP(j, m)
{
if (mat[i][j] == '*') sx = i, sy = j, mat[i][j] = '.';
else if (mat[i][j] == 'K') num[i][j] = cnt, pie[cnt].x = i, pie[cnt].y = j, pie[cnt].type = k, cnt++;
else if (mat[i][j] == 'B') num[i][j] = cnt, pie[cnt].x = i, pie[cnt].y = j, pie[cnt].type = b, cnt++;
else if (mat[i][j] == 'R') num[i][j] = cnt, pie[cnt].x = i, pie[cnt].y = j, pie[cnt].type = r, cnt++;
}
}
solve();
}
}

SGU536 Berland Chess的更多相关文章

  1. NEERC Southern Subregional 2011

    NEERC Southern Subregional 2011 A - Bonnie and Clyde solution 双指针搞搞就好. 时间复杂度:\(O(n)\) B - Building F ...

  2. Codeforces Beta Round #7 A. Kalevitch and Chess 水题

    A. Kalevitch and Chess 题目连接: http://www.codeforces.com/contest/7/problem/A Description A famous Berl ...

  3. 7A - Kalevitch and Chess

    A. Kalevitch and Chess time limit per test 2 seconds memory limit per test 64 megabytes input standa ...

  4. hdu4405 Aeroplane chess

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 5742 Chess SG函数博弈

    Chess Problem Description   Alice and Bob are playing a special chess game on an n × 20 chessboard. ...

  6. POJ2425 A Chess Game[博弈论 SG函数]

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3917   Accepted: 1596 Desc ...

  7. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  8. HDU 4832 Chess (DP)

    Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

随机推荐

  1. FreeMarker快速入门

    虽然当前比较推荐使用thymeleaf替代jsp作为java网页开发的模板语言,不过公司推荐使用freemarker,那就顺势而为,速度学一发,然后迅速开始新项目了. 简介 FreeMarker第一个 ...

  2. opesntack 底层共享存储 迁移配置

    底层共享存储在迁移配置: 每台compute 节点都需要配置一下 让nova用户可以登陆 usermod -s /bin/bash nova 设置nova 用户密码 echo "nova&q ...

  3. 吴恩达-coursera-机器学习-week9

    十五.异常检测(Anomaly Detection) 15.1 问题的动机 15.2 高斯分布 15.3 算法 15.4 开发和评价一个异常检测系统 15.5 异常检测与监督学习对比 15.6 选择特 ...

  4. sqlserver -- 查看当前数据库的数据表(备忘)

    @_@||... 记性不好,备忘... 语句功能:查看当前数据库的所有表(根据所需,进行语句改写即可) SELECT * FROM sysobjects WHERE (xtype = 'U') 备注: ...

  5. 喵哈哈村的魔法考试 Round #4 (Div.2) 题解

    有任何疑问,可以加我QQ:475517977进行讨论. A 喵哈哈村的嘟嘟熊魔法(1) 题解 这道题我们只要倒着来做就可以了,因为交换杯子是可逆的,我们倒着去模拟一遍就好了. 有个函数叫做swap(a ...

  6. hdu 5781 ATM Mechine dp

    ATM Mechine 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  7. FireDAC 下的 Sqlite [2] - 第一个例子

    为了方便测试, 我把官方提供的 C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\data\FDDemo.sdb 复制了一份到 C:\ ...

  8. Java中的锁(转)

    Java中的锁 锁像synchronized同步块一样,是一种线程同步机制,但比Java中的synchronized同步块更复杂.因为锁(以及其它更高级的线程同步机制)是由synchronized同步 ...

  9. SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法

    今天看到这篇文章:没有了SA密码,无法Windows集成身份登录,DBA怎么办? 想起来之前着急哥问我的一个问题,一个DBA删除了Windows登录用户,而且SQLSERVER服务器的验证方式是Win ...

  10. 【Unity 3D】碰撞检测

    在unity3d中,能检测碰撞发生的方式有两种, 碰撞器 触发器 概念:     (一)碰撞器是一群组件,它包含了很多种类,比如:Box Collider,Capsule Collider等,这些碰撞 ...