SGU536 Berland Chess
棋盘上白子只有一个国王 黑子给出
各子遵从国际象棋的走法
黑子不动,白子不能走进黑子的攻击范围以内
问白字能不能吃掉所有的黑子
直接搜索就好了,各子状态用二进制表示
不过每个子被吃之后攻击范围会改变
所以重点是预处理每种剩余棋子状态的攻击范围
比较麻烦,注意白子吃掉一颗子之后所在的位置也可能是危险位置
//#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的更多相关文章
- NEERC Southern Subregional 2011
NEERC Southern Subregional 2011 A - Bonnie and Clyde solution 双指针搞搞就好. 时间复杂度:\(O(n)\) B - Building F ...
- Codeforces Beta Round #7 A. Kalevitch and Chess 水题
A. Kalevitch and Chess 题目连接: http://www.codeforces.com/contest/7/problem/A Description A famous Berl ...
- 7A - Kalevitch and Chess
A. Kalevitch and Chess time limit per test 2 seconds memory limit per test 64 megabytes input standa ...
- hdu4405 Aeroplane chess
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 5742 Chess SG函数博弈
Chess Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. ...
- POJ2425 A Chess Game[博弈论 SG函数]
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3917 Accepted: 1596 Desc ...
- 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 ...
- HDU 4832 Chess (DP)
Chess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
随机推荐
- .NET工作准备--02基础知识
(已过时) 框架基础,语法基础,字符串&集合&流,常见类和接口; 02.net基础(重点) -第一部分 框架基础 1.基础概念 CTS(Common Type System),CLS( ...
- linux学习笔记-2.常用命令
说明:安装linux时,创建一个luao用户,然后使用root用户登陆系统 1.进入到用户根目录 cd ~ 或 cd cd / 返回到根目录 2.查看当前所在目录 pwd 3.进入到luao用户根目录 ...
- spring RequestMapping 包含http的部分以及各部分详解
@RequestMapping有四个参数,分别为:1.value:表示请求URL2.method:表示请求方法3.params:表示请求参数4.headers:表示请求头 [例如]:@RequestM ...
- BZOJ.4939.[Ynoi2016]掉进兔子洞(莫队 bitset 分组询问)
BZOJ 洛谷 删掉的数即三个区间数的并,想到bitset:查多个区间的数,想到莫队. 考虑bitset的每一位如何对应每个数的不同出现次数.只要离散化后不去重,每次记录time就可以了. 但是如果对 ...
- BZOJ.3990.[SDOI2015]排序(DFS)
题目链接 操作序列的顺序显然是无关的,所以只需按特定顺序求出一个长度为\(l\)的操作序列,它对答案的贡献为\(l!\). 我们从小到大枚举所有选择.若当前为第\(i\)个,如果有一段长度为\(2^i ...
- Xtreme8.0 - Sum it up 水题
Sum it up 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/sum-it-up Descr ...
- 理解JVM模型
概括 JVM运行时数据区可以划分为5部分,分别是:程序计数器.虚拟机栈.本地方法栈.堆.方法区 程序计数器(Program Counter Register) 相当于当前线程所执行字节码的行号指示器. ...
- IntraWeb XIV 类型速查表
tkClass ================== IWUserSessionBase.TIWUserSessionBase < TDataModule < TComponent < ...
- java容器类---概述
1.容器类关系图 虚线框表示接口. 实线框表示实体类. 粗线框表示最经常使用的实体类. 点线的箭头表示实现了这个接口. 实线箭头表示类能够制造箭头所指的那个类的对象. Java集合工具包位于Java. ...
- ASP.NET Web API实践系列01,以ASP.NET Web Form方式寄宿
创建一个空的ASP.NET Web Form项目. 右键项目,添加新项,创建Web API控制器类,TestController. 删除掉TestController默认的内容,编写如下: using ...