codechef Correctness of Knight Move题解

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();
	}
};



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值