模拟题,就是细心就好了~
八个方向都试一遍,途中遇到敌方则失败,最后的落点为己方则失败。
最后要求字典序输出,得排一下序。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
char a[10][10]; //棋盘
char q[2]; //下棋的一方
int hang[8]; //提前把每行的棋数保存
int lie[8]; //提前把每列的棋数保存
int s; //保存有几个输出
struct node{//为了排序写了个结构体
char a[8];
bool operator < (const node& t)const{
return strcmp(a,t.a)>0?false:true;
}
}ans[10000];
void find(int x,int y,int l,int r,int step);
int main(){
int time = 0; //每两个答案之间一个空行
while(1){
for(int i=0;i<8;i++){
if(scanf("%s",a[i])==EOF) return 0;
}
scanf("%s",q);
if(time!=0) printf("\n");
memset(hang,0,sizeof(hang));
memset(lie,0,sizeof(lie));
for(int i=0;i<8;i++){ //预处理
for(int j=0;j<8;j++){
if(a[i][j]!='.'){
hang[i]++;
lie[j]++;
}
}
}
s = 0;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(a[i][j]==q[0]){
find(i,j,0,-1,hang[i]); //向左
find(i,j,0,1,hang[i]); //向右
find(i,j,-1,0,lie[j]); //向上
find(i,j,1,0,lie[j]); //向下
int t = 0; //对角线
for(int x=i,y=j;x>=0&&y>=0;x--,y--){
if(a[x][y]!='.') t++;
}
for(int x=i+1,y=j+1;x<8&&y<8;x++,y++){
if(a[x][y]!='.') t++;
}
find(i,j,-1,-1,t); //斜向左上
find(i,j,1,1,t); //斜向右下
t = 0;//对角线
for(int x=i,y=j;x>=0&&y<8;x--,y++){
if(a[x][y]!='.') t++;
}
for(int x=i+1,y=j-1;x<8&&y>=0;x++,y--){
if(a[x][y]!='.') t++;
}
find(i,j,1,-1,t); //斜向左下
find(i,j,-1,1,t); //斜向右上
}
}
}
if(s==0) printf("No moves are possible\n");
else{
sort(ans,ans+s); //排序输出
for(int i=0;i<s;i++){
printf("%s\n",ans[i].a);
}
}
time++;
}
return 0;
}
void find(int x,int y,int l,int r,int step){
int i=x+l,j=y+r; //从当前点的下一个点开始
int t = 1;
for(;i>=0&&i<8&&j>=0&&j<8&&t<step;i+=l,j+=r){ //若点在棋盘之外或为敌方返回
if(!(a[i][j]=='.'||a[i][j]==a[x][y])) return;
t++;
}
if(i>=0&&i<8&&j>=0&&j<8&&t==step){ //最后一个点特判
if(!(a[i][j]==a[x][y])){ //若不为己方,成立
ans[s].a[0] = 'A'+x;
ans[s].a[1] = y+1+'0';
ans[s].a[2] = '-';
ans[s].a[3] = 'A'+i;
ans[s].a[4] = j+1+'0';
ans[s].a[5] = 0;
s++;
}
}
}