A - Two Semiknights Meet

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 and2 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.


思路是 因为只有8个格子 而且要求同时移动,因此必然只要满足

		if ((abs(x1-x2)==4||abs(x1-x2)==0)&&(abs(y1-y2)==4||abs(y1-y2)==0))

就可以了

这是后来才发现的,之前用的是两个dfs+该条件   dfs多余了 


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue> 
#include <set>
#include <vector>
using namespace std;

int map[505][505]; 

int main()
{
	int i,j;
	
	int t;
	cin>>t;
 
	while(t--)
	{ 
		getchar();
		int x1,y1,x2,y2;
		int line=0;
		for (i=1;i<=8;i++)
		{
			for (j=1;j<=8;j++)
			{
				scanf("%c",&map[i][j]);
				if (map[i][j]=='K')
				{
					if (line==0)
					{
						x1=i;
						y1=j;
						line=1;
					}
					else
					{
						x2=i;y2=j;
					}
				}
			}
			getchar();
		}
		 
		if ((abs(x1-x2)==4||abs(x1-x2)==0)&&(abs(y1-y2)==4||abs(y1-y2)==0))
		{	
			printf("YES\n");
		}
		else
			printf("NO\n");
		
		
	}
	return 0; 
	
} 





你可能感兴趣的:(A - Two Semiknights Meet)