HDU 1240 三维bfs

#include <iostream>
#include <queue>
using namespace std;
int map[15][15][15];
int flag, a, b, c, d, e, f, n;
char str[15][15][15];
int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, 
				{0,-1,0}, {0,0,1}, {0,0,-1}};

struct node
{
	int x, y, z, step;
};

int judge(int x, int y, int z)
{
	if(x >= 0 && x < n && y >= 0 && y < n && z >= 0 
		&& z < n && map[x][y][z] ==0 && str[x][y][z] == 'O')
		return 1;
	return 0;
}

void bfs()
{
	int i, x, y, z;
	queue<node> q;
	node cur, next;
	memset(map, 0, sizeof(map));
	cur.x = a;
	cur.y = b;
	cur.z = c;
	cur.step = 0;
	q.push(cur);
	map[a][b][c] = 1;
	if(a == d && b == e && c == f)
	{
		printf("%d 0\n", n);
		return;
	}
	while(!q.empty())
	{
		cur = q.front();
		q.pop();
		for(i = 0; i < 6; i++)
		{
			next.x = x = cur.x + dir[i][0];
			next.y = y = cur.y + dir[i][1];
			next.z = z = cur.z + dir[i][2];
			if(judge(x,y,z))
			{
				map[x][y][z] = 1;
				next.step = cur.step + 1;
				if(x == d && y == e && z == f)
				{
					printf("%d %d\n", n, next.step);
					return;
				}
				q.push(next);
			}
		}
	}
	printf("NO ROUTE\n");
}

int main()
{
	int i, j;
	char s[15], t[15];
	while(scanf("%s %d", s, &n) == 2)
	{
		for(i = 0; i < n; i++)
			for(j = 0; j < n; j++)
				scanf("%s", str[i][j]);
		scanf("%d%d%d", &a, &b, &c);
		scanf("%d%d%d", &d, &e, &f);
		str[d][e][f] = 'O';
		bfs();
		scanf("%s", t);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值