zoukankan      html  css  js  c++  java
  • bnuoj 25662 A Famous Grid (构图+BFS)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <math.h>
      5 #include <algorithm>
      6 #include <stack>
      7 #include <queue>
      8 
      9 #define N 150
     10 using namespace std;
     11 
     12 struct Nod
     13 {
     14     int x,y,step;
     15 }node[15000];
     16 
     17 int map[N][N];
     18 int mark[N][N];
     19 int prim[15000];
     20 
     21 int cx[]={1,0,-1,0};
     22 int cy[]={0,1,0,-1};
     23 
     24 int cnt=1;
     25 
     26 void getMap(int x,int y,int re)
     27 {
     28     while(1)
     29     {
     30         int dx,dy;
     31         dx = x + cx[(re+1)%4];
     32         dy = y + cy[(re+1)%4];
     33         if(!(dx>=0&&dx<N&&dy>=0&&dy<N))
     34         {
     35             break;
     36         }
     37         if(map[dx][dy]==0)
     38         {
     39             map[dx][dy]= ++cnt;
     40             node[cnt].x = dx;
     41             node[cnt].y = dy;
     42           //  getMap(dx,dy,(re+1)%4);
     43             x = dx;
     44             y = dy;
     45             re = (re+1)%4;
     46         }
     47         else
     48         {
     49             dx = x + cx[re%4];
     50             dy = y + cy[re%4];
     51             if(!(dx>=0&&dx<N&&dy>=0&&dy<N))
     52             {
     53                 break ;
     54             }
     55             map[dx][dy]= ++cnt;
     56             node[cnt].x = dx;
     57             node[cnt].y = dy;
     58            // getMap(dx,dy,re);
     59             x = dx;
     60             y = dy;
     61         }
     62     }
     63 }
     64 
     65 int bfs(int x,int y)
     66 {
     67     memset(mark,0,sizeof(mark));
     68     queue<Nod> Q;
     69     Nod temp;
     70     temp.x = node[x].x;
     71     temp.y = node[x].y;
     72     temp.step = 0;
     73     Q.push(temp);
     74     mark[temp.x][temp.y] = 1;
     75     while(!Q.empty())
     76     {
     77         Nod td = Q.front();
     78         Q.pop();
     79         if(td.x==node[y].x&&td.y==node[y].y)
     80         {
     81             return td.step;
     82         }
     83         int i;
     84         for(i=0;i<4;i++)
     85         {
     86             temp.x = td.x + cx[i];
     87             temp.y = td.y + cy[i];
     88             temp.step = td.step + 1;
     89             if(map[temp.x][temp.y]>=1&&map[temp.x][temp.y]<=14000&&prim[map[temp.x][temp.y]]&&!mark[temp.x][temp.y])
     90             {
     91                 mark[temp.x][temp.y]=1;
     92                 Q.push(temp);
     93             }
     94         }
     95     }
     96     return -1;
     97 }
     98 
     99 
    100 int main()
    101 {
    102     cnt=1;
    103     int cas=1;
    104     map[60][60]=1;
    105     getMap(60,60,0);
    106     node[1].x = 60;
    107     node[1].y = 60;
    108    // cout<<cnt<<endl;
    109     int i,j;
    110     prim[1]=1;
    111     for(i=2;i<15000;i++)
    112     {
    113         for(j=2;j*j<=i;j++)
    114         {
    115             if(i%j==0)
    116             {
    117                 prim[i]=1;
    118                 break;
    119             }
    120         }
    121     }
    122  //   for(i=1;i<=100;i++)
    123    // if(!prim[i])cout<<i<<" ";
    124 
    125     int x,y;
    126     while(~scanf("%d%d",&x,&y))
    127     {
    128         printf("Case %d: ",cas++);
    129         int ans = bfs(x,y);
    130         if(ans==-1)
    131         {
    132             puts("impossible");
    133         }
    134         else
    135         {
    136             printf("%d
    ",ans);
    137         }
    138     }
    139 
    140     return 0;
    141 }
  • 相关阅读:
    十七 SpringCloud Alibaba入门简介
    十六、Spring Cloud Sleuth 分布式请求链路追踪
    Linux下安装mongo
    MongoDB零基础入门到高级进阶-尚学堂
    十五、Spring Cloud Stream 消息驱动
    十四、SpringCloud Bus 消息总线
    十二、Gateway新一代网关
    十一、Zuul路由网关
    十、Hystrix熔断器(三)服务熔断
    十、Hystrix 断路器(二)服务降级
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3406901.html
Copyright © 2011-2022 走看看