"Bzoj 1193" [HNOI2006] horse distance

Source: Internet
Author: User

1193: [HNOI2006] Horse distance time limit: ten Sec Memory Limit: 162 MB
Submit: 919 Solved: 419
[Submit] [Status] Description

Input

Contains only 4 integers separated by a space, respectively, Xp,yp,xs,ys. And all of them are less than 10000000.

Output

Contains an integer that represents the minimum number of horse moves to pass from the point P to S.

Sample Input1 2 7 9Sample Output5

Greed + violence/play the Watch pattern


First, greed:

First of all two coordinates subtract absolute value, make the problem become from (x, y) to (0,0) the shortest distance, then first greedy, let (x, y) Close (0,0), the remaining road directly violent BFS can.


#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < Cstdlib> #include <queue>using namespace Std;int x,y,fx[10][3],dis[105][105];struct point{int x, y;}; void BFs () {memset (dis,-1,sizeof (dis));d is[x][y]=0;fx[1][1]=fx[2][1]=1,fx[3][1]=fx[4][1]=-1,fx[5][1]=fx[6][1]=2, Fx[7][1]=fx[8][1]=-2;fx[1][2]=fx[3][2]=2,fx[2][2]=fx[4][2]=-2,fx[5][2]=fx[7][2]=1,fx[6][2]=fx[8][2]=-1;queue <Point> Q; Point P;p.x=x,p.y=y;q.push (P), while (!q.empty ()) {point X;x=q.front (), Q.pop (); for (int i=1;i<=8;i++) {int nowx=x.x+ Fx[i][1],nowy=x.y+fx[i][2];if (nowx<0| | nowy<0| | nowx>100| | nowy>100) continue;if (dis[nowx][nowy]!=-1) continue;dis[nowx][nowy]=dis[x.x][x.y]+1; Point X; X.x=nowx,x.y=nowy;q.push (x); if (nowx==50&&nowy==50) return;}}} int main () {int xp,yp,xs,ys;scanf ("%d%d%d%d", &xp,&yp,&xs,&ys), X=abs (XS-XP), Y=abs (YS-YP); int ans=0; while (x+y>=50) {if (x<y) swap (x, y), if (x-4>=y*2) X-=4;else x-=4,y-=2;ans+=2;} X+=50,Y+=50;BFS ();p rintf ("%d\n", ans+dis[50][50]); return 0;}




Then say the pattern to find the law:

(It took a long time for this 0ms procedure.) )


0 in the center of the graph, his coordinates are (0,0)


It can be found that the color of the most outer ring color is interval distribution.


Now consider only the upper right corner of the block, and only see the line ordinal >= column ordinal part, all the others are ignored.


The number of lines after the >=3 can almost say that the color is spaced out (note is almost!). ), it is easy to find the distribution pattern, 2 is 1-2 rows, 4 is 3-6 lines, 6 is 7-10 lines ... 3 is 1-4 rows, 5 is 5-8 rows, and 7 is 9-12 rows. Even and odd have a fought feeling.


I did not notice "almost", directly according to the interval distribution to do, took 90 points.


Then look closely at the graph and discover the special points in each row, marked with a circle.


Find them all around the diagonal, then you can move to the diagonal by walking the word (the number of rows than the number of columns each time minus 1), and then you can find that the number on the diagonal is regular from (to) the beginning of 444666888 ...


So for the special treatment can go to the diagonal, can not go to the diagonal according to the previous distribution law to do.




Sentiment:

Large-scale greedy + small-scale violence is worth learning.


"Bzoj 1193" [HNOI2006] horse distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.