程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 網絡賽(杭州賽區)1003 (13.09.15)

HDU 網絡賽(杭州賽區)1003 (13.09.15)

編輯:C++入門知識

Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1) and the cell below the up-left cell is (1,0)..... A 4×4 grid is shown below:     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.     Input There are several test cases.   In each test case: First line is an integer N, meaning 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 direction 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)     Output For each test case, if the donkey and the tiger would meet in a cell, print the coordinate of the cell where they meet first time. If they would never meet, print -1 instead.     Sample Input 2 0 0 0 0 1 2 4 0 1 0 3 2 0 0     Sample Output -1 1 3   題意: 給出一片森林 在這片森林裡, 驢和虎從沒見過對方, 因此一見面就嚇尿了, 撒腿就跑 0 1 2 3代表四個方向, 沒有特殊情況就按初始方向直直跑 不能繼續向前了, 驢是只考慮向右, 而虎是只考慮向左   注意, 驢和虎都不能跑自己曾經跑過的地塊!   AC代碼:

#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;  
}  

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved