Pool Time Limit: 3000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
There is a farm with many water supply devices on it. Now the farm owner wants to enclose these water supply devices with a fence to prevent their own livestock from drinking water, each pool is marked with its own coordinates. Now you need to write a program to use the shortest fence to enclose these water supply devices! (There are enough fences and the length is variable)
-
-
Input
-
The first line inputs N, which indicates that N groups of test data are used (1 <= N <= 10)
M is input in the second row, which indicates that the test data of this group contains m water supply devices (3 <= m <= 100)
Next, the m line represents the vertical and horizontal coordinates of each water supply device.
-
Output
-
The coordinate points of each fence passing through each water supply device are output from small to large according to the X axis coordinate value. If the X axis coordinate value is the same, then the output is from small to large according to the Y axis coordinate value.
-
Sample Input
-
140 01 12 33 0
-
Sample output
-
0 02 33 0
Code:
I just wrote a question in the same method, which is almost the same as HDU 1348 and POJ 1113.
I thought about it.
I thought the point obtained after the first sorting is ordered. What is the result? WA?
So I changed the queue storage to array storage, and sorted it once ~
# Include <stdio. h> # include <algorithm> # include <math. h> # include <string. h> using namespace std; typedef struct point {int x, y;} point; point v [105], res [105]; int vis [105], ver; int cmp (point a, point B) {if (. x <B. x) return 1; else if (. x = B. x) {if (. y <B. y) return 1; return 0;} return 0;} int calc (point a, point B, point c) // Cross Product {return (B. x-a.x) * (c. y-a.y)-(c. x-a.x) * (B. y-a.y);} double dis (point a, point B) {return s Qrt (double) (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} double solve () {vis [0] = 1; int in, I, bu, k = 0; in = 0; res [k ++] = v [in]; while (1) {bu =-1; for (I = 0; I <ver; I ++) {if (! Vis [I]) {bu = I; break;} if (bu =-1) break; for (I = 0; I <ver; I ++) {if (calc (v [in], v [bu], v [I])> 0 | (calc (v [in], v [bu], v [I]) = 0 & dis (v [in], v [I])> dis (v [in], v [bu]) bu = I;} if (vis [bu]) break; res [k ++] = v [bu]; vis [bu] = true; in = bu ;} sort (res, res + k, cmp); // here !!! For (I = 0; I <k; I ++) printf ("% d \ n", res [I]. x, res [I]. y);} int main () {int I, t; scanf ("% d", & t); while (t --) {scanf ("% d ", & ver); for (I = 0; I <ver; I ++) scanf ("% d", & v [I]. x, & v [I]. y); sort (v, v + ver, cmp); memset (vis, 0, sizeof (vis); solve ();} return 0 ;}