HDU T1253 胜利大逃亡

版权声明:希望能帮到弱校的ACMer成长,因为自己是弱校菜鸡~~~~ https://blog.csdn.net/Mr__Charles/article/details/82218309

                           HDU T1253 胜利大逃亡


题解:

    这题并不难,别被题目吓住了,只要搞清楚三维坐标怎么摆放,这题就OK了.

    因为(0,0,0)在左上后方(造词---),所以坐标抽该这么摆

    对应的关系是:

    A——X

    B——Y

    C——Z

    这关系在我代码中对maps数组的使用就可以看出来。

 

    代码:

#include<cstdio>
#include<iostream>
#include<queue>
#define maxn 55
using namespace std;
int a,b,c,tl;
int maps[maxn][maxn][maxn];
int mov[6][3] = {0,-1,0, 0,1,0, 0,0,-1, 0,0,1, 1,0,0, -1,0,0};

struct Node{
	int x,y,z,st;
}now,nex;

bool charge(int x,int y,int z){
	if(x < 0 || x >= a|| y < 0|| y >= b|| z < 0|| z >= c || maps[x][y][z])
	    return false;
	return true;
}

int Bfs(int st){
	now.x = 0;
	now.y = 0;
	now.z = 0;
	now.st = st;
	queue<Node> Q;
	Q.push(now);
	while(!Q.empty()){
		now = Q.front();
		Q.pop();
		
		if(now.x == a-1 && now.y == b-1 && now.z == c-1 && now.st <= tl)
		    return now.st;
        
		for(int i = 0; i < 6; ++i){
			nex.x = now.x + mov[i][0];
			nex.y = now.y + mov[i][1];
			nex.z = now.z + mov[i][2];
			if(charge(nex.x,nex.y,nex.z)){
				nex.st = now.st + 1;
				maps[nex.x][nex.y][nex.z] = 1;
				Q.push(nex);
			}
		}
	}
	return -1;
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&a,&b,&c,&tl);
		for(int i = 0; i < a; ++i)
		    for(int j = 0; j < b; ++j)
		        for(int k = 0; k < c; ++k)
		            scanf("%d",&maps[i][j][k]);
		printf("%d\n",Bfs(0));
	}
}

猜你喜欢

转载自blog.csdn.net/Mr__Charles/article/details/82218309