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

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

时间: 2024-08-12 17:34:59

codechef Correctness of Knight Move题解的相关文章

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

codechef Sums in a Triangle题解

Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths st

codechef Holes in the text 题解

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For exampl

codechef Little Elephant and Permutations题解

The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ...,N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversio

codechef Johnny and the Beanstalk 题解

One evening Johnny found some funny looking beens in his grandfather's garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count it

HDU 1372 Knight Moves 题解

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14125    Accepted Submission(s): 8269 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe

CodeChef Chef and Digit Jumps 题解

原题链接:Chef and Digit Jumps 题意:原题中有链接. 题解:一道很明显的bfs题,就是跳就可以了,当然,跳的时候可以加一些优化,具体看代码 #include <queue> #include <cstdio> #include <cstring> using namespace std; #define Maxn 100000 char s[Maxn+5]; int n; int a[Maxn+5]; queue<int> q; vect

codechef The Ball And Cups题解

The Ball And Cups At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game