[HDU-1240] 三维的FLOODFILL

一道简单的编程题,由于输入变量名错误导致调试困难。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单的题目,不过我又开始犯打字错误——误将一个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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值