Two chess pieces, a rook and a knight, stand on a standard chessboard 8 × 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one.
Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board beat another one. A new piece can only be placed on an empty square.
The first input line contains the description of the rook's position on the board. This description is a line which is 2 in length. Its first symbol is a lower-case Latin letter from a to h, and its second symbol is a number from 1 to 8. The second line contains the description of the knight's position in a similar way. It is guaranteed that their positions do not coincide.
Print a single number which is the required number of ways.
a1 b2
44
a8 d4
38
题意:
已知有一个8*8的棋盘,棋盘上有一个车和一个马,两个棋子不会吃到另一个棋子,给你两个棋子在棋盘上的位置,求在该棋盘上再放置一个马,使得三个棋子都不会吃到其他棋子有几种放置的方法
枚举棋盘上每一个格子,每次都判断该点放入马后是否会吃到另外两个棋子或被另外两个棋子吃到
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; char c1,c2; int x1,x2,y1,y2; int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1}; bool check(int x,int y){ for(int i=0;i<8;i++){ int X = x + dir[i][0]; int Y = y + dir[i][1]; if(X<1||X>8||Y<1||Y>8) continue; if((X==x1&&Y==y1)||(X==x2&&Y==y2)) return true; } return false; } int main() { scanf("%c%d\n%c%d",&c1,&y1,&c2,&y2); x1 = c1-'a'+1; x2 = c2-'a'+1; int ans = 0; for(int i=1;i<=8;i++){ if(i==x1) continue; for(int j=1;j<=8;j++){ if(j==y1||(i==x2&&j==y2)||check(i,j)) continue; ans++; } } printf("%d\n",ans); return 0; }