HDU 4444 walk (discretization + graph creation + BFS + 3D judgment)

Source: Internet
Author: User
Walk

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)

Total submission (s): 1142 accepted submission (s): 195 problem descriptionbiaoge is planning to walk to amusement park. the city he lives can be named acted as a 2D plane. biaoge is at (x1, Y1) and the amusement park is at (X2, Y2 ). there are also some rectangle buildings. biaoge can only walk parallel
The coordinate axis. Of course biaoge can't walk into ss the buildings.
What's the minimum number of turns biaoge need to make?


As the figure above shows, there are 4 buildings and biaoge need to make at least 3 turns to reach the amusement park (before walking he can chose a direction freely ). it is guaranteed that all the buildings are parallel to the coordination axis. buildings may
Contact but overlapping is impossible. The amusement park and biaoge's initial positions will not contact or inside any building.

Inputthere are multiple test case.
Each test case contains several lines.
The first line contains 4 integers X1, Y1, X2, Y2 indicating the coordinate of biaoge and amusement park.
The second line contains one integer N (0 ≤ n ≤ 50), indicating the number of buildings.
Then n lines follows, each contains 4 integer X1, Y1, X2, Y2, indicating the coordinates of two opposite vertices of the building.
Input ends with 0 0 0 0, you should not process it.
All numbers in the input range from-108 to 108.

Outputfor each test case, output the number of least turns in a single line. If biaoge can't reach the amusement park, output-1 instead.

Sample Input

0 0 0 1010 5 5 80 0 0 1020 5 5 8-2 1 0 50 0 0 0
 

Sample output

02HintIn the first case, Biaoge can walk along the side of building, and no turn needed.In the second case, two buildings block the direct way and Biaoge need to make 2 turns at least. 
 

Source2012 Asia Jinhua Regional Contest: Wow Haha, this question has finally come up in a day! In the morning, I started to study a person's code (very concise and well-styled). After studying the code for one morning, I understood all my thoughts. Then I started to write it myself in the afternoon. I wrote it and tested the data posted by others, I found that the last group of data could not be passed. I studied it for a while and found that this idea was wrong. Me
X_x, at that time there was an impulse to hit the computer, waste me so long. PS: During the competition, the data was in the water, so some code pages were out of water. Therefore, some of the problem-solving reports provided were incorrect. Then I started to do it again in the afternoon. After reading a person's ideas, I thought it was very correct. Then I started to write this idea at night. I wrote various debugging, various code changes, and finally AC, that's cool!
Question: give n rectangles in a two-dimensional plane and two more points to find the minimum number of turns in all paths between two points.

Idea: discretization the rectangle. Because the boundary of the rectangle can go, it is still a little troublesome. It is better to regard a 1*1 square as a 3*3 square. The most important question is how to represent a rectangle. I did this: black the covered area of the rectangle (marked as 1). Note that not all the rectangles should be black. Otherwise, the boundary cannot be moved, the left boundary of the rectangle is + 1, and the right boundary is-1, and the upper and lower boundary is the same. In this way, the boundary of the rectangle can go. However, when there are two adjacent rectangles, their boundaries cannot go. We can determine whether a point can go up: If the left and right sides of the point above the 3*3 square are black, then it cannot go up. (Bottom, left, right) The most tangled is the inflection point ("L" type area)
This is a special sentence. In eight cases, you may want to clarify the situation and write it again. After the image is created, it will be the same as "HDU 1728. Note: 1. the boundary condition is very important. The boundary can be crossed, not vertical. The same is true for vertical boundary. 2. Special processing is required for the 'l' region. In eight cases, make a good analysis. 3. A single point may go through multiple times, so it cannot be a simple two-dimensional Weight Determination. A direction should be added. (For example, I posted the last group of data) PS: Train of Thought from: http://blog.csdn.net/asdfgh0308/article/details/8125832 data from: Taobao.
Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 350 using namespace STD; int n, m, ans; int Sx, Sy, ex, ey; int X [maxn], Y [maxn]; int XX [maxn], YY [maxn]; int MP [maxn] [maxn]; int up [maxn] [maxn], down [maxn] [maxn]; int le [maxn] [maxn], RI [maxn] [maxn]; int DX [] = {-,}; int dy [] = {,-}; bool vis [maxn] [maxn] [4]; // 3D Determination of weight X + Y + direction struct node {int L, D, R, U;} rect [maxn]; struct node {Int MX, my; int D, CNT; // Number of redirection times} cur, now, Q [maxn * maxn]; void showxxyy () // output the x y {int I, j; printf ("XX: \ n") after discretization; for (I = 1; I <= m; I ++) {printf ("% d", XX [I]);} printf ("\ n"); printf ("YY: \ n"); for (I = 1; I <= m; I ++) {printf ("% d", YY [I]);} printf ("\ n");} void showmap () // output map {int I, j; printf ("showmap: \ n"); printf ("1 2 3 4 5 6 7 8 9 10 11 12 \ n "); for (I = 1; I <= 3 * XX [m]; I ++) {printf ("% 3d:", I); For (j = 1; J <= 3 * YY [m]; j ++) {printf ("% d", MP [I] [J]);} printf ("\ n") ;}} void disc () // discretization {int I, j; M = 2 * n + 2; sort (x + 1, X + m + 1); XX [1] = 1; for (I = 2; I <= m; I ++) {If (X [I] = x [I-1]) xx [I] = XX [I-1]; else XX [I] = XX [I-1] + 1 ;} sort (Y + 1, Y + m + 1); YY [1] = 1; for (I = 2; I <= m; I ++) {If (Y [I] = Y [I-1]) YY [I] = YY [I-1]; else YY [I] = YY [I-1] + 1 ;} // showxxyy ();} int find (int v, int K) // find the point {int I, j; If (k) {for (I = 1; I <= m; I ++) {If (Y [I] = V) return YY [I] ;}} else {for (I = 1; I <= m; I ++) {If (X [I] = V) return XX [I] ;}} void buildgraph () // convert the square of 1*1 to a square of 3*3 and then create a graph {int I, j, Ni, NJ, K, L, D, R, U; memset (MP, 0, sizeof (MP); memset (up, 1, sizeof (up); memset (down, 1, sizeof (down); memset (Le, 1, sizeof (LE); memset (Ri, 1, sizeof (RI); For (k = 1; k <= N; k ++) // black the rectangle on the map {L = 3 * Find (rect [K]. l, 0) + 1; D = 3 * Find (rect [K]. d, 1) + 1; r = 3 * Find (rect [K]. r, 0)-1; u = 3 * Find (R ECT [K]. u, 1)-1; // printf ("L: % d r: % d u: % d \ n", l, D, R, u); for (I = L; I <= r; I ++) {for (j = D; j <= u; j ++) {MP [I] [J] = 1 ;}}for (I = 1; I <= XX [m]; I ++) // whether the record can go is a 'l' region {for (j = 1; j <= YY [m]; j ++) {Ni = 3 * I; nj = 3 * j; If (MP [ni-1] [nj-1] & MP [Ni + 1] [NJ + 1]) {up [Ni] [NJ] = down [Ni] [NJ] = Le [Ni] [NJ] = Ri [Ni] [NJ] =-1 ;} else if (MP [Ni + 1] [nj-1] & MP [ni-1] [NJ + 1]) up [Ni] [NJ] = down [Ni] [NJ] = Le [Ni] [NJ] = Ri [Ni] [NJ] =-2; if (MP [ni-1] [n J-1] & MP [ni-1] [NJ + 1]) up [Ni] [NJ] = 0; if (MP [Ni + 1] [nj-1] & MP [Ni + 1] [NJ + 1]) down [Ni] [NJ] = 0; if (MP [ni-1] [nj-1] & MP [Ni + 1] [nj-1]) Le [Ni] [NJ] = 0; if (MP [ni-1] [NJ + 1] & MP [Ni + 1] [NJ + 1]) RI [Ni] [NJ] = 0 ;}} // showmap ();} bool BFS () // After the graph is created, the BFS is simple. {int I, j, NX, NY, Nd, ncnt, TX, Ty; int head = 0, tail =-1; memset (VIS, 0, sizeof (VIS); SX = 3 * Find (sx, 0 ); sy = 3 * Find (SY, 1); Ex = 3 * Find (ex, 0); ey = 3 * Find (ey, 1); // printf ("SX: % d Sy: % d ex: % D ey: % d \ n ", SX/3, SY/3, EX/3, ey/3); cur. MX = SX; cur. my = sy; cur. D =-1; cur. CNT = 0; vis [SX] [sy] [0] = vis [SX] [sy] [1] = vis [SX] [sy] [2] = vis [SX] [sy] [3] = 1; Q [++ tail] = cur; while (Head <= tail) {now = Q [head]; head ++; Nx = now. MX; ny = now. my; ND = now. d; ncnt = now. CNT; // printf ("NX: % d NY: % d Nd: % d ncnt: % d \ n", NX/3, NY/3, Nd, ncnt ); if (Nx = ex & ny = ey) {ans = ncnt; return true ;}for (I = 0; I <4; I ++) {if (I = 0 &&! Up [NX] [NY] | I = 1 &&! Down [NX] [NY] | I = 2 &&! Le [NX] [NY] | I = 3 &&! RI [NX] [NY]) continue; // determines whether the path can be in this direction. If (up [NX] [NY] =-1) // determine {if (I = 0 & (nD = 0 | nD = 3) continue for the 'l' region; else if (I = 1 & (nD = 1 | nD = 2) continue; else if (I = 2 & (nD = 2 | nD = 1) continue; else if (I = 3 & (nD = 3 | nD = 0) continue;} else if (up [NX] [NY] =-2) {if (I = 0 & (nD = 0 | nD = 2) continue; else if (I = 1 & (nD = 1 | nD = 3) continue; else if (I = 2 & (nD = 2 | nD = 0) continue; else if (I = 3 & (nD = 3 | nd = 1) continue;} Tx = NX + dx [I]; ty = ny + dy [I]; while (TX> = 1 & TX <= 3 * XX [m] & ty> = 1 & ty <= 3 * YY [m] &! MP [TX] [ty]) // searches for {If (! Vis [TX] [ty] [I]) {vis [TX] [ty] [I] = 1; cur. MX = TX; cur. my = ty; cur. D = I; cur. CNT = ncnt; If (cur. d! = Nd & nd! =-1) cur. CNT ++; Q [++ tail] = cur ;}if (I = 0 &&! Up [TX] [ty] | I = 1 &&! Down [TX] [ty] | I = 2 &&! Le [TX] [ty] | I = 3 &&! RI [TX] [ty]) break; If (up [TX] [ty] =-1 | up [TX] [ty] =-2) break; tx + = DX [I]; ty + = Dy [I] ;}} return false;} int main () {int I, j; int X1, Y1, X2, y2, L, D, R, U; while (scanf ("% d", & Sx, & Sy, & Ex, & ey ), SX | Sy | ex | ey) {scanf ("% d", & N); for (I = 1; I <= N; I ++) {scanf ("% d", & X1, & Y1, & X2, & Y2); L = min (x1, x2 ); R = max (x1, x2); D = min (Y1, Y2); U = max (Y1, Y2); X [I] = rect [I]. L = L; X [I + N] = rect [I]. R = r; y [I] = rect [I]. D = D; y [I + N] = Rect [I]. U = u;} X [2 * n + 1] = SX; X [2 * n + 2] = ex; y [2 * n + 1] = sy; Y [2 * n + 2] = ey; disc (); buildgraph (); If (BFS () printf ("% d \ n", ANS ); else printf ("-1 \ n");} return 0 ;} /* // detour 21 1 1 410 2 2 3 // go straight 01 1 413 3 4 5 // go straight along the edge 00 1 0 720 2 2 3-1 4 0 5/ /Go straight along the edge 00 1 0 720 2 30 3 2 4 // cannot pass through the gap, detour 20 1 0 720 2 2 3-1 3 0 5 // cannot reach-10 0 5 5 54-1 1 1 21-1 2 1-1-1 1 1-2-1 1-2-1 // The same point goes through twice. 51 3 4 110-1-1 3 23-1 7 07-1 11 39 3 11 7-1 7 11 90 5 4 7-1 2 0 72 2 4 34 3 5 45 4 8 6 */

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.