【hdu】The Donkey of Gui Zhou(搜索)

一开始的思路是,先走驴子,之后走老虎,每次走的时候记录他们的达到该位置的步数,如果一样就可以相遇,特殊处理末尾。

但是没办法过,之后索性让他们一起走,一起修改移动的坐标,结果过了。。。

真心不知道错哪了。。

11662502 2014-09-16 00:43:01 Accepted 4740 31MS 8300K 2547 B G++ KinderRiven

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<algorithm>
#include<map>
#include<sstream>
#include<cmath>
#include<queue>
#define INF (1 << 30)
#define eps (1e-10)
#define _PI acos(-1.0)
using namespace std;
/*=========================================
=========================================*/
#define MAXD 1000 + 10
int n;
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int aa , bb;
int vis1[MAXD][MAXD],vis2[MAXD][MAXD];
bool solve(int x1,int y1,int d1,int x2,int y2,int d2){
    memset(vis1,0,sizeof(vis1));
    memset(vis2,0,sizeof(vis2));
    vis1[x1][y1] = 1;
    vis2[x2][y2] = 1;
    int _x1 ,_y1;
    int _x2 ,_y2;
    while(aa || bb){
        if(x1 == x2 && y1 ==y2){
            printf("%d %d\n",x1,y1);
            return true;
        }
        if(aa){
            _x1 = x1 + dir[d1][0];
            _y1 = y1 + dir[d1][1];
            if(_x1 >= 0 && _x1 < n && _y1 >= 0 && _y1 < n && !vis1[_x1][_y1]){
                vis1[_x1][_y1] = 1;
            }
            else{
                d1 = (d1 + 1) % 4;
                _x1 = x1 + dir[d1][0];
                _y1 = y1 + dir[d1][1];
                if(_x1 >= 0 && _x1 < n && _y1 >= 0 && _y1 < n && !vis1[_x1][_y1]){
                    vis1[_x1][_y1] = 1;
                }
                else{
                    aa = 0;
                }
            }
        }
        if(bb){
            _x2 = x2 + dir[d2][0];
            _y2 = y2 + dir[d2][1];
            if(_x2 >= 0 && _x2 < n && _y2 >= 0 && _y2 < n && !vis2[_x2][_y2]){
                vis2[_x2][_y2] = 1;
            }
            else{
                d2 = (d2 + 3) % 4;
                _x2 = x2 + dir[d2][0];
                _y2 = y2 + dir[d2][1];
                if(_x2 >= 0 && _x2 < n && _y2 >= 0 && _y2 < n && !vis2[_x2][_y2]){
                    vis2[_x2][_y2] = 1;
                }
                else{
                    bb = 0;
                }
            }
        }
        if(aa) {x1 = _x1; y1 = _y1;}
        if(bb) {x2 = _x2; y2 = _y2;}
    }
    return false;
}
int main(){
    while(scanf("%d",&n) && n){
        int x1,y1,x2,y2,d1,d2;
        aa = 1; bb = 1;
        scanf("%d%d%d",&x1,&y1,&d1);
        scanf("%d%d%d",&x2,&y2,&d2);
        if(!solve(x1,y1,d1,x2,y2,d2))
           printf("-1\n");
    }
    return 0;
}

你可能感兴趣的:(【hdu】The Donkey of Gui Zhou(搜索))