7-16 白骑士的移动 (20分)
小S第一次接触国际象棋。他发现国际象棋中的Knight棋子的移动方式和中国象棋中的马类似,移动方式如图所示。 QQ图片20191115182554.png
于是小S在棋盘上随意摆上了一些棋子,其中包括一枚白骑士、一枚黑皇后、若干黑战车和若干黑主教。
小S想知道,如何能在避开黑战车和黑主教的攻击范围的前提下,花费更少的步数吃掉黑皇后。
注1:战车的攻击范围呈直线,和中国象棋的車类似;主教的攻击范围呈斜线,无障碍物情况下可无限延伸。
注2:白骑士只能吃黑皇后,不可以吃掉黑战车和黑主教。
输入格式:
输入仅包含一组样例。
一组样例包含8行(分别对应1-8行),每行包含8个字符,每个字符代表对应行对应列的棋盘格子状况。
其中’ . ‘代表格子上没有摆放任何棋子;’ K '代表格子上摆放的是白骑士; ’ Q '代表格子上摆放的是黑皇后; ’ R '代表格子上摆放的是黑战车; ’ B '代表格子上摆放的是黑主教。
注:题目保证白骑士的初始位置不在黑战车和黑主教的攻击范围内。
输出格式:
如果白骑士可以在避开黑战车和黑主教的攻击的情况下吃掉黑皇后,则输出花费步数的最小值;否则输出"Checkmate"。
输入样例1:
R.B.QB.R
........
........
........
........
........
........
.K......
输出样例1:
4
输入样例2:
....RR.Q
........
.K......
........
........
........
........
........
输出样例2:
Checkmate
没什么好说的,预处理棋盘,每个点8中可能,广度搜索遍历,顺便一提深度搜索应该也可以,不管怎么说这道题代码较长,好在马的移动的大部分代码相同,略微修改即可.预处理黑车时需要注意的地方是要从中间向两边走,不能直接从左到右遍历,上下也一样.黑主教就用四个循环处理左上左下,右上,右下四个方向即可.
//
// Created by HMN on 2020/1/27.
//
#include <iostream>
#include <queue>
using namespace std;
struct point {
int x, y;
};
char chess[10][10];
bool inq[10][10];
int min1 = 0x3fffffff;
int level[10][10];
bool bfs(point s) {
queue<point> q;
q.push(s);
inq[s.x][s.y] = true;
level[s.x][s.y] = 1;
point tmp;
while (!q.empty()) {
point pro = q.front();
q.pop();
int x = pro.x, y = pro.y;
if (chess[x][y] == 'Q') {
min1 = level[x][y];
return true;
}
if (x - 2 > 0 && y - 1 > 0) {
int x2 = x - 2, y2 = y - 1;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x - 1 > 0 && y - 2 > 0) {
int x2 = x - 1, y2 = y - 2;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x + 1 < 9 && y - 2 > 0) {
int x2 = x + 1, y2 = y - 2;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x + 2 < 9 && y - 1 > 0) {
int x2 = x + 2, y2 = y - 1;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x + 2 < 9 && y + 1 < 9) {
int x2 = x + 2, y2 = y + 1;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x + 1 < 9 && y + 2 < 9) {
int x2 = x + 1, y2 = y + 2;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x - 2 > 0 && y + 1 < 9) {
int x2 = x - 2, y2 = y + 1;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
if (x - 1 > 0 && y + 2 < 9) {
int x2 = x - 1, y2 = y + 2;
if (chess[x2][y2] == '.'||chess[x2][y2]=='Q') {
if (!inq[x2][y2]) {
tmp = {x2, y2};
q.push(tmp);
inq[x2][y2] = true;
level[x2][y2] = level[x][y] + 1;
}
}
}
}
return false;
}
int main(int argc, char **argv) {
point horse;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
chess[i][j] = '#';
}
}
for (int i = 1; i < 9; ++i) {
for (int j = 1; j < 10; ++j) {
chess[i][j] = getchar();
if (chess[i][j] == 'K') {
horse = {i, j};
}
if (j == 9)
chess[i][j] = '#';
}
}
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) { //预处理
if (chess[i][j] == 'R') {
int l=j-1,r=j+1,up=i-1,down=i+1;
while(chess[i][l]=='.'||chess[i][l]=='*'){
chess[i][l]='*';
l--;
}
while(chess[i][r]=='.'||chess[i][r]=='*'){
chess[i][r]='*';
r++;
}
while(chess[up][j]=='.'||chess[up][j]=='*'){
chess[up--][j]='*';
}
while(chess[down][j]=='.'||chess[down][j]=='*'){
chess[down++][j]='*';
}
/*for (int k = 1; k < 9; ++k) {
if (flag1 && (chess[i][k] == '.' || chess[i][k] == '*'))
chess[i][k] = '*';
else
flag1 = false;
if (flag2 && (chess[k][j] == '.' || chess[k][j] == '*'))
chess[k][j] = '*';
else
flag2 = false;
}*/
}
int i2 = i, j2 = j;
if (chess[i2][j2] == 'B') {
while (chess[i2 - 1][j2 - 1] == '.' || chess[i2 - 1][j2 - 1] == '*') {
chess[i2 - 1][j2 - 1] = '*';
i2--;
j2--;
}
i2 = i, j2 = j;
while (chess[i2 + 1][j2 + 1] == '.' || chess[i2 + 1][j2 + 1] == '*') {
chess[i2 + 1][j2 + 1] = '*';
i2++;
j2++;
}
i2 = i, j2 = j;
while (chess[i2 - 1][j2 + 1] == '.' || chess[i2 - 1][j2 + 1] == '*') {
chess[i2 - 1][j2 + 1] = '*';
i2--;
j2++;
}
i2 = i, j2 = j;
while (chess[i2 + 1][j2 - 1] == '.' || chess[i2 + 1][j2 - 1] == '*') {
chess[i2 + 1][j2 - 1] = '*';
i2++;
j2--;
}
}
}
}/*
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
cout << chess[i][j];
}
cout << endl;
}*/
if(bfs(horse)){
cout<<min1-1;
} else{
cout<<"Checkmate";
}
return 0;
}