Problem DescriptionThere was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which cocould be considered as an N × N grid. the coordinates of the up-left cell is (), the down-right cell is (N-1, N-1) and the cell below the up-left cell is )..... A 4× 4 grid is shown below: The donkey lived happily until it saw a tiger far away. the donkey had nev Er seen a tiger, and the tiger had never seen a donkey. both of them were frightened and wanted to escape from each other. so they started running fast. because they were scared, they were running in a way that didn't make any sense. each step they moved to the next cell in their running ction, but they couldn't get out of the forest. and because they both wanted to go to new places, the donke Y wowould never stepped into a cell which had already been visited by itself, and the tiger acted the same way. both the donkey and the tiger ran in a random ction at the beginning and they always had the same speed. they wocould not change their directions until they couldn't run straight ahead any more. if they couldn't go ahead any more, they changed their directions immediately. when changing Direction, the donkey always turned right and the tiger always turned left. if they made a turn and still couldn't go ahead, they wocould stop running and stayed where they were, without trying to make another turn. now given their starting positions and directions, please count whether they wocould meet in a cell. inputThere are several test cases. in each test case: First line is an integer N, meanin G that the forest is a N × N grid. the second line contains three integers R, C and D, meaning that the donkey is in the cell (R, C) when they started running, and it's original ction is D. D can be 0, 1, 2 or 3. 0 means east, 1 means south, 2 means west, and 3 means north. the third line has the same format and meaning as the second line, but it is for the tiger. the input ends with N = 0. (2 <= N <= 1000, 0 <= R, C <N) OutputFor each test case, if the donkey and the tiger wocould meet in a cell, print the coordinate of the cell where they meet first time. if they wocould never meet, print-1 instead. sample Input20 0 00 1 240 1 03 2 00 Sample Output-11, running 0, 1, 2, and 3 means four directions. If there are no special cases, you can directly run in the initial direction and cannot continue to the forward direction. The donkey only considers the right direction, while the tiger only considers the left direction, neither the donkey nor the tiger can run the land they once ran! AC code:
#include<stdio.h> #include<stdlib.h> #include<string.h> int lgrid[1234][1234]; int hgrid[1234][1234]; int t; int success; int lunremove; int hunremove; int ansx, ansy; int main() { while(scanf("%d", &t) != EOF && t) { success = 0; lunremove = 1; hunremove = 1; memset(lgrid, 0, sizeof(lgrid)); memset(hgrid, 0, sizeof(hgrid)); int ltx, lty, htx, hty; int ltd, htd; scanf("%d %d %d", <x, <y, <d); scanf("%d %d %d", &htx, &hty, &htd); while(lunremove == 1 || hunremove == 1) { if(ltx == htx && lty == hty) { success = 1; ansx = ltx; ansy = lty; break; } lgrid[ltx][lty] = 1; hgrid[htx][hty] = 1; if(ltd == 2 && lunremove == 1) { if(lty > 0 && lgrid[ltx][lty-1] != 1) lty = lty - 1; else if(ltx > 0 && lgrid[ltx-1][lty] != 1) { ltx = ltx - 1; ltd = 3; } else lunremove = 0; } else if(ltd == 1 && lunremove == 1) { if(ltx < t-1 && lgrid[ltx+1][lty] != 1) ltx = ltx + 1; else if(lty > 0 && lgrid[ltx][lty-1] != 1) { lty = lty - 1; ltd = 2; } else lunremove = 0; } else if(ltd == 0 && lunremove == 1) { if(lty < t-1 && lgrid[ltx][lty+1] != 1) lty = lty + 1; else if(ltx < t-1 && lgrid[ltx+1][lty] != 1) { ltx = ltx + 1; ltd = 1; } else lunremove = 0; } else if(ltd == 3 && lunremove == 1) { if(ltx > 0 && lgrid[ltx-1][lty] != 1) ltx = ltx - 1; else if(lty < t-1 && lgrid[ltx][lty+1] != 1) { lty = lty + 1; ltd = 0; } else lunremove = 0; } if(htd == 2 && hunremove == 1) { if(hty > 0 && hgrid[htx][hty-1] != 1) hty = hty - 1; else if(htx < t-1 && hgrid[htx+1][hty] != 1) { htx = htx + 1; htd = 1; } else hunremove = 0; } else if(htd == 1 && hunremove == 1) { if(htx < t-1 && hgrid[htx+1][hty] != 1) htx = htx + 1; else if(hty < t-1 && hgrid[htx][hty+1] != 1) { hty = hty + 1; htd = 0; } else hunremove = 0; } else if(htd == 0 && hunremove == 1) { if(hty < t-1 && hgrid[htx][hty+1] != 1) hty = hty + 1; else if(htx > 0 && hgrid[htx-1][hty] != 1) { htx = htx - 1; htd = 3; } else hunremove = 0; } else if(htd == 3 && hunremove == 1) { if(htx > 0 && hgrid[htx-1][hty] != 1) htx = htx - 1; else if(hty > 0 && hgrid[htx][hty-1] != 1) { hty = hty - 1; htd = 2; } else hunremove = 0; } } if(success == 1) printf("%d %d\n", ansx, ansy); else printf("-1\n"); } return 0; }