中国象棋
中国象棋博大精深,其中马的规则最为复杂,也是最难操控的一颗棋子。
我们都知道象棋中马走"日",比如在 (2,4) 位置的一个马,跳一步能到达的位置有 (0,3),(0,5),(1,2),(1,6),(3,2),(3,6),(4,3),(4,5)。
蒜头君正在和花椰妹下棋,蒜头君正在进行战略布局,他需要把在 (x,y) 位置的马跳到 (x′,y′) 位置,以达到威慑的目的。
但是棋盘大小有限制,棋盘是一个 10×9 的网格,左上角坐标为 (0,0),右下角坐标为 (9,8),马不能走出棋盘,并且有些地方已经有了棋子,马也不能跳到有棋子的点。
蒜头君想知道,在不移动其他棋子的情况下,能否完成他的战略目标。
#include <iostream>
#include <string>
using namespace std;
string s[10];
bool vis[110][110];
int n = 10;
int m = 9;
int dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,+2},{2,-1},{2,1}};
bool in(int x, int y) {
return 0 <= x && x <= 9 && 0 <= y && y <= 8;
}
bool dfs(int x,int y){
if(s[x][y] == 'T'){
return true;
}
vis[x][y] = 1;
s[x][y] = 'm';
for(int i=0;i<8;i++){
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(in(tx,ty) && s[tx][ty] != '#' && !vis[tx][ty]){
if(dfs(tx,ty)){
return true;
}
}
}
vis[x][y] = 0;
s[x][y] = '.';
return false;
}
int main(){
for(int i=0;i<10;i++){
cin >> s[i];
}
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'S') {
x = i, y = j;
break;
}
}
}
//cout << "x:" << x << " y:" << y << endl;
if (dfs(x, y)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}