[HDU-1240] 三維的FLOODFILL

很簡單的題目,不過我又開始犯打字錯誤——誤將一個f[][][]打成tag[][][],結果我一直在debug...

/*
 * HDU-1240 asteroid
 * mike-w
 * 2012-9-25
 */
#include<stdio.h>
#include<string.h>

#define MAXSIZE 16
#define BUFSIZE 64
#define QSIZE 1024

typedef struct Position
{
	int x, y, z;
}pos;

int n, x0, yy0, z0, x1, yy1, z1;
char f[MAXSIZE][MAXSIZE][MAXSIZE];
int tag[MAXSIZE][MAXSIZE][MAXSIZE];
pos que[QSIZE];
int qhead, qtail, qlen;
int dir[6][3]={{0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0}};

int enque(pos *p)
{
	que[qtail].x=p->x;
	que[qtail].y=p->y;
	que[qtail].z=p->z;
	qtail=(qtail+1)%QSIZE;
	qlen++;
	return 0;
}

int deque(pos *p)
{
	p->x=que[qhead].x;
	p->y=que[qhead].y;
	p->z=que[qhead].z;
	qhead=(qhead+1)%QSIZE;
	qlen--;
	return 0;
}

int read(void)
{
	char buf[BUFSIZE];
	int t, i, j;
	if(scanf("%s%d", buf, &t)==EOF)
		return 0;
	n=t;
	memset(f, 0, sizeof(f));
	for(i=0; i<n; i++)
		for(j=0; j<n; j++)
			scanf("%s", f[i][j]);
	scanf("%d%d%d", &z1, &yy1, &x1);
	scanf("%d%d%d", &z0, &yy0, &x0);
	scanf("%s", buf);
	return 1;
}

int main(void)
{
	pos cur;
	int x2, y2, z2;
	int i;
	while(read())
	{
		memset(tag, 0, sizeof(tag));
		tag[x0][yy0][z0]=1;
		cur.x=x0;
		cur.y=yy0;
		cur.z=z0;

		enque(&cur);
		while(qlen>0)
		{
			deque(&cur);
			for(i=0; i<6; i++)
			{
				x2=cur.x+dir[i][0];
				y2=cur.y+dir[i][1];
				z2=cur.z+dir[i][2];
				if(x2>=0 && x2<n && y2>=0 && y2<n && z2>=0 && z2<n
						&& !tag[x2][y2][z2] && f[x2][y2][y2]=='O')
				{
					tag[x2][y2][z2]=tag[cur.x][cur.y][cur.z]+1;
					cur.x=x2;
					cur.y=y2;
					cur.z=z2;
					enque(&cur);
				}
			}
		}
		if(tag[x1][yy1][z1])
			printf("%d %d\n", n, tag[x1][yy1][z1]-1);
		else
			puts("NO ROUTE");
	}
	return 0;
}



發佈了204 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章