本文主要是介绍hdu1240、hdu1253(三维搜索题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、从后往前输入,(x,y,z);
2、从下往上输入,(y , z, x);
3、从左往右输入,(z,x,y);
hdu1240代码如下:
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>#define N 15
#define eps 1e-9
#define P system("pause")
using namespace std;
struct node{int x,y,z;int step;
};
int n;
char a[N][N][N];
int vis[N][N][N];
int sx,sy,sz,ex,ey,ez;
int d[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
queue<node> q;int bfs()
{while(!q.empty()){node u=q.front();// printf("%d %d %d\n",u.x,u.y,u.z);q.pop();if(u.x==ex&&u.y==ey&&u.z==ez) return u.step;for(int i=0;i<6;i++){int nx=u.x+d[i][0];int ny=u.y+d[i][1];int nz=u.z+d[i][2];if(nx>=0&&nx<n && ny>=0&&ny<n && nz>=0&&nz<n && !vis[nx][ny][ny]&&a[nx][ny][nz]!='X'){node v;v.x=nx;v.y=ny;v.z=nz;v.step=u.step+1;vis[nx][ny][nz]=1;q.push(v); } }} return -1;
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);ccchar s[20];int i,j;while(scanf("%s",&s)!=EOF){while(!q.empty()) q.pop(); scanf("%d",&n);for(int i=0;i<n;i++) //三维数组的输入(从下往上输入) for(int j=0;j<n;j++)scanf("%s",a[i][j]);// scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez); //习惯上的(x,y,z) 变成了实际中的(y,z,x)scanf("%d%d%d%d%d%d",&sz,&sy,&sx,&ez,&ey,&ex); //注意点,关键 scanf("%s",s);memset(vis,0,sizeof(vis));vis[sx][sy][sz]=1; node u;u.x=sx;u.y=sy;u.z=sz;u.step=0;q.push(u);int k=bfs();if(k==-1) printf("NO ROUTE\n");else printf("%d %d\n",n,k); } //P; return 0;
}
hdu1253代码如下:
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>#define eps 1e-9
#define N 55
#define P system("pause")
using namespace std;
int a,b,c,T;
int vis[N][N][N] ;
int d[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
struct node{int x,y,z;int step;
};
queue<node> q;
int bfs()
{node u,v;while(!q.empty()) {u=q.front();q.pop();// cout<<u.x<<" "<<u.y<<" "<<u.z<<" "<<u.step<<endl;if(u.x==a-1 && u.y==b-1 &&u.z==c-1) return u.step;if(u.step>=T) return -1;for(int i=0;i<6;i++){int nx=u.x+d[i][0];int ny=u.y+d[i][1];int nz=u.z+d[i][2];if(!vis[nx][ny][nz]&&nx>=0&&nx<a&&ny>=0&&ny<b&&nz>=0&&nz<c){vis[nx][ny][nz]=1;v.x=nx;v.y=ny;v.z=nz;v.step=u.step+1;q.push(v); }}} return -1;
}int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);ccint t;scanf("%d",&t);while(t--){while(!q.empty()) q.pop(); scanf("%d%d%d%d",&a,&b,&c,&T);int i,j,k;for(i=0;i<a;i++)for(j=0;j<b;j++)for(k=0;k<c;k++)scanf("%d",&vis[i][j][k]); //从后往前输入node u;u.x=u.y=u.z=u.step=0;vis[0][0][0]=1;q.push(u);int z=bfs();printf("%d\n",z); } // P; return 0;
}
这篇关于hdu1240、hdu1253(三维搜索题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!