bzoj1193: [HNOI2006]马步距离
机房考试居然出了这题。。。
考场上个个大佬都写个贪心狗过了,就我找规律AC。。
首先先把dis搞出来方便。
然后我就打了个表:
 13 14 13 14 13 14 13 14 13 14 13 14 13 14 13 14*15 14 15 16 15 16 17 16 17 18
 12 13 12 13 12 13 12 13 12 13 12 13 12 13*14 13 14 15 14 15 16 15 16 17 16 17
 13 12 13 12 13 12 13 12 13 12 13 12 13 12 13*14 13 14 15 14 15 16 15 16 17 16
 12 11 12 11 12 11 12 11 12 11 12 11 12*13 12 13 14 13 14 15 14 15 16 15 16 17
 11 12 11 12 11 12 11 12 11 12 11 12 11 12*13 12 13 14 13 14 15 14 15 16 15 16
 10 11 10 11 10 11 10 11 10 11 10 11*12 11 12 13 12 13 14 13 14 15 14 15 16 15
 11 10 11 10 11 10 11 10 11 10 11 10 11*12 11 12 13 12 13 14 13 14 15 14 15 16
 10  9 10  9 10  9 10  9 10  9 10*11 10 11 12 11 12 13 12 13 14 13 14 15 14 15
  9 10  9 10  9 10  9 10  9 10  9 10*11 10 11 12 11 12 13 12 13 14 13 14 15 14
  8  9  8  9  8  9  8  9  8  9*10  9 10 11 10 11 12 11 12 13 12 13 14 13 14 15
  9  8  9  8  9  8  9  8  9  8  9*10  9 10 11 10 11 12 11 12 13 12 13 14 13 14
  8  7  8  7  8  7  8  7  8* 9  8  9 10  9 10 11 10 11 12 11 12 13 12 13 14 13
  7  8  7  8  7  8  7  8  7  8* 9  8  9 10  9 10 11 10 11 12 11 12 13 12 13 14
  6  7  6  7  6  7  6  7* 8  7  8  9  8  9 10  9 10 11 10 11 12 11 12 13 12 13
  7  6  7  6  7  6  7  6  7* 8  7  8  9  8  9 10  9 10 11 10 11 12 11 12 13 14
  6  5  6  5  6  5  6* 7  6  7  8  7  8  9  8  9 10  9 10 11 10 11 12 13 12 13
  5  6  5  6  5  6  5  6* 7  6  7  8  7  8  9  8  9 10  9 10 11 12 11 12 13 14
  4  5  4  5  4  5* 6  5  6  7  6  7  8  7  8  9  8  9 10 11 10 11 12 13 12 13
  5  4  5  4  5  4  5* 6  5  6  7  6  7  8  7  8  9 10  9 10 11 12 11 12 13 14
  4  3  4  3  4* 5  4  5  6  5  6  7  6  7  8  9  8  9 10 11 10 11 12 13 12 13
  3  4  3  4  3  4* 5  4  5  6  5  6  7  8  7  8  9 10  9 10 11 12 11 12 13 14
  2  3  2  3* 4  3  4  5  4  5  6  7  6  7  8  9  8  9 10 11 10 11 12 13 12 13
  3  2  3  2  3  4  3  4  5  6  5  6  7  8  7  8  9 10  9 10 11 12 11 12 13 14
  2  1  4  3  2  3  4  5  4  5  6  7  6  7  8  9  8  9 10 11 10 11 12 13 12 13
  3  2  1  2  3  4  3  4  5  6  5  6  7  8  7  8  9 10  9 10 11 12 11 12 13 14
  0  3  2  3  2  3  4  5  4  5  6  7  6  7  8  9  8  9 10 11 10 11 12 13 12 13
通过观察法,观察第一列发现后面的都是那种ABAB的形式。。然后就可以表示出来。
然后就发现*号前面都是两个数不断重复。那么我就把*号位置找出来,发现有奇偶性规律,那*号的位置也可以表示出来。
然后disx小于*号位置的就搞定了。
再看*号后面,发现三个一组,每组+1,所以又可以用规律搞搞,那就完了。
PS:下面有几行不符合的,就打个表。。。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int main() { int xs,ys,xp,yp,ans; scanf("%d%d%d%d",&xs,&ys,&xp,&yp); int disx=abs(xs-xp),disy=abs(ys-yp); if(disx>disy)swap(disx,disy); if(disx==0&&disy==0)printf("0\n"); else if(disx==0&&disy==1)printf("3\n"); else if(disx==1&&disy==1)printf("2\n"); else if(disx==0&&disy==2)printf("2\n"); else if(disx==1&&disy==2)printf("1\n"); else if(disx==2&&disy==2)printf("4\n"); else if(disx==0&&disy==3)printf("3\n"); else if(disx==1&&disy==3)printf("2\n"); else if(disx==2&&disy==3)printf("3\n"); else if(disx==3&&disy==3)printf("2\n"); else { int first=(disy+2)/2+((disy%4==3)?1:0)+((disy%4==0)?-1:0); int second; if(disy%4<=1)second=first+1; else second=first-1; int tip; if(disy%2==0)tip=(disy-2)/2+3; else tip=(disy-3)/2+5; if(disx<=tip)printf("%d\n",(disx%2==0)?first:second); else { disx-=tip; int last; if(disy%4<=1)last=(tip%2==0)?first:second+1; else last=(tip%2==0)?first:second; last++; printf("%d\n",disx/3+last-((disx%3==2)?1:0)); } } return 0; }

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号