首页 > 其他 > 详细

codechef Correctness of Knight Move题解

时间:2014-05-06 23:10:26      阅读:554      评论:0      收藏:0      [点我收藏+]

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first whether this string represents the correct pair of cells on the chess board (we call such strings correct) and then report whether it represents the correct move depending on the situation on the chess board. Chef always has troubles with analyzing knight moves. So at first he needs a test program that can say whether a given string is correct and then whether it represents a correct knight move (irregardless of the situation on the chess board). The cell on the chessboard is represented as a string of two characters: first character is a lowercase Latin letter from a to h and the second character is a digit from 1 to 8. The string represents the correct pair of cells on the chess board if it composed of 5 characters where first two characters represent the cell where chess figure was, 3rd character is the dash "-" and the last two characters represent the destination cell.

Input

The first line contains a single integer T <= 50000, the number of test cases. T test cases follow. The only line of each test case contains a non-empty string composed the characters with ASCII-codes from 32 to 126. The length of the string is not greater than 10.

Output

For each test case, output a single line containing the word "Error" if the corresponding string does not represent the correct pair of cells on the chess board. Otherwise output "Yes" if this pair of cells represents the correct knight move and "No" otherwise.

Example

Input:
4
a1-b3
d2-h8
a3 c4
ErrorError

Output:
Yes
No
Error
Error

算是简单程序。

本程序之所以搞的那么复杂,是因为最求速度。处理输入输出过万的数据。

本程序OJ提交0.01ms,速度并排第一。


#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

class CorrectnessofKnightMove
{
	const static int MAX_BU = 5120;
	const static int FLASH_P = MAX_BU - 7;
	int st, len, ost;
	char buffer[MAX_BU];
	char outBuffer[MAX_BU];

	char getFromBuf()
	{
		if (st >= len)
		{
			len = fread(buffer, 1, MAX_BU, stdin);
			st = 0;
		}
		return buffer[st++];
	}

	void fillinOBuffer(char *ans, const int n)
	{
		if (ost > FLASH_P)
		{
			fwrite(outBuffer, 1, ost, stdout);
			ost = 0;
		}
		for (int i = 0; i < n; i++)
		{
			outBuffer[ost++] = ans[i];
		}
	}
	void flashLeft()
	{
		if (ost) fwrite(outBuffer, 1, ost, stdout);
	}

	bool isLegalKnight(char *kni, const int n)
	{
		if (kni[0] - kni[3] == 2 || kni[3] - kni[0] == 2)
		{
			if (kni[1] - kni[4] == 1 || kni[4] - kni[1] == 1) return true;
		}
		else if (kni[1] - kni[4] == 2 || kni[4] - kni[1] == 2)
		{
			if (kni[0] - kni[3] == 1 || kni[3] - kni[0] == 1) return true;
		}
		return false;
	}

	bool isLegalStr(char *kni, const int n)
	{
		if (n != 5) return false;
		if (kni[0] < ‘a‘ || kni[0] > ‘h‘) return false;
		if (kni[1] < ‘1‘ || kni[1] > ‘8‘) return false;
		if (kni[2] != ‘-‘) return false;
		if (kni[3] < ‘a‘ || kni[3] > ‘h‘) return false;
		if (kni[4] < ‘1‘ || kni[4] > ‘8‘) return false;
		return true;
	}

public:
	CorrectnessofKnightMove() : st(0), len(0), ost(0)
	{
		int T = 0;
		scanf("%d", &T);
		getchar();//加getchar正确,使用scanf("%d\n", &T)错误
		char str[12], c;
		while (T--)
		{
			int i = 0;
			while (((c = getFromBuf()) != ‘\n‘) && len)
			{
				str[i++] = c;
			}

			if (!isLegalStr(str, i))
			{
				fillinOBuffer("Error\n", 6);
			}
			else if (isLegalKnight(str, i))
			{
				fillinOBuffer("Yes\n", 4);
			}
			else fillinOBuffer("No\n", 3);
		}
		flashLeft();
	}
};



codechef Correctness of Knight Move题解,布布扣,bubuko.com

codechef Correctness of Knight Move题解

原文:http://blog.csdn.net/kenden23/article/details/25139747

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!