1342: Two Semiknights Meet

Time Limit: 1 Sec  Memory Limit: 128 MB

[​​Submit​​​][​​Status​​​][​​Web Board​​]

Description

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.

Sample Input

2

........

........

......#.

K..##..#

.......#

...##..#

......#.

K.......

........

....... .

..#.... .

..#..#. .

..####. .

...##.. .

....... .

....K#K#

Sample Output

YES

NO

【分析】

题意就是两个骑士在K的位置,行动方式是“田”字型,长度为4,问两个骑士能不能见面。

1.最基本的dfs就是两个骑士同时走去搜...不用说肯定超时了

2.稍微加强一点版本的dfs就是去搜两个骑士能到达的点,如果某个点两个骑士都可以到达,那么就“YES”,因为棋盘规定了8*8,判断能否到达的dfs时间复杂度不高,所以肯定能过(但是我没写过这个方法...只是提一下也可以过)

3.我认为的正确解法是去判断两个骑士所在位置,因为规定了每次的行走方式,所以,如果两个骑士的横坐标差和纵坐标差都必须为4的倍数。所以直接判断即可,一开始的时候我认为这个方法还需要去判断到达点是不是墙,后来发现并不需要这个判断。

因为如果两个骑士能同时到达一个墙的位置,那么说明两个骑士出生点这两个点之间一定联通,那么倒推回到某一个骑士的出生点,就可以让这两个骑士见面。(这一点最早的时候没理解。。墙的存在并不是说无法通过,而只是墙这个点无法开会)


【代码】

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int pp;cin>>pp;
while(pp--)
{
int x1=-1,x2,y1,y2;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
{
char s;cin>>s;
if(s=='K')
if(x1==-1)
{
x1=i;
y1=j;
}
else
{
x2=i;
y2=j;
}
}
if(abs(x1-x2)%4 == 0 && abs(y1-y2)%4==0)
cout <<"YES"<<endl;
else
cout <<"NO"<<endl;
}
return 0;
}