zoukankan      html  css  js  c++  java
  • hdu1240 三维搜索 bfs dfs都可以

    题意,给出一个N,这是这个三空间的大小,
    然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐
    标和终点的坐标,输出到达的步数
    三维的BFS,很裸的题,不过一定要注意这一题给的坐标x表示列,y表示行,z表示层,
    实际输入的时候调试一下看看给的坐标是不是跟样例在图中所指的对象一样就行了。
    http://blog.csdn.net/iaccepted/article/details/23277717
    bfs讲得好http://iprai.hust.edu.cn/icl2002/algorithm/algorithm/commonalg/graph/traversal/bfs.htm

    bfs

      1 #include <cstdio>
      2 #include <cstdlib>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <queue>
      6 #include <climits>
      7 
      8 using namespace std;
      9 
     10 const int MAX = 11;
     11 const int INF = INT_MAX    -3;
     12 const int dirx[6]= {1,-1,0,0,0,0},diry[6]= {0,0,1,-1,0,0},dirz[6]= {0,0,0,0,1,-1};
     13 
     14 typedef struct Point
     15 {
     16     int x,y,z;
     17 };
     18 
     19 char map[MAX][MAX][MAX];
     20 int dist[MAX][MAX][MAX];
     21 int sx,sy,sz,dx,dy,dz;
     22 int minx,n;
     23 
     24 Point pp;
     25 
     26 queue<Point> que;
     27 
     28 int bfs(int x,int y,int z)
     29 {
     30     pp.x = x;
     31     pp.y = y;
     32     pp.z = z;
     33     que.push(pp);
     34     int i,tx,ty,tz;
     35     while(!que.empty())
     36     {
     37         pp = que.front();
     38         x = pp.x;
     39         y = pp.y;
     40         z = pp.z;
     41         que.pop();
     42         if(x==dx && y==dy && z==dz)break;
     43         for(i=0; i<6; ++i)
     44         {
     45             tx = x+dirx[i];
     46             ty = y+diry[i];
     47             tz = z+dirz[i];
     48             if(tx<0 || ty<0 || tz<0 || tx>=n || ty>=n || tz>=n)continue;
     49             if(dist[tz][tx][ty]!=-1)continue;
     50             if(map[tz][tx][ty]=='X')continue;
     51             dist[tz][tx][ty] = dist[z][x][y]+1;//十分巧妙的计算结果
     52             pp.x = tx;
     53             pp.y = ty;
     54             pp.z = tz;
     55             que.push(pp);
     56         }
     57     }
     58     return dist[dz][dx][dy];
     59 }
     60 
     61 int main()
     62 {
     63     //freopen("in.txt","r",stdin);
     64     char str[10];
     65     int i,j,k;
     66     while(scanf("%s %d%*c",str,&n)!=EOF)
     67     {
     68         for(k=0; k<n; ++k)
     69         {
     70             for(i=0; i<n; ++i)
     71             {
     72                 for(j=0; j<n; ++j)
     73                 {
     74                     map[k][i][j]=getchar();
     75                     dist[k][i][j] = -1;
     76                 }
     77                 getchar();
     78             }
     79         }
     80         scanf("%d %d %d",&sy,&sx,&sz);
     81         scanf("%d %d %d",&dy,&dx,&dz);
     82         scanf("%s",str);
     83         dist[sz][sx][sy] = 0;
     84         minx = bfs(sx,sy,sz);
     85         if(minx==-1)
     86         {
     87             printf("NO ROUTE
    ");
     88         }
     89         else
     90         {
     91             printf("%d %d
    ",n,minx);
     92         }
     93         /*while(!que.empty())释放队列,可有可无
     94         {
     95             que.pop();
     96         }*/
     97     }
     98     return 0;
     99 }
    100 /*
    101 #include <iostream>
    102 #define MAX 10000
    103 using namespace std;
    104 struct node
    105 {
    106     int j, i, k, step;
    107 } que[MAX];
    108 char s[10];
    109 int n;
    110 int sj, si, sk;
    111 int ej, ei, ek;
    112 char map[11][11][11];
    113 int dir[6][3]= {{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
    114 int ok;
    115 
    116 bool inmap(int j ,int i, int k)
    117 {
    118     return j >= 0 && j < n && i >= 0 && i <n && k >= 0 && k < n;
    119 }
    120 int bfs()
    121 {
    122     int front = 0, rear = 1;
    123     int ti, tj, tk;
    124     que[0].i = si;
    125     que[0].j = sj;
    126     que[0].k = sk;
    127     que[0].step = 0;
    128     map[si][sj][sk] = 'X';
    129     while(front < rear)
    130     {
    131         if(que[front].i == ei && que[front].j == ej && que[front].k == ek)
    132         {
    133             printf("%d %d
    ",n, que[front].step);
    134             return 1;
    135             break;
    136         }
    137         for(int d = 0; d < 6; d++)
    138         {
    139             ti = que[front].i + dir[d][1];
    140             tj = que[front].j + dir[d][0];
    141             tk = que[front].k + dir[d][2];
    142             if(inmap(ti, tj, tk) && map[ti][tj][tk] == 'O')
    143             {
    144                 map[ti][tj][tk] = 'X';
    145                 que[rear].j = tj;
    146                 que[rear].i = ti;
    147                 que[rear].k = tk;
    148                 que[rear++].step = que[front].step + 1;
    149             }
    150         }
    151         front++;
    152     }
    153     return 0;
    154 }
    155 int main()
    156 {
    157     while(scanf("%s%d", s, &n) != EOF)
    158     {
    159         int i, j, k;
    160         ok = 0;
    161         memset(map, 0, sizeof(map));
    162         for(i = 0; i < n; i ++)
    163             for(j = 0; j < n; j++)
    164                 for(k = 0; k < n; k++)
    165                 {
    166                     cin >>map[j][k][i];
    167                 }
    168         scanf("%d%d%d%d%d%d", &si, &sj, &sk, &ei, &ej, &ek);
    169         scanf("%s", s);
    170         ok = bfs();
    171         if(!ok)
    172             printf("NO ROUTE
    ");
    173     }
    174     return 0;
    175 }
    176 */

    dfs

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <climits>
     6 
     7 const int MAX = 11;
     8 const int INF = INT_MAX    ;//最大整数
     9 const int dirx[6]={1,-1,0,0,0,0},diry[6]={0,0,1,-1,0,0},dirz[6]={0,0,0,0,1,-1};
    10 
    11 char map[MAX][MAX][MAX];
    12 int dist[MAX][MAX][MAX];
    13 int sx,sy,sz,dx,dy,dz;
    14 int minx,n;
    15 
    16 void dfs(int x,int y,int z,int cnt){
    17     if(x<0 || y<0 || z<0 || x>=n || y>=n || z>=n)return;
    18     if(map[z][x][y]=='X')return;
    19     if(x==dx && y==dy && z==dz){
    20         if(cnt<minx)minx = cnt;
    21         return;
    22     }
    23     if(dist[z][x][y]<=cnt)return;
    24     else dist[z][x][y] = cnt;
    25     int i,tx,ty,tz;
    26     for(i=0;i<6;++i){
    27         tx = x+dirx[i];
    28         ty = y+diry[i];
    29         tz = z+dirz[i];
    30         dfs(tx,ty,tz,cnt+1);
    31     }
    32 }
    33 
    34 int main(){
    35     //freopen("in.txt","r",stdin);
    36     char str[10];
    37     int i,j,k,cnt;
    38     while(scanf("%s %d%*c",str,&n)!=EOF){
    39         for(k=0;k<n;++k){
    40             for(i=0;i<n;++i){
    41                 for(j=0;j<n;++j){
    42                     map[k][i][j]=getchar();
    43                     dist[k][i][j]=INF;
    44                 }
    45                 getchar();
    46             }
    47         }
    48         scanf("%d %d %d",&sy,&sx,&sz);
    49         scanf("%d %d %d",&dy,&dx,&dz);
    50         scanf("%s",str);
    51         cnt = 0;
    52         minx = INT_MAX;
    53         dfs(sx,sy,sz,cnt);
    54         if(minx==INT_MAX){
    55             printf("NO ROUTE
    ");
    56         }else{
    57             printf("%d %d
    ",n,minx);
    58         }
    59     }
    60 
    61 
    62     return 0;
    63 }
  • 相关阅读:
    多线程(一)高并发和多线程的关系
    spring源码浅析——IOC
    网络爬虫(java)
    数据结构—平衡二叉树
    设计模式—抽象工厂模式
    设计模式—工厂方法模式
    scala(二) Future执行逻辑解读
    java异常处理机制
    为什么覆写equals必须要覆写hashCode?
    Scala对MongoDB的增删改查操作
  • 原文地址:https://www.cnblogs.com/SSYYGAM/p/4508927.html
Copyright © 2011-2022 走看看