Poj 1984 Navigation Nightmare and query set

Source: Internet
Author: User

And check the deformation of the Set application. The question refers to a graph with only the top, bottom, and left sides. Some of these edges are given,
Evaluate the distance between any two specified nodes.
It is possible that the current information does not involve the required two nodes, or only involves one node.
The distance between them cannot be determined. Or, based on the given edge, only know that the two nodes are in different Unicom components.
The distance cannot be determined. According to the question requirements, output-1.
The problem is how to determine their distance in a connected component.
The key to this question is that there are only the top, bottom, and left sides. If each node has a coordinate, then they
The coordinates of these nodes must be fixed, so you do not need to consider the situations such as loops in the graph.
In this way, you can easily apply and query sets.
Use and query sets to add other information to each node, that is, the coordinates (x, y) of the nodes that represent the and query set ).
Find the coordinates in FindSet, and modify the coordinates of the root node of the newly added set in UnionSet.
The Code is as follows:

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <algorithm>
Using namespace std;

Const int MAX_N = 40010;
Int nN, nM;
Int nSets [MAX_N];
Int nX [MAX_N];
Int nY [MAX_N];
Char szInput [MAX_N] [100];

Void MakeSets (int nNum)
{
For (int I = 0; I <nNum; ++ I)
{
NSets [I] = I;
NX [I] = nY [I] = 0;
}
}

Int FindSets (int nI)
{
If (nSets [nI]! = NI)
{
Int nPre = nSets [nI];
NSets [nI] = FindSets (nSets [nI]);
NX [nI] + = nX [nPre];
NY [nI] + = nY [nPre];
}
Return nSets [nI];
}

Void UnionSets (int nBeg, int nEnd, int dx, int dy)
{
Int nA = FindSets (nBeg );
Int nB = FindSets (nEnd );
If (nA! = NB)
{
NSets [nB] = nA; // combines Set B into set
NX [nB] = nX [nBeg] + dx-nX [nEnd]; // if the direction is reversed, subtract
NY [nB] = nY [nBeg] + dy-nY [nEnd];
}
}

Int main ()
{
Int nBeg, nEnd, nL;
Char szDir [10];

While (scanf ("% d % * c", & nN, & nM) = 2)
{
MakeSets (nN );
For (int I = 0; I <nM; ++ I)
{
Fgets (szInput [I], 100, stdin );
}
Int nK;
Int nF1, nF2, nI;
Scanf ("% d", & nK );
Int nCur = 0;
While (nK --)
{
Scanf ("% d", & nF1, & nF2, & nI );
For (int I = nCur; I <nI; ++ I)
{
Sscanf (szInput [I], "% d % s", & nBeg,
& NEnd, & nL, szDir );
Int dx = 0, dy = 0;
Switch (szDir [0])
{
Case 'N': dy + = nL; break;
Case's ': dy-= nL; break;
Case 'E': dx + = nL; break;
Case 'W': dx-= nL; break;
}
UnionSets (nBeg, nEnd, dx, dy );
}
NCur = nI;

If (FindSets (nF1 )! = FindSets (nF2 ))
{
Printf ("-1 \ n ");
}
Else
{
Printf ("% d \ n", abs (nX [nF1]-nX [nF2])
+ Abs (nY [nF1]-nY [nF2]);
}
}
}

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.