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.
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.
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
2 ........ ........ ......#. K..##..# .......# ...##..# ......#. K....... ........ ........ ..#..... ..#..#.. ..####.. ...##... ........ ....K#K#
YES NO
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.
很好的一道找规律
题意:两个骑士分别同时从各自的位置出发走棋盘,其中有好的格子和坏的格子(都能走),他们的路径是“田”字(一开始理解错了路径),然后问是否存在让他们相遇的好格子
一开始没细想就用广搜了,,,写完后WA了,然后仔细一想,广搜根本不对题意啊,,,(这里要仔细想想广搜的本质(求的最短路径),与原题是不相符的)
规律仔细一想还是比较好找的,,,画个图(当神魔时候两个骑士一定会相遇呢,,,当横坐标还相差4或0时,如果这时他们相向而行,就能碰到一起,,,纵坐标也是如此,,,,继而想到如果相差4的倍数,,,最终也一定会遇到
还有就是坏格子的问题,感觉是混淆视听的,,,如果两个骑士相遇(不管在哪里),那么最终一定可以再同时返回到任意一个骑士最初的位置,而骑士原来的位置是好的。。。
看到了一篇解释的比较好的博客:http://blog.csdn.net/firwaless/article/details/16342823
找规律代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char map[10][10];
int main()
{
int T,i,j,flag;
int x1,y1,x2,y2;
scanf("%d",&T);
while(T--)
{
flag=0;
for(i=0; i<8; i++)
{
scanf("%s",map[i]);
for(j=0; j<8; j++)
{
if(map[i][j]=='K')
{
if(flag)
{
x1=i;
y1=j;
}
else
{
x2=i;
y2=j;
flag=1;
}
}
}
}
if((x1-x2)%4==0&&(y1-y2)%4==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
上面解释过了,不管在哪里相遇,最后一定会回到其中一个骑士最初的位置,所以我们还可以这样考虑:假设骑士1不动,骑士2走,如果骑士2可以到达骑士1,并且走了偶数步,那么一定会相遇,
因为如果可以走到,,证明骑士2的路径一定也是适合骑士1的,这样两个骑士同时从两个初始位置沿这条路径走,只要之前骑士2自己走的路径步数是偶数,那么一定会在中途相遇。。。
深搜代码:
#include <cstdio>
#include <cstring>
char map[10][10];
int vis[10][10];
int dir[4][2]={2,-2,-2,2,2,2,-2,-2};
int x1,x2,y1,y2;
int tmp;
void dfs(int x,int y,int step)
{
int i;
if(x==x2&&y==y2&&step%2==0)
{
tmp=1;
return ;
}
for(i=0;i<4;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx<0||tx>=8||ty<0||ty>=8||vis[tx][ty])
continue;
vis[tx][ty]=1;
dfs(tx,ty,step+1);
}
}
int main()
{
int i,j,T,flag;
scanf("%d",&T);
while(T--)
{
tmp=0;
flag=0;
memset(vis,0,sizeof(vis));
for(i=0;i<8;i++)
{
scanf("%s",map[i]);
for(j=0;j<8;j++)
{
if(map[i][j]=='K')
{
if(flag)
{
x1=i;
y1=j;
}
else
{
x2=i;
y2=j;
flag=1;
}
}
}
}
dfs(x1,y1,0);
if(tmp)
printf("YES\n");
else
printf("NO\n");
}
}