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 ...
随机推荐
- FreeMarker快速入门
虽然当前比较推荐使用thymeleaf替代jsp作为java网页开发的模板语言,不过公司推荐使用freemarker,那就顺势而为,速度学一发,然后迅速开始新项目了. 简介 FreeMarker第一个 ...
- 邻接矩阵实现图的存储,DFS,BFS遍历
图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...
- URAL 1963 Kite 计算几何
Kite 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/C Description Vova bought a kite con ...
- CocoaPods第三方库管理工具
http://code4app.com/article/cocoapods-install-usage
- CentOS下使用LVM进行分区(转)
说明:为什么抄,因为这篇文章图文并茂,所有测试都在CentOS 6和7测试过. 许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统 ...
- 新浪微博基于MySQL的分布式数据库实践
提起微博,相信大家都是很了解的.但是有谁知道微博的数据库架构是怎样的呢?在今天举行的2011数据库技术大会上,新浪首席DBA杨海潮为我们详细解读了新浪微博的数据库架构——基于MySQL的分布式数据库实 ...
- Training JTAG Interface
For most embedded CPU architecture implementations, the JTAG port is used by the debugger to interfa ...
- ASp.net中Froms验证方式
微软的ASP.NET提供了3种用户验证方式,即Windows验证.窗体(Forms)验证和护照验证(Passport)验证. 由于验证方式各不相同,因而这3种验证方式在使用范围上也有很大的不同, Wi ...
- Flume 1.5.0简单部署试用
================================================================================ 一.Flume简介 ========= ...
- MAC系统压缩文件传到WINDOWS下出现乱码
可能使用Mac系统的朋友,在压缩文件时遇到过这样的问题: 要给朋友传文件,而对方又是WIN系统.我们打好包传过去以后,对方解压缩发现中文文件名都成乱码了.这是怎么回事? 原来,Mac下,默认文字编码是 ...