简单题
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 55
int vis[maxn][maxn];
bool map[maxn][maxn];
int n, r, sx, sy;
int dir[4][2] =
{
{ 1, 0 },
{ 0, -1 },
{ -1, 0 },
{ 0, 1 } };
void input()
{
scanf("%d%d", &n, &r);
memset(map, 0, sizeof(map));
for (int i = 0; i < r; i++)
{
int a, b;
scanf("%d%d", &a, &b);
map[a][b] = true;
}
scanf("%d%d", &sx, &sy);
}
bool out(int x, int y)
{
if (x == 0 || y == 0 || x == n + 1 || y == n + 1)
return true;
return false;
}
void work()
{
int d;
if (sx == 0)
d = 0;
if (sy == n + 1)
d = 1;
if (sx == n + 1)
d = 2;
if (sy == 0)
d = 3;
int x = sx;
int y = sy;
memset(vis, 0, sizeof(vis));
while (1)
{
x += dir[d][0];
y += dir[d][1];
if (out(x, y))
{
printf("%d %d\n", x, y);
return;
}
if (vis[x][y] & (1 << d))
break;
vis[x][y] += 1 << d;
if (map[x][y])
d = (d + 1) % 4;
}
printf("0 0\n");
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
work();
}
return 0;
}