zoukankan      html  css  js  c++  java
  • bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离

    题目:传送门 

    题解:

       毒瘤题...

       模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律

       结果....

       还是说正解吧:

       暴力的解法肯定是直接bfs,但是范围太大,肯定爆

       那么我们可以利用贪心,缩小范围,这样bfs就很快啦~

    详解代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 int dx[9]={0,1,2,1,2,-1,-2,-1,-2};
     8 int dy[9]={0,2,1,-2,-1,2,1,-2,-1};
     9 int sx,sy,ex,ey;
    10 int ans,X,Y;
    11 int x[11000],y[11000];
    12 int v[250][250];
    13 void tx()
    14 {
    15     while(1)
    16     {
    17         if(X+Y<=100)break;
    18         if(Y>X)swap(X,Y);
    19         if(X-4>Y*2)X-=4;
    20         else{X-=4;Y-=2;}
    21         ans+=2;
    22     }
    23 }
    24 int main()
    25 {
    26     scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    27     ans=0;
    28     X=abs(sx-ex);Y=abs(sy-ey);
    29     tx();
    30     memset(v,0,sizeof(v));v[110][110]=0;
    31     int head=1,tail=1;x[head]=110;y[head]=110;
    32     while(head<=tail)
    33     {
    34         for(int i=1;i<=8;i++)
    35         {
    36             int tx=x[head]+dx[i],ty=y[head]+dy[i];
    37             if(tx>=0 && tx<=210 && ty>=0 && ty<=210)
    38             {
    39                 if(v[tx][ty]==0)
    40                 {
    41                     v[tx][ty]=v[x[head]][y[head]]+1;
    42                     x[++tail]=tx;
    43                     y[tail]=ty;
    44                 }
    45             }
    46         }
    47         head++;
    48     }
    49     printf("%d
    ",ans+v[110+X][110+Y]);
    50     return 0;
    51 }
  • 相关阅读:
    Vue框架之基础知识
    Vue框架之初识
    Django组件之modelformset
    Django之小结
    Django组件之modelform
    Django之form表单详解
    jquery基础知识2
    jQuery基础知识1
    js基础知识4
    js基础知识3
  • 原文地址:https://www.cnblogs.com/CHerish_OI/p/8425451.html
Copyright © 2011-2022 走看看