zoukankan      html  css  js  c++  java
  • nyoj-58-最小步数

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<math.h>
     5 #define L 9
     6 using namespace std;
     7 bool map[9][9]={{1,1,1,1,1,1,1,1,1},
     8                 {1,0,0,1,0,0,1,0,1},
     9                 {1,0,0,1,1,0,0,0,1},
    10                 {1,0,1,0,1,1,0,1,1},
    11                 {1,0,0,0,0,1,0,0,1},
    12                 {1,1,0,1,0,1,0,0,1},
    13                 {1,1,0,1,0,1,0,0,1},
    14                 {1,1,0,1,0,0,0,0,1},
    15                 {1,1,1,1,1,1,1,1,1}};
    16 int num[9][9],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},dis[81],temp[9][9];//num数组存放到某一点的最小步数,dis为队列,temp标记是否访问过
    17 int bfs(int x1,int y1,int x2,int y2);
    18 int main()
    19 {
    20     int n,a,b,c,d;
    21     scanf("%d",&n);
    22     while(n--)
    23     {
    24         scanf("%d%d%d%d",&a,&b,&c,&d);
    25         printf("%d
    ",bfs(a,b,c,d));
    26     }
    27     return 0;
    28 }
    29 
    30 int bfs(int x1,int y1,int x2,int y2)
    31 {
    32     memset(num,0,sizeof(num));
    33     memset(temp,0,sizeof(temp));
    34     int l=0,r=0,t;
    35     t=x1*L+y1;
    36     dis[r++]=t;
    37     temp[x1][y1]=1;
    38     num[x1][y1]=0;
    39     while(l<r)
    40     {
    41         t=dis[l++];
    42         int x,y,bx,by;
    43         x=t/L;y=t%L;
    44         for(int i=0;i<4;i++)
    45         {
    46             bx=x+dx[i];by=y+dy[i];
    47             if(temp[bx][by]==0&&bx>=0&&bx<L&&by>=0&&by<L&&map[bx][by]==0)
    48             {
    49                 dis[r++]=bx*L+by;
    50                 num[bx][by]=num[x][y]+1;
    51                 temp[bx][by]=1;
    52             }
    53         }
    54     }
    55     return num[x2][y2];
    56 }

    此题用广搜 打出步数表  然后输出 其实可以在找到这点后就可以跳出了  没必要全循环完的我的第一次广搜代码 见谅啊  各位

  • 相关阅读:
    Dynamics 365 CRM large instance copy
    Dynamics CRM Plug-in
    Dynamics CRM Publisher
    Dynamics 365 CRM Free up storage 清理Dynamics 365 CRM的空间
    账户和联系人 Accounts and Contacts 译
    Dynamics CRM Instances
    Dynamics CRM Solution
    微软Azure通知中心 (Azure Notification Hubs)
    CLR(Common Language Runtime) 公共语言运行库
    构建Apache Web服务器
  • 原文地址:https://www.cnblogs.com/nylg-haozi/p/3199912.html
Copyright © 2011-2022 走看看