# 2013 ACM/ICPC 杭州网络赛C题

#include <cstring>
#include <cstdio>
using namespace std;

#define MAX_Y 1005
#define MAX_X MAX_Y

struct Point
{
int x, y;
Point()
{}
Point(int x, int y):x(x), y(y)
{}
bool operator == (const Point &a) const
{
return a.x == x && a.y == y;
}
Point operator + (const Point &a)
{
return Point(x + a.x, y + a.y);
}
};

Point dir[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)};
bool donkey_vis[MAX_X][MAX_Y];
bool tiger_vis[MAX_X][MAX_Y];
int tiger_dir;
int donkey_dir;
int grid_size;
Point tiger, donkey;
bool tiger_stop, donkey_stop;

void visit(Point a, bool vis[][MAX_X])
{
vis[a.x][a.y] = true;
}

void input(Point &a, int &dir)
{
int x, y;
scanf("%d%d%d", &x, &y, &dir);
a.x = x;
a.y = y;
}

bool ok(Point a, bool vis[][MAX_Y])
{
if (a.x < 0 || a.x >= grid_size || a.y < 0 || a.y >= grid_size)
return false;
return !vis[a.x][a.y];
}

void move(Point &animal, int &cur_dir, bool vis[][MAX_Y], bool &stop, int dir_delta)
{
if (stop)
return;
Point des = animal + dir[cur_dir];
if (ok(des, vis))
{
animal = des;
visit(des, vis);
return;
}
cur_dir = (cur_dir + dir_delta + 4) % 4;
des = animal + dir[cur_dir];
if (ok(des, vis))
{
animal = des;
visit(des, vis);
return;
}
stop = true;
}

bool work()
{
tiger_stop = false;
donkey_stop = false;
memset(tiger_vis, 0, sizeof(tiger_vis));
memset(donkey_vis, 0, sizeof(donkey_vis));
if (tiger == donkey)
return true;
visit(tiger, tiger_vis);
visit(donkey, donkey_vis);
while (!(tiger_stop && donkey_stop))
{
move(tiger, tiger_dir, tiger_vis, tiger_stop, -1);
move(donkey, donkey_dir, donkey_vis, donkey_stop, 1);
if (tiger == donkey)
return true;
}
return false;
}

int main()
{
while (scanf("%d", &grid_size), grid_size)
{
input(donkey, donkey_dir);
input(tiger, tiger_dir);
if (work())
printf("%d %d\n", tiger.x, tiger.y);
else
printf("-1\n");
}
return 0;
}
View Code

posted @ 2013-09-16 17:31  金海峰  阅读(...)  评论(...编辑  收藏