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 ...
随机推荐
- oracle 查询 约束
select * FROM all_constraints where CONSTRAINT_NAME='SYS_xxx'
- 分位函数(四分位数)概念与pandas中的quantile函数
p分位函数(四分位数)概念与pandas中的quantile函数 函数原型 DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpola ...
- Java多线程runnable
主要为大家分享Java多线程怎么实现Runnable方式 一 :主要步骤 1.定义实现Runnable接口 2.覆盖Runnable接口中run方法,将线程要运行的代码存在run方法里 3.用Thre ...
- Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何
A. Crazy Town 题目连接: http://codeforces.com/contest/498/problem/A Description Crazy Town is a plane on ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- WAP2.0(XHTML MP)基础介绍
(一)XHTML MP 介绍XHTML MP(eXtensible HyperText Markup Language Mobile Profile)WAP2.0与WCSS(WAP CSS /WAP ...
- WINUSB Descriptors
Reading string descriptors: String (0x01): "Travis Robinson" String (0x02): "Benchmar ...
- springcloud 分布式服务跟踪sleuth+zipkin
原文:https://www.jianshu.com/p/6ef0b76b9c26 分布式服务跟踪需求 随着分布式服务越来越多,调用关系越来越复杂,组合接口越来越多,要进行分布式服务跟踪监控的需求也越 ...
- cocos2d-x 输出debug信息
cocos2d-x 输出debug信息 在Classes目录下添加文件AppDef.h #ifndef _APP_DEF_H_#define _APP_DEF_H_ #include <an ...
- 用Service实现断点下载
整体的思路: 在下载文件时,将进度写入数据库,同一时候通知该ContentProvider的观察者更新页面,这个通知过程不要太频繁.我设置了10次,否则页面会灰常卡. 假设异常中断(网络中断或程 ...