三次元の最短

即ち、別のポイントからの最短距離に三次元空間を解きます

ポータル:ゼリー
の質問の意味:3次元空間でのn * n個* n個の各点にゼリーゼリー良いか悪いかを持っています。(1,1,1)から、ゼリー悪い食の前提の下で計算された(N、N、N)は、ゼリーを食べる良いの最小数が必要です。
実践:周り、アップと各ポイントダウンし、ゼリーゼリー良いを持っているがサイドアップが良い点を持っています。spfaとすることができます。

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
char g[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int d[maxn][maxn][maxn];
int X[]={1,-1,0,0,0,0};
int Y[]={0,0,1,-1,0,0};
int Z[]={0,0,0,0,1,-1};
int n;
bool check(int x,int y,int z){
    if((0<x&&x<=n&&0<y&&y<=n&&0<z&&z<=n))
        return true;
    else return false;
}
struct ac{
    int x,y,z;int d;
};
int inq[maxn][maxn][maxn];
void SPFA(){
    memset(inq,false,sizeof inq);
    memset(d,0x3f,sizeof d);
    queue<ac> Q;
    Q.push((ac){1,1,1,1});
    inq[1][1][1]=true;
    d[1][1][1]=1;
    while(!Q.empty()){
        ac u=Q.front();
        Q.pop();
        inq[u.x][u.y][u.z]=false;
        for(int i=0;i<6;i++){
            int vx=u.x+X[i],vy=u.y+Y[i],vz=u.z+Z[i];
            if(check(vx,vy,vz)==false||g[vx][vy][vz]=='*') continue;
            if(d[vx][vy][vz]>d[u.x][u.y][u.z]+1){
                d[vx][vy][vz]=d[u.x][u.y][u.z]+1;
                if(inq[vx][vy][vz]==false){
                    Q.push((ac){vx,vy,vz,d[vx][vy][vz]});
                    inq[vx][vy][vz]=true;
                }
            }
        }
    }
}
 
int main(){
    cin>>n;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            cin>>(g[i][j]+1);
        }
    }
    SPFA();
    if(d[n][n][n]!=inf)
    cout<<d[n][n][n]<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}

ポータル:ダンジョンマスター
の質問の意味は:ポイント#に行くことができますあなたは何度も行くことができる、少なくとも場合は、EにSから尋ねたかどうか、行くことはありません。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
char g[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int d[maxn][maxn][maxn];
int X[]={1,-1,0,0,0,0};
int Y[]={0,0,1,-1,0,0};
int Z[]={0,0,0,0,1,-1};
int n,m,p;
bool check(int x,int y,int z){
    if((0<x&&x<=n&&0<y&&y<=m&&0<z&&z<=p))
        return true;
    else return false;
}
struct ac{
    int x,y,z;
};
int inq[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
void SPFA(){
    memset(inq,false,sizeof inq);
    memset(d,0x3f,sizeof d);
	queue<ac> Q;
    Q.push((ac){sx,sy,sz});
    inq[sx][sy][sz]=true;
    d[sx][sy][sz]=0;
    while(!Q.empty()){
    	ac u=Q.front();
    	Q.pop();
    	inq[u.x][u.y][u.z]=false;
    	for(int i=0;i<6;i++){
    		int vx=u.x+X[i],vy=u.y+Y[i],vz=u.z+Z[i];
    		if(check(vx,vy,vz)==false||g[vx][vy][vz]=='#') continue;
    		if(d[vx][vy][vz]>d[u.x][u.y][u.z]+1){
    			d[vx][vy][vz]=d[u.x][u.y][u.z]+1;
    			if(inq[vx][vy][vz]==false){
    				Q.push((ac){vx,vy,vz});
    				inq[vx][vy][vz]=true;
				}
			}
		}
	}
}

int main(){
    while(~scanf("%d%d%d",&n,&m,&p)){
        if(n==m&&m==p&&p==0) break;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                cin>>(g[i][j]+1);
                for(int k=1;k<=p;++k){
                    if(g[i][j][k]=='S'){
                        sx=i;sy=j;sz=k;
                    }
                    if(g[i][j][k]=='E'){
                        ex=i;ey=j;ez=k;
                    }
                }
            }
        }
        SPFA();
        if(d[ex][ey][ez]!=inf)
        cout<<"Escaped in "<<d[ex][ey][ez]<<" minute(s)."<<endl;
        else
            cout<<"Trapped!"<<endl;
    }
    return 0;
}
公開された96元の記事 ウォン称賛11 ビュー2260

おすすめ

転載: blog.csdn.net/weixin_43769146/article/details/104059071