您的位置:首页 > 产品设计 > UI/UE

hdu4740The Donkey of Gui Zhou dfs水题

2015-09-25 22:49 387 查看
//一头驴和一只虎在n*n的格子中跑
//它们都一直往前走,且它们不会对一个格子重复走
//它们走到不能向前走后,驴会一直向右转
//虎会一直向左转,当转了一次后还是无法向前走
//它们会待在原地
//问驴和虎最终能相遇吗
//直接dfs就行
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 1010 ;
int vis[2][maxn][maxn] ;
int dx[4] = {0 , 1 , 0 , -1} ;
int dy[4] = {1 , 0 , -1 , 0} ;
int nd[4] = {1, 2, 3, 0} ;
int nt[4] = {3, 0, 1, 2} ;
int ax , ay ;
int n ;
bool judge(int x , int y , int t)
{
    if(x < 1 || x > n || y < 1 || y > n || vis[t][x][y])
    return false ;
    return true ;
}
bool dfs(int ddx , int ddy , int ddd ,int tx , int ty , int ddt)
{
    vis[0][tx][ty] = 1 ;
    vis[1][ddx][ddy] = 1 ;
    if(tx == ddx && ty == ddy)
    {
        ax = tx ;ay = ty ;
        return true ;
    }
    int ntx = tx + dx[ddt] ;
    int nty = ty + dy[ddt] ;
    int nddt = ddt ;
    int sum = 0 ;
    if(!judge(ntx , nty , 0))
    {
        nddt = nt[ddt] ;
        ntx = tx + dx[nddt];
        nty = ty + dy[nddt];
        if(!judge(ntx , nty  , 0))
        ntx = tx , nty = ty , nddt = ddt ,sum++ ;
    }
    int nddx = ddx + dx[ddd] ;
    int nddy = ddy + dy[ddd] ;
    int nddd = ddd ;
    if(!judge(nddx , nddy , 1))
    {
        nddd = nd[ddd] ;
        nddx = ddx + dx[nddd] ;
        nddy = ddy + dy[nddd] ;
        if(!judge(nddx , nddy , 1))
        nddx = ddx , nddy = ddy , nddd = ddd , sum++ ;
    }
    if(sum == 2)
    return false ;
    return dfs(nddx , nddy , nddd , ntx , nty ,nddt) ;
}
int main()
{
    while(scanf("%d" , &n) && n)
    {
        int a , b , c ;
        int d , e , f ;
        memset(vis, 0 , sizeof(vis))  ;
        scanf("%d%d%d%d%d%d" , &a , &b , &c , &d , &e ,&f) ;
        vis[0][d+1][e+1] = vis[1][a+1][b+1] = 1;
        if(dfs(a + 1 , b + 1, c , d + 1, e + 1 , f))
        printf("%d %d\n" ,ax - 1, ay - 1);
        else puts("-1") ;
    }
    return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: