hdu 1253 bfs

这个题啊 WA了N次 Time out了N次

继续加油!!


#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

#define MAXN 55



int a, b, c, t, castle[MAXN][MAXN][MAXN],vist[MAXN][MAXN][MAXN];

int rx[] = { 1, -1, 0, 0, 0, 0 };
int ry[] = { 0, 0, 1, -1, 0, 0 };
int rz[] = { 0, 0, 0, 0, 1, -1 };

int scheck(int ix, int iy, int iz){
	if (ix < 0 || iy < 0 || iz < 0 || ix >= a || iy >= b || iz >= c || castle[ix][iy][iz] == 1 || vist[ix][iy][iz] == 1){
		return 0;
	}
	return 1;
}

int abs(int x,int a){
	if (x - a < 0)
		return a - x;
	return x - a;
}

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

int bfs(){
	queue<node> q;
	node m, n;
	int i, j, k,s,ss;
	m.x = 0;
	m.y = 0;
	m.z = 0;
	m.step = 0;
	vist[0][0][0] = 1;
	q.push(m);
	while (!q.empty()){
		n = q.front();
		q.pop();
		for (s = 0; s < 6; s++){
			m.x = n.x + rx[s];
			m.y = n.y + ry[s];
			m.z = n.z + rz[s];
			m.step = n.step;
			if (scheck(m.x,m.y,m.z) == 1){
				m.step++;
				vist[m.x][m.y][m.z] = 1;
				if (m.x == a - 1 && m.y == b - 1 && m.z == c - 1 && m.step <= t){
					return m.step;
				}
				if (abs(m.x, a-1) + abs(m.y, b-1) + abs(m.z, c-1) + m.step > t){
					continue;
				}
				q.push(m);
			}
		}
	}
	return -1;
}

int main(){
	freopen("TestDate.txt", "r", stdin);
	int test,i,j,k;
	scanf("%d", &test);
	while (test--){
		scanf("%d %d %d %d", &a, &b, &c, &t);
		for (i = 0; i < a; i++){
			for (j = 0; j < b; j++){
				for (k = 0; k < c; k++){
					scanf("%d", &castle[i][j][k]);
				}
			}
		}
		if (castle[a - 1][b - 1][c - 1] == 1){
			cout << "-1" << endl;
			continue;
		}
		memset(vist, 0, sizeof(vist));
		cout << bfs() << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值