HDU 网络赛(杭州赛区)1003 (13.09.15)
Problem Description
The donkey lived happily until it saw a tiger far away. The donkey had never 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 direction, but they couldn't get out of the forest. And because they both wanted to go to new places, the donkey would 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 direction at the beginning and they always had the same speed. They would 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 would stop running and stayed where they were, without trying to make another turn. Now given their starting positions and directions, please count whether they would meet in a cell.InputOutputSample InputSample Output#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;}