spoj 1418 The Cats and the Mouse

来源:互联网 发布:网络安全产品排名 编辑:程序博客网 时间:2024/04/30 08:35

CATM - The Cats and the Mouse

#bfs

In a rectangular field of size n by m squares there is a mouse and two cats. The mouse is the first to make a move, then each of the cats makes a move, then again its the mouse's turn, and so on. In each move both the mouse and the cats can move exactly one square vertically or horizontally. If the mouse is standing at the edge of the field then in its next move it can jump off the field and is saved from the cats. If in the next move one of the cats moves to the field with the mouse then there is no escape for the mouse ... =(

You are to write a program which, knowing the initial positions of mouse and the two cats, will find out if there is any way for the mouse to escape from the cats, assuming of course that each cat will do its best to catch the mouse.

Input

In the first line of input two integers n and m are given, not exceeding 100, where n is the number of rows, and m - the number of columns. The second line contains a number k [k <= 10], which defines the number of test cases for the given field. In the next k lines the initial positions of the mouse and the cats are given. The position in the field is given by two numbers: the first is the number of the row, the second is the number of the column. The first two integers are the coordinates of the mouse, the next four integers are the coordinates of the cats.

Output

You must output k lines with answers for each test case. The answer is YES, if the mouse can escape or NO otherwise.

Example

Input:5 332 2 1 1 3 32 3 1 3 5 23 2 1 2 4 3Output:NOYESYES
#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;int main() {int R, C, test, mr, mc, ar, ac, br, bc;int mtop, mleft, mbot, mright, ctop, cleft, cbot, cright;scanf("%d%d%d", &R, &C, &test);while(test--) {scanf("%d%d%d%d%d%d", &mr, &mc, &ar, &ac, &br, &bc);if(mr >= ar && mr >= br) printf("YESn");else if(mr <= ar && mr <= br) printf("YES\n");else if(mc >= ac && mc >= bc) printf("YES\n");else if(mc <= ac && mc <= bc) printf("YES\n");else {mtop = mr - 1;ctop = min(abs(1-ar)+abs(mc-ac), abs(1-br)+abs(mc-bc));mleft = C - mc;cleft = min(abs(mr-ar)+abs(C-ac), abs(mr-br)+abs(C-bc));mbot = R - mr;cbot = min(abs(R-ar)+abs(mc-ac), abs(R-br)+abs(mc-bc));mright = mc - 1;cright = min(abs(mr-ar)+abs(1-ac), abs(mr-br)+abs(1-bc));if(mtop < ctop || mbot < cbot || mleft < cleft || mright < cright) printf("YES\n");else printf("NO\n");}}return 0;}