问题描述 任务很简单. 确定国际象棋棋盘上处于骑士攻击之下的格子个数. 棋盘上没有其它棋子. 骑士的走法: 横 (纵)向走两个格, 再纵(横)向走一个格(类似于中国象棋中的马). 输入 第一行为测试次数N, 1 ≤ N ≤ 100. 后面N行每行包含一个坐标表示骑士的位置. 字母表示横向位置, 数字表示纵向位置. 输出 输出N行. 每行一个整数, 表示骑士可攻击的格子个数. 输入样例 3a1d4g6 输出样例 286 // 注意国际象棋的棋盘为 8*8 的矩阵 #include <stdio.h>#include <stdlib.h>int main(){ int k,n,i,x,y; int b[] = {-2,-1,1,2,2,1,-1,-2}; int c[] = {1,2,2,1,-1,-2,-2,-1}; char str[4]; scanf("%d",&n); for(; n > 0; n--){ scanf("%s",str); y = tolower(str[0]) - 'a'; x = str[1] - '1'; for(k = 0,i = 0; i < 8; i++){ if(x+c[i]<0 || x+c[i]>7 || y+b[i]<0 || y+b[i]>7) continue; k++; } printf("%d\n",k); } return 0;}

评论