Exercise 10-2 the industrious Bee UVa808

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Solving ideas: The problem is solved by the construction method. I think for a long time, but there is no good idea. Finally realized that only need to follow the honeycomb structure in the Xoy coordinate system to draw these points can be solved. The presentation of the hive is not difficult to detect by observation, similar to the spiral structure. Arrange (0,0) as the first point, the longitudinal distance and the transverse distance are 2 units distance. This allows you to draw all the stores in the drawing. After the calculation is not difficult to know, only need to loop 60 times to the point within 10000.

The equivalent of this input is the ordinal of the point, the coordinates of the point are not difficult to get two points of the transverse distance x and the longitudinal distance y. When y≤x, the shortest distance is x. Otherwise, because both portrait and landscape are spaced 2 units, the minimum distance is actually the average of X, Y.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;const int N = 20005;const int dx[] = {-1, 0, 1, 1, 0};const int dy[] = {1, 2, 1,-1, 2};struct point{int x, y; Point () {}point (int x, int y) {this->x = X;this->y = y;}} P[n];int A, b;void init () {int pn = 1, x = 0, y = 0;p[pn++] = point (x, y), y-= 2;p[pn++] = point (x, y); for (int i = 1; I &l T;= 60; i++) {for (int j = 0; J < 5; J + +)//In order to draw 5 directions for (int k = 0; k < i; k++)//Draw along Dx[j],dy[j] Direction {x + = Dx[j], y + + dy[j];p [P n++] = point (x, y);} Y-= 2;p[pn++] = point (x, Y), for (int j = 0; J < i; j + +)//return to Y-axis {x--; y--;p [pn++] = point (x, y);}} int main () {//freopen ("T.txt", "R", stdin); INIT (); while (~SCANF ("%d%d", &a, &b) && (A | | b)) {int x = ABS (p[a].x-p[b].x); int y = ABS (P[A].Y-P[B].Y);p rintf ("The distance between cells%d and%d is", A, B), if (y <= x) printf ("%d.\n", x),//Because the horizontal two points in the graph are also spaced 2 units, so it is directly xelse printf ("%d.\n", X + (Y-x)/2);//due to the horizontal of two points, the longitudinal interval is two units, so the average value is}return 0;}

Exercise 10-2 the industrious Bee UVa808

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.