nyoj 523 run away

run away

Time Limit: 1000 ms | Memory Limit: 65535 KB

Difficulty: 4

describe

    Once upon a time, there was a knight named hck. In order to save our beautiful princess, he infiltrated the lair of the Demon King. He was a hero. But heroes are not so easy to be. This poor baby was captured by the Demon King and was tortured to the point of death. One day the Demon King went out on a date, which was a once-in-a-lifetime chance to escape. Your task now is to judge whether this failed hero can escape from the devil's castle before the devil returns, escape successfully, and finally marry our beautiful princess.

    The devil lives in a castle, the castle is a cube of A*B*C, which can be represented as a matrix of A and B*C. At the beginning, hck is locked at the position of (0,0,0), leaving the door of the castle At (A-1, B-1, C-1) position, now knowing that the devil will return to the castle in T minutes, hck can walk from one coordinate to one of the six adjacent coordinates every minute. Now Give you the map of the castle, please calculate whether hck can leave the castle before the devil returns (as long as you walk to the exit, you will leave the castle, and if the devil just comes back when you reach the exit, it will be considered a successful escape), if you can, please output how many minutes it will take to leave, if not, output -1.

 

As shown in the figure, the top left corner of block 0 in the input data is where hck is closed, and the bottom right corner of block A-1 is the exit of the castle. Move each layer in the direction of the red arrow in the picture to form the entire castle . 

enter

The first line of input data is a positive integer K, indicating the number of test data. The first line of each set of test data is four positive integers A, B, C and T (1<=A,B,C<=50 , 1<=T<=1000), they represent the size of the castle and the time when the devil returns.
Then there is the A block of input data (first block 0, then block 1, block 2...) , each block of input data has B rows, and each row has C positive integers, representing the layout of the maze, where 0 represents the road and 1 represents the wall.
(If the input description is not clear, you can refer to the above maze description, which represents the The maze in the picture above)

output

For each set of test data , if hck can leave the castle before the devil returns, please output the minimum number of minutes he needs, otherwise output -1.

sample input

2

3 2 2 10

0 1

0 0

1 1

1 0

0 0

0 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

Sample output

-1

11

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
using namespace std;
char visit[52][52][52];//Visit flag
char map[52][52][52];
int a,b,c;
struct node
{
    int x;
    int y;
    int z;
    int time;
};
int dir[6][3]={{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0 },{0,-1,0}};
int go(int x,int y,int z)//Determine whether to enter the queue
{
    if(0<=x&&x<a&&0<=y&&y<b&&0<=z&&z<c&&map[x][y][z]==0)
    return 1;
    return 0;
}
int bfs(int m)
{
    node st,ed;
    int i;
    queue<node>q;
    st.x=0;
    st.y=0;
    st.z=0;
    st.time=0;
    q.push(st);//The starting point enters the queue
    memset(visit,0,sizeof(visit));//Clear the access flag
    visit[0][0][0]=1;//The mark has been passed
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(st.time>m)//Exit after timeout
        return -1;
        if(st.x==a-1&&st.y==b-1&&st.z==c-1&&st.time<=m)//成功逃出
        return st.time;
        for(i=0;i<6;i++)
        {
            ed.x=st.x+dir[i][0];
            ed.y=st.y+dir[i][1];
            ed.z=st.z+dir[i][2];
            if(go(ed.x,ed.y,ed.z)&&!visit[ed.x][ed.y][ed.z])
            {
                visit[ed.x][ed.y][ed.z]=1;
                ed.time=st.time+1;
                if(abs(ed.x-a+1)+abs(ed.y-b+1)+abs(ed.z-c+1)+ed.time>m)//The remaining time for pruning is not enough
                continue;
                q.push(ed);
            }
        }
    }
    return -1;
}
intmain()
{
    int i,j,k,t,ans,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&a,&b,&c,&m);
        for (i = 0; i <a; i ++) //
        for(j=0;j<b;j++)
        for(k=0;k<c;k++)
        scanf("%d",&map[i][j][k]);
        ans=bfs(m);
        printf("%d\n",ans);
    }
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654436&siteId=291194637