Pku1984 (navigation nightmare)

Source: Internet
Author: User

This question is okay. It is a little abstract when we find the relative coordinates, but we can still understand it. Just draw a coordinate pair and you will know it.

Another is to check the application of the set, because it is necessary to determine whether the point knows the specific coordinates at a certain query location. The problem is to convert whether the two points belong to the same set and belong to the same set, we know the coordinates relative to the root node. We can directly find the Manhattan distance. Otherwise, we cannot find the distance.

CodeWith the following explanations:

# Include <stdio. h> # include <string. h> # define maxn 40010int n, m, K; struct node {int X, Y, F;} f [maxn]; struct node1 {int A, B, D; char ch;} e [maxn]; int find (int x) {If (x = f [X]. f) return f [X]. f; int T = find (F [X]. f); F [X]. X + = f [f [X]. f]. x; F [X]. Y + = f [f [X]. f]. y; F [X]. F = T; return f [X]. f ;}// query the root node and compress the path. When merging a set, only the root node of a set is merged into another set, that is, only the relative coordinates of the root node are modified. // you can find the root node and modify the relative coordinates of the node. Note that void Union (int u, int V, int W, char c) {int A = find (U); int B = find (V); F [B]. F = A; // merge Set B into the set aint temp1 = f [v]. x, temp2 = f [v]. y; Switch (c) // This is a better understanding of {Case 'E': F [B]. X = F [u]. X + W; F [B]. y = f [u]. y; break; Case 'W': F [B]. X = F [u]. x-W; F [B]. y = f [u]. y; break; Case 'N': F [B]. y = f [u]. Y + W; F [B]. X = F [u]. x; break; Case's ': F [B]. y = f [u]. y-W; F [B]. X = F [u]. x; break;} f [B]. x-= temp1; // calculates the coordinate f [B] of the root node of the set where u is located relative to v. y-= temp2;} int ABS (int A, int B) {If (A> B) return a-B; else return B-A;} int main () {int I, j, S,, B; scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) {f [I]. F = I; F [I]. X = F [I]. y = 0 ;}for (I = 1; I <= m; I ++) {getchar (); scanf ("% d % C ", & E [I]. a, & E [I]. b, & E [I]. d, & E [I]. ch);} s = 0; scanf ("% d", & K); While (k --) {scanf ("% d", &, & B, & J); While (S <j) // here, as the query location is increasing, as long as S is smaller than the query location, merge {s ++; union (E [s]. a, E [s]. b, E [s]. d, E [s]. ch);} If (find ()! = Find (B) // not in the same set, that is, the specific coordinates of a node do not know printf ("-1 \ n "); else printf ("% d \ n", (ABS (F [A]. x, F [B]. x) + ABS (F [A]. y, F [B]. y); // calculate the Manhattan DISTANCE} return 0 ;}

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.