zoukankan      html  css  js  c++  java
  • NYOJ 592spiral grid(BFS)

    建图(蛇形填数)+求素数+bfs。

    注意:数据可以是素数。当起点与终点不同时,终点为素数时,结果就是impossible,起点与终点相同时,结果都是0.

     1 #include<stdio.h>
     2 #include<string.h>
     3 int vis[101][101],bfs[10010];
     4 int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
     5 int A[101][101];
     6 int prim[10002]={0};
     7 void prime()
     8 {
     9     int i,j;
    10     memset(prim,0,sizeof(prim));
    11     prim[1] = 1;
    12     for(i=2;i<=10000;i++)
    13     {
    14         if(!prim[i])
    15             for(j = i*i; j<=10000; j+=i)
    16                 prim[j] = 1;
    17     }
    18 }
    19 void map()
    20 {
    21     int i,row_s,colu_s,row_e,colu_e;
    22     int S = 10000;
    23     row_s = colu_s = 0;
    24     row_e = colu_e = 99;
    25     while(S>0)
    26     {
    27         for(i=colu_s;i<=colu_e;i++)A[row_s][i] = S--;//左—》右
    28         row_s +=1;//行开始位置+1
    29         for(i=row_s;i<=row_e;i++)A[i][colu_e] = S--;//上—》下
    30         colu_e -=1;//列最后位置-1
    31         for(i=colu_e;i>=colu_s;i--)A[row_e][i] = S--;//右—》左
    32         row_e -=1;//行最后位置-1
    33         for(i=row_e;i>=row_s;i--)A[i][colu_s] = S--;//下—》上
    34         colu_s +=1;//列开始位置+1
    35     }
    36 }
    37 int main()
    38 {
    39     //freopen("in.txt","r",stdin);
    40     int a,b,c,d,a_x,a_y,b_x,b_y,M,Mm,x,y,i,j,k,n,s,t=1,f;
    41     int rear,first,end;
    42     prime();//素数存入prim[]中
    43     map();//地图数据存入A[][]中
    44     while(~scanf("%d%d",&a,&b))
    45     {
    46         memset(vis,0,sizeof(vis));
    47         memset(bfs,0,sizeof(bfs));
    48         //找出始点、终点坐标
    49         for(i=0;i<100;i++)
    50             for(int j=0;j<100;j++){
    51                 if(A[i][j] == a){
    52                     a_x = i;
    53                     a_y = j;
    54                 }
    55                 if(A[i][j] == b){
    56                     b_x = i;
    57                     b_y = j;
    58                 }
    59             }
    60         //宽度遍历
    61         bfs[0]=a_x*100+a_y;
    62         end = b_x*100+b_y;//终点位置
    63         vis[a_x][a_y]=1;
    64         rear=f=k=1;
    65         printf("Case %d: ",t++);
    66         if(bfs[0]==end)f=0;
    67         for(first=n=s=j=0;f;)
    68         {
    69             if(first==1){k=s;n++;s=0;}
    70             if(first>k+j){
    71                 n++;//记录步数
    72                 k=s;
    73                 s=0;
    74                 j=first-1;
    75             }
    76             M=bfs[first++];
    77             if(first>rear || rear>10000)break;
    78             if(M==end){f=0;break;}
    79             x=M/100;y=M%100;//坐标
    80             for(i=0;i<4;i++)
    81             {
    82                 c=x+dx[i];d=y+dy[i];
    83                 if(c>=0 && c<100 && d>=0 && d<100 && !vis[c][d] && prim[A[c][d]]){
    84                     Mm=100*c+d;
    85                     bfs[rear++]=Mm;
    86                     vis[c][d]=1;
    87                     s++;
    88                 }
    89             }
    90         }
    91         if(!f)
    92             printf("%d
    ",n);
    93         else
    94             printf("impossible
    ");
    95     }
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    C# 解决组合优化问题
    <@spring.message "index.title"/>
    服务容错处理库Polly使用
    Pycharm使用入门
    JS知识点
    design pattern
    java的NIO
    Promise
    Docker Compose + Spring Boot + Nginx + Mysql
    苹果开发者账号如何多人协作进行开发和真机调试XCode
  • 原文地址:https://www.cnblogs.com/qiu520/p/3217371.html
Copyright © 2011-2022 走看看