bfs三维地图

    xiaoxiao2026-10-01  7

    hdu1253; 题目链接;http://acm.hdu.edu.cn/showproblem.php?pid=1253; 题目大意;在一个三维空间中从左上角到右下角;在t时间内是否可以达到;三维空间内可以移动6个方向; hdu2102; 题目链接;http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目大意; 1,有两层,里面存在传送机;可以从这层这个位置传送到另外一层的相应位置; 2,在t时间内从左上角(0,0,0)处到达P处;(最优解是否小于t即可); bfs的mp三维; 1253;是三维的地图;因此要用三维的mp;这类型注意输入处理就ok; 不要去用xyz;可以用高,行,列这样好理解一些; 2102;是二维的地图但是它分为两大块;并且存在传送机;它传送到相应层的对应位置;用三维数组处理很方便; 关于2102我写过一篇博客;这里就不详细介绍了; 直接贴第一题的代码吧; 特别注意输入;;;;;;;

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<queue> #include<algorithm> #include<stack> #include<set> #include<string.h> using namespace std; int mark[52][52][52]; int mp[52][52][52]; int da[6]={1,-1,0,0,0,0}; int db[6]={0,0,1,-1,0,0}; int dc[6]={0,0,0,0,1,-1}; int a, b, c, t; struct node { int x, y, z, time; }; void bfs() { //printf("asc\n"); int i,na,nb,nc; memset(mark,0,sizeof(mark)); queue<node> q; node now,next; mark[1][1][1]=1; now.x=1;now.y=1;now.z=1;now.time=0; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); for(i = 0; i < 6; i++) { na=now.x+da[i];nb=now.y+db[i];nc=now.z+dc[i]; if(na>=1&&na<=a&&nb>=1&&nb<=b&&nc>=1&&nc<=c&&mark[na][nb][nc]==0&&mp[na][nb][nc]==0) { //printf("%d %d %d\n",na,nb,nc); next.x=na;next.y=nb;next.z=nc;next.time=now.time+1; mark[na][nb][nc]=1; if(na==a&&nb==b&&nc==c&&next.time<=t) { printf("%d\n",next.time); return ; } q.push(next); } } } printf("-1\n"); } int main() { int tt,i,j, k; scanf("%d",&tt); while(tt--) { scanf("%d %d %d %d",&a,&b,&c,&t); for(i = 1; i <= a; i++)//h; { for(j = 1; j <=b; j++)//r { for(k = 1; k <= c; k++)//l { scanf("%d",&mp[i][j][k]); } } } //printf("%d \n",mp[1][1][c-1]); bfs(); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1312504.html
    最新回复(0)