• HDU 1240


     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<cstdio>
     4 #include<cstring>
     5 int mmin,n;
     6 using namespace std;
     7 const int qq=11;
     8 char map[qq][qq][qq];
     9 int dx[]={0,0,-1,1,0,0},dy[]={-1,1,0,0,0,0},dz[]={0,0,0,0,1,-1};
    10 int tx,ty,tz;
    11 void dfs(int sz,int sy,int sx,int cnt)
    12 {
    13     if(cnt>=mmin)    return;
    14     if(sx<0||sy<0||sz<0||sx>=n||sy>=n||sz>=n)    return;
    15     if(sz==tz&&sy==ty&&sx==tx){
    16         if(cnt<mmin)    mmin=cnt;
    17         return;
    18     }
    19     for(int i=0;i<6;++i){
    20         if(map[sz+dz[i]][sy+dy[i]][sx+dx[i]]!='X'){
    21             map[sz+dz[i]][sy+dy[i]][sx+dx[i]]='X';
    22             dfs(sz+dz[i],sy+dy[i],sx+dx[i],cnt+1);
    23             map[sz+dz[i]][sy+dy[i]][sx+dx[i]]='O';
    24         }
    25     }
    26     return;
    27 }
    28 int main()
    29 {
    30     char s[15];
    31     while(scanf("%s %d%*c",s,&n)!=EOF){
    32         mmin=1e6;
    33         for(int x,y,z=0;z<n;++z)
    34             for(y=0;y<n;++y){
    35                 for(x=0;x<n;++x){
    36                     map[z][y][x]=getchar();
    37                 }
    38                 getchar();
    39             }
    40         int sx,sy,sz;
    41         scanf("%d %d %d",&sx,&sy,&sz);
    42         scanf("%d %d %d",&tx,&ty,&tz);scanf("%s",s);
    43         map[sz][sy][sx]='X';
    44         dfs(sz,sy,sx,0);
    45         if(mmin==1e6)    printf("NO ROUTE
    ");
    46         else            printf("%d %d
    ",n,mmin);
    47     }
    48 }

    搜索第二题

    这是我模仿1010写出来的一段代码,说到底也剪枝了吧... 虽然没什么乱用

    上面代码测试到第三组数据就超时了.  主要原因是首先要找到一个解才能起到一个剪枝的效果,如果一开始找到的就是那个最大的解的话,这剪枝也没什么效果了

    所以参考了别人的代码    下面的剪枝效果就比较好了,记录出发点到每一点的最小步数.但是先要标记一下

     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<cstdio>
     4 #include<cstring>
     5 int mmin,n;
     6 const int MIN=1e7;
     7 using namespace std;
     8 const int qq=11;
     9 char map[qq][qq][qq];
    10 int dis[qq][qq][qq];
    11 int dx[]={0,0,-1,1,0,0},dy[]={-1,1,0,0,0,0},dz[]={0,0,0,0,1,-1};
    12 int tx,ty,tz;
    13 void dfs(int sz,int sy,int sx,int cnt)
    14 {
    15     if(map[sz][sy][sx]=='X')    return;
    16     if(sx<0||sy<0||sz<0||sx>=n||sy>=n||sz>=n)    return;
    17     if(sz==tz&&sy==ty&&sx==tx){
    18         if(cnt<mmin)    mmin=cnt;
    19         return;
    20     }
    21     if(dis[sz][sy][sx]<=cnt)    return;
    22     else                    dis[sz][sy][sx]=cnt;
    23     int dxx,dyy,dzz;
    24     for(int i=0;i<6;++i){
    25         dxx=sx+dx[i];dyy=sy+dy[i];dzz=sz+dz[i];
    26         dfs(dzz,dyy,dxx,cnt+1);
    27     }
    28     return;
    29 }
    30 int main()
    31 {
    32     char s[15];
    33     while(scanf("%s %d%*c",s,&n)!=EOF){
    34         mmin=1e8;
    35         for(int x,y,z=0;z<n;++z)
    36             for(y=0;y<n;++y){
    37                 for(x=0;x<n;++x){
    38                     map[z][y][x]=getchar();
    39                     dis[z][y][x]=MIN;
    40                 }
    41                 getchar();
    42             }
    43         int sx,sy,sz;
    44         scanf("%d %d %d",&sx,&sy,&sz);
    45         scanf("%d %d %d",&tx,&ty,&tz);
    46         scanf("%s",s);
    47         dfs(sz,sy,sx,0);
    48         if(mmin==1e8)    printf("NO ROUTE
    ");
    49         else            printf("%d %d
    ",n,mmin);
    50     }
    51 }

    继续出发!!!搜索

  • 相关阅读:
    python day04 列表增删改查
    python day03 字符串操作
    python day02 格式化输出逻辑运算
    面试题
    python基础练习题(一)
    编译安装PostgreSQL
    Python函数初识二
    Python函数初识
    笨办法学Python
    笨办法学Python
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5135657.html
Copyright © 2020-2023  润新知