The only king stands on the standard chess board. You are given his position in format "cd", wherec is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king.
Check the king's moves here https://en.wikipedia.org/wiki/King_(chess).

King moves from the position e4
The only line contains the king's position in the format "cd", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'.
Print the only integer x — the number of moves permitted for the king.
e4
8
解:国际象棋中的王可以上下左右斜方向进行移动,给定两个字符代表王所在的位置,加之判断四个特殊情况即可。
#include<stdio.h>
#include<string.h>
int main()
{
char a[2];
while(gets(a)){
int x=8;
if(a[0]=='a' || a[0]=='h'){
x-=3;
}
if(a[1]=='1' || a[1]=='8'){
x-=3;
}
if((a[0]=='a' && a[1]=='1') || (a[0]=='a' && a[1]=='8') ||(a[0]=='h' && a[1]=='1') || (a[0]=='h' && a[1]=='8')){
x+=1;
}
printf("%d\n",x);
}
return 0;
}

本文介绍了一个简单的程序,用于计算国际象棋中王从特定位置出发可能的移动数量。通过输入王的位置坐标,程序将输出允许的移动次数。
566

被折叠的 条评论
为什么被折叠?



