Cod sursa(job #577192)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 9 aprilie 2011 20:35:34
Problema Gropi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include<stdio.h>

struct pct{long x; long y;} p1, p2, aux;
long i, j, col, gr, xx, n,yy, a[3][100001];

void mergi(long i, long j, long pas)
{
	a[i][j] = pas;

	if (a[i - 1][j] == 0 && i > 1) mergi(i - 1, j, pas + 1);
	if (a[i + 1][j] == 0 && i < 2) mergi(i + 1, j, pas + 1);
	if (a[i][j - 1] == 0 && j > 1) mergi(i, j - 1, pas + 1);
	if (a[i][j + 1] == 0 && j < col) mergi(i, j + 1, pas + 1);
}

int main()
{
	freopen("gropi.in","r",stdin);
	freopen("gropi.out","w",stdout);
	
		scanf("%ld %ld", &col, &gr);
		
		for (i = 1; i <= gr; i ++)
		{
			scanf("%ld %ld", &xx, &yy);
			a[xx][yy] = -1;
		}
		
		for (i = 1; i <= 2; i ++)
			for (j = 1; j <= col; j ++)
			{
				if (a[i][j] == 0)
				{
					if (a[i - 1][j] == 0 && i > 1) 
					{
						a[i][j] = 1;
						mergi(i - 1, j, 2);
					}
					else if (a[i + 1][j] == 0 && i < 2)
					{
						a[i][j] = 1;
						mergi(i + 1, j, 2);
					}
					else if (a[i][j - 1] == 0 && j > 1)
					{
						a[i][j] = 1;
						mergi(i, j - 1, 2);
					}
					else if (a[i][j + 1] == 0 && j < col)
					{
						a[i][j] = 1;
						mergi(i, j + 1, 2);
					}
				}
			}
		
		scanf("%ld", &n);
		
		for (i = 1; i <= n; i ++)
		{
			scanf("%ld %ld %ld %ld", &p1.x, &p1.y, &p2.x, &p2.y);
			if (p2.y < p1.y)
				aux = p1, p1 = p2, p2 = aux;
			printf("%ld\n", a[p2.x][p2.y] - a[p1.x][p1.y] + 1);
		}
			
	return 0;
}