All pastes #812791 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-12-12 00:41 UTC
rendered paste body
/*
ID: tageorg1
PROG: mud
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int mid=500;
int offset=500;
short field[1001][1001];
int amax = 1001;
//0=empty,-1=mud
typedef struct {
	short x,y,length;
} point;
int main() {
	FILE* fin = fopen("mud.in","r");
	FILE* fout = fopen("mud.out","w");
	int X,Y,N;
	fscanf(fin,"%d%d%d",&X,&Y,&N);
	for (int n = 0; n < N; n++)
	{
		int x,y;
		fscanf(fin,"%d%d",&x,&y);
		x+=offset;
		y+=offset;
		if (x >= 0 && x < amax && y >= 0 && y < amax)
			field[x][y]=-1;
	}
	queue<point> tq;
	point start;
	start.x=mid;
	start.y=mid;
	start.length=0;
	tq.push(start);
	while (!tq.empty())
	{
		point cpoint = tq.front();
		tq.pop();
		int cx = cpoint.x;
		int cy = cpoint.y;
		int clength = cpoint.length;
		if (cx < 0 || cx >=amax || cy < 0 || cy >=amax)
			continue;
		if (field[cx][cy]!=0)
			continue;
		if (cx==X+offset && cy==Y+offset)
		{
			field[cx][cy]=clength;
			break;
		}
		//left
		if (cx-1 > 0 && field[cx-1][cy]==0)
		{
			point left;
			left.x=cx-1;
			left.y=cy;
			left.length=clength+1;
			tq.push(left);
		}
		//right
		if (cx+1 <= amax && field[cx+1][cy]==0)
		{
			point right;
			right.x=cx+1;
			right.y=cy;
			right.length=clength+1;
			tq.push(right);
		}
		//up
		if (cy+1 <= amax && field[cx][cy+1]==0)
		{
			point up;
			up.x=cx;
			up.y=cy+1;
			up.length=clength+1;
			tq.push(up);
		}
		//down
		if (cy-1>0 && field[cx][cy-1]==0)
		{
			point down;
			down.x=cx;
			down.y=cy-1;
			down.length=clength+1;
			tq.push(down);
		}
	}
	fprintf(fout,"%d\n",field[X+offset][Y+offset]);
}