欢迎使用CSDN-markdown编辑器

#include<stdio.h>
#include<string.h>
int book[10][50][50],n,m,t,f,a[6][3]={0,0,-1,0,0,1,0,1,0,0,-1,0,1,0,0,-1,0,0};
char str[10][50][50];
void dfs(int x,int z,int y,int step)
{
    int tx,ty,tz,i;
    if(str[x][z][y]=='P')
    {   //printf("step==%d\n",step);
        if(step<=t)
            f=1;
        return ;
    }
        //printf("x==%d z==%d y==%d\n",x,z,y);
    if(str[x][z][y]=='.'||str[x][z][y]=='S')
    {
        for(i=0;i<4;i++)
            {
                tx=x+a[i][0];
                ty=y+a[i][1];
                tz=z+a[i][2];
            if(tx>=0&&tx<2&&ty>=0&&ty<m&&tz>=0&&tz<n&&!book[tx][tz][ty]&&str[tx][tz][ty]!='*')
                {
                    book[tx][tz][ty]=1;
                    dfs(tx,tz,ty,step+1);
                    if(f==1) return ;
                    book[tx][tz][ty]=0;
                }
            }
    }
    if(str[x][z][y]=='#')
    {
            if(x==0)
                {
                    tx=x+a[4][0];
                    ty=y+a[4][1];
                    tz=z+a[4][2];
                }
            else if(x==1)
                {
                    tx=x+a[5][0];
                    ty=y+a[5][1];
                    tz=z+a[5][2];
                }
                //printf("tx==%d,tz==%d,ty==%d\n",tx,tz,ty);
            if(tx>=0&&tx<2&&ty>=0&&ty<m&&tz>=0&&tz<n&&!book[tx][tz][ty]&&str[tx][tz][ty]!='*')
                {
                    book[tx][tz][ty]=1;
                    dfs(tx,tz,ty,step);
                    if(f) return ;
                    book[tx][tz][ty]=0;
                }
    }
    return ;
}
int main()
{
    int i,j,k;
    scanf("%d",&k);
    while(k--)
    {
        scanf("%d%d%d",&n,&m,&t);
        for(i=0;i<2;i++)
            for(j=0;j<n;j++)
                scanf("%s",str[i][j]);
        memset(book,0,sizeof(book));
        book[0][0][0]=1;
        f=0;
        dfs(0,0,0,0);
        if(f) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

你可能感兴趣的:(欢迎使用CSDN-markdown编辑器)