Two Semiknights Meet CodeForces - 362A

A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.

Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.

Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.

Please see the test case analysis.

Input

The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.

Output

For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.

Example
Input
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......

........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#
Output
YES
NO
Note

Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).

On the second board the semiknights will never meet.

题目思路:假设两个骑士横坐标的距离是a,每次移动骑士,骑士横坐标的改变为2。若两个骑士沿横坐标同一方向移动,他们横坐标的距离仍然是a,若两个骑士沿横坐标相反方向移动,他们横坐标的距离为a+4或a-4。多次移动后,他们横坐标的距离就是a±4n(a,n∈N)。他们相遇时横坐标的距离是0,也就是说,如果两个骑士可以相遇,这个a±4n=0,a=±4n,所以只有a是4的整数倍,他们才能相遇,纵坐标同理。在这里我们完全不必考虑坏的格子的问题,因为两个骑士最初的位置是好的格子,这两个骑士如果能相遇,他们一定能同时回到任意一个骑士最初的位置。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
struct node{
    int x,y;
}knight[2];
char mp[10][10];
int t;
int main(){
    scanf("%d",&t);
    getchar();
    while(t--){
        int i,j;
        int flag = 0;
        int cnt = 0;
        for(i = 0; i < 8; i++)
            gets(mp[i]);
        //记录下初始两个骑士的位置
        for(i = 0; i < 8; i++){
            for(j = 0; j < 8; j++){
                if(mp[i][j] == 'K'){
                    knight[cnt].x = i;
                    knight[cnt++].y = j;
                }
            }
        }
        //计算两个骑士横纵坐标的距离
        int abx = abs(knight[0].x - knight[1].x);
        int aby = abs(knight[0].y - knight[1].y);
        //如果横纵坐标的距离不是4的整数倍,无法相遇
        if(abx % 4 || aby % 4){
            flag = 1;
        }
        if(flag) puts("NO");
        else puts("YES");
        if(t) getchar();//如果是最后一组数据,后面没有空行的,否则有空行有读进去
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值