标签:
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
11这一题是按照6个方向走的:上,下,左,右,前,后。创建一个三维数组记录当前的位置状态。#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define N 60 int map[N][N][N]; int vis[N][N][N]; int t,a,b,c,tt; int dx[] = {0,0,-1,1,0,0}; int dy[] = {0,0,0,0,-1,1}; int dz[] = {-1,1,0,0,0,0}; struct point { int x,y,z,step; }; int bfs() { if (a - 1 == 0 && b - 1 == 0 && c - 1 == 0)//如果出口位置与入口相同,直接返回0. return 0; point temp, head, tt; queue<point>q; while (!q.empty()) q.pop(); vis[0][0][0] = 1; temp.x = 0; temp.y = 0; temp.z = 0; temp.step = 0; q.push(temp); while (!q.empty()) { head = q.front(); q.pop(); for (int i = 0; i < 6; i++) { tt.x = head.x + dx[i]; tt.y = head.y + dy[i]; tt.z = head.z + dz[i]; if (tt.x < 0 || tt.y < 0 || tt.z < 0 || tt.x >= a || tt.y >= b || tt.z >= c || vis[tt.x][tt.y][tt.z]||map[tt.x][tt.y][tt.z]) continue; tt.step = head.step + 1; vis[tt.x][tt.y][tt.z] = 1; if (tt.x == a - 1 && tt.y == b - 1 && tt.z == c - 1) return tt.step; q.push(tt); } } return (-1);//如果找不到出口就返回-1. } int main() { scanf("%d", &t); while (t--) { memset(vis, 0, sizeof(vis)); scanf("%d%d%d%d", &a, &b, &c, &tt); for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { for (int k = 0; k < c; k++) { scanf("%d", &map[i][j][k]); } } } int ans = bfs(); if (ans<0 || ans>tt)//返回值小于零找不到出口,大于魔王的时间都不能逃脱. printf("-1\n"); else printf("%d\n", ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_24489717/article/details/45564989