pku2766.cpp

来自「这是ACM 方面的资料 是PKU的 北京大学的出来的」· C++ 代码 · 共 95 行

CPP
95
字号
#include <stdio.h>
#include <stdlib.h>

int b[51][51];
int D[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n;

int In(int x, int y)
{
	if (x < 1 || x > n || y < 1 || y > n)
	{
		return 0;
	}
	if (b[x][y] == 0)
	{
		return 1;
	}
	return 2;
}

int main()
{
	int T, x, y, i, r, dis, j;

	scanf("%d", &T);
	
	while (T--)
	{
		scanf("%d %d", &n, &r);
		for (i = 0; i <= n; i++)
		{
			for (j = 0; j <= n; j++)
			{
				b[i][j] = 0;
			}
		}
		while (r--)
		{
			scanf("%d %d", &x, &y);
			b[x][y] = 1;
		}
		scanf("%d %d", &x, &y);
		if (x == n + 1)
		{
			dis = 2;
			x--;
		}
		else if (y == n + 1)
		{
			dis = 3;
			y--;
		}
		else if (x == 0)
		{
			dis = 0;
			x++;
		}
		else if (y == 0)
		{
			dis = 1;
			y++;
		}
		i = 0;
		while (i < 100000)
		{
			if (In(x, y) == 0)
			{
				break;
			}
			if (In(x, y) == 1)
			{
				x += D[dis][0];
				y += D[dis][1];
			}
			else if (In(x, y) == 2)
			{
				i++;
				dis = (dis + 3) % 4;
				x += D[dis][0];
				y += D[dis][1];
			}
		}
		if (i == 100000)
		{
			printf("0 0\n");
		}
		else
		{
			printf("%d %d\n", x, y);
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?