HDU 4444 离散化+bfs 注意细节

Walk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1972    Accepted Submission(s): 308

Problem Description

Biaoge is planning to walk to amusement park. The city he lives can be abstracted 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 to the coordinate axis. Of course Biaoge can’t walk across 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.

Input

There 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.

Output

For 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 10

1

0 5 5 8

0 0 0 10

2

0 5 5 8

-2 1 0 5

0 0 0 0

Sample Output

0

2

Hint

In 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.

题目意思:

有n个矩形,然后两个不在矩形上面的点,求一个点到另一个点路径上最少的拐弯数目(点不能穿过矩形,但是可以在边界上走)。

思路:

离散化所有点,建图,bfs就行了。

  可以走

         不可以走

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 #include <bitset>
 10 #include <map>
 11 using namespace std;
 12
 13 #define N 400
 14 #define inf 999999999
 15
 16 int max(int x,int y){return x>y?x:y;}
 17 int min(int x,int y){return x<y?x:y;}
 18 int abs(int x,int y){return x<0?-x:x;}
 19
 20 int xx[]={-1,0,1,0};
 21 int yy[]={0,1,0,-1};
 22 int lx, ly;
 23 int n;
 24
 25 struct node{
 26     int x1, y1, x2, y2;
 27 }a[55];
 28 int llx[N], lly[N];
 29 map<int,int>mapx, mapy;
 30 int stx, sty, endx, endy;
 31 int sx, sy, ex, ey;
 32 int g[N][N];
 33 int dp[N][N][4];
 34 bool visited[N][N][4];
 35
 36 void creat(){             //建图
 37     memset(g,0,sizeof(g));
 38     int i, j, k;
 39     for(i=0;i<n;i++){
 40         for(j=mapx[a[i].x1];j<=mapx[a[i].x2];j++){
 41             g[j][mapy[a[i].y1]]=2;//printf("11111\n");
 42             g[j][mapy[a[i].y2]]=2;
 43         }
 44         for(j=mapy[a[i].y1]+1;j<mapy[a[i].y2];j++){
 45             g[mapx[a[i].x1]][j]=2;
 46             g[mapx[a[i].x2]][j]=2;
 47         }
 48         for(j=mapx[a[i].x1]+1;j<mapx[a[i].x2];j++){
 49             for(k=mapy[a[i].y1]+1;k<mapy[a[i].y2];k++){
 50                 g[j][k]=2;
 51             }
 52         }
 53     }
 54     for(i=0;i<=lx;i++){
 55         for(j=0;j<=ly;j++){
 56             if(j<ly&&!g[i][j]&&g[i][j+1]) g[i][j+1]=1;
 57             if(j>0&&!g[i][j]&&g[i][j-1]) g[i][j-1]=1;
 58             if(i<lx&&!g[i][j]&&g[i+1][j]) g[i+1][j]=1;
 59             if(i>0&&!g[i][j]&&g[i-1][j]) g[i-1][j]=1;
 60         }
 61     }
 62     for(i=1;i<lx;i++){
 63         for(j=1;j<ly;j++){
 64             if(g[i][j]==2){
 65                 if(!g[i+1][j+1]&&g[i-1][j]==1&&g[i][j-1]==1) g[i][j]=1;
 66                 if(!g[i-1][j-1]&&g[i-1][j]==1&&g[i][j-1]==1) g[i][j]=1;
 67                 if(!g[i+1][j-1]&&g[i-1][j]==1&&g[i][j+1]==1) g[i][j]=1;
 68                 if(!g[i-1][j+1]&&g[i-1][j]==1&&g[i][j+1]==1) g[i][j]=1;
 69                 if(!g[i-1][j+1]&&g[i+1][j]==1&&g[i][j-1]==1) g[i][j]=1;
 70                 if(!g[i+1][j-1]&&g[i+1][j]==1&&g[i][j-1]==1) g[i][j]=1;
 71                 if(!g[i-1][j-1]&&g[i+1][j]==1&&g[i][j+1]==1) g[i][j]=1;
 72                 if(!g[i+1][j+1]&&g[i+1][j]==1&&g[i][j+1]==1) g[i][j]=1;
 73             }
 74             if(g[i][j]==1&&g[i-1][j-1]==2&&g[i+1][j+1]==2&&!g[i+1][j-1]&&!g[i-1][j+1]) g[i][j]=2;
 75             if(g[i][j]==1&&!g[i-1][j-1]&&!g[i+1][j+1]&&g[i+1][j-1]==2&&g[i-1][j+1]==2) g[i][j]=2;
 76         }
 77     }
 78 }
 79
 80 struct G{
 81     int x, y, z, f;
 82 };
 83
 84 void bfs(){
 85     int i, j, k;
 86     queue<G>Q;
 87     G p, q;
 88     for(i=0;i<=lx;i++){
 89         for(j=0;j<=ly;j++){
 90             for(k=0;k<4;k++){
 91                 dp[i][j][k]=inf;
 92             }
 93         }
 94     }
 95     memset(visited,false,sizeof(visited));
 96     p.x=sx;p.y=sy;p.f=0;
 97     for(i=0;i<4;i++){
 98         p.z=i;
 99         dp[p.x][p.y][p.z]=0;
100         visited[p.x][p.y][p.z]=true;
101         Q.push(p);
102     }
103     while(!Q.empty()){
104         p=Q.front();Q.pop();
105         visited[p.x][p.y][p.z]=false;
106         for(i=0;i<4;i++){
107             q.x=p.x+xx[i];
108             q.y=p.y+yy[i];
109             q.z=i;
110             if(p.f>0&&q.z!=p.f-1) continue;              //之前的点是拐角的话,必须按照走特定的方向
111             if(q.x<0||q.x>lx||q.y<0||q.y>ly) continue;
112             if(p.f>0) q.f=0;
113             if(g[q.x][q.y]==2){                          //拐角判断
114                 if(!g[q.x-1][q.y-1]&&!g[q.x+1][q.y+1]){
115                     if(q.z==0) q.f=1+1;
116                     else if(q.z==3) q.f=2+1;
117                     else if(q.z==1) q.f=0+1;
118                     else if(q.z==2) q.f=3+1;
119                 }
120                 else if(!g[q.x+1][q.y-1]&&!g[q.x-1][q.y+1]){
121                     if(q.z==0) q.f=3+1;
122                     else if(q.z==3) q.f=0+1;
123                     else if(q.z==1) q.f=2+1;
124                     else if(q.z==2) q.f=1+1;
125                 }
126                 else continue;
127             }
128             else q.f=0;
129             if(q.z==p.z){
130                 if(dp[q.x][q.y][q.z]>dp[p.x][p.y][p.z]){
131                     dp[q.x][q.y][q.z]=dp[p.x][p.y][p.z];
132                     if(!visited[q.x][q.y][q.z]){
133                         Q.push(q);
134                         visited[q.x][q.y][q.z]=true;
135                     }
136                 }
137             }
138             else{
139                 if(dp[q.x][q.y][q.z]>dp[p.x][p.y][p.z]+1){
140                     dp[q.x][q.y][q.z]=dp[p.x][p.y][p.z]+1;
141                     if(!visited[q.x][q.y][q.z]){
142                         Q.push(q);
143                         visited[q.x][q.y][q.z]=true;
144                     }
145                 }
146             }
147         }
148     }
149 }
150
151 main()
152 {
153     int i, j, k;
154     int len1, len2;
155     while(scanf("%d %d %d %d",&stx,&sty,&endx,&endy)==4){
156         if(!stx&&!sty&&!endx&&!endy) break;
157         len1=len2=0;
158         llx[len1++]=stx;
159         llx[len1++]=endx;
160         lly[len2++]=sty;
161         lly[len2++]=endy;
162         scanf("%d",&n);
163         for(i=0;i<n;i++){
164             scanf("%d %d %d %d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
165             llx[len1++]=a[i].x1;
166             llx[len1++]=a[i].x2;
167             lly[len2++]=a[i].y1;
168             lly[len2++]=a[i].y2;
169         }
170         sort(llx,llx+len1);
171         sort(lly,lly+len2);
172         lx=ly=1;
173         mapx.clear();
174         mapy.clear();
175         for(i=0;i<len1;i++){
176             if(mapx.find(llx[i])==mapx.end()){
177                 mapx[llx[i]]=lx;lx+=2;
178             }
179         }
180         for(i=0;i<len2;i++){
181             if(mapy.find(lly[i])==mapy.end()){
182                 mapy[lly[i]]=ly;ly+=2;
183             }
184         }
185         creat();
186     /*    for(i=0;i<len1;i++) printf("%d ",mapx[llx[i]]);
187         cout<<endl;
188         for(i=0;i<=lx;i++){
189             for(j=0;j<=ly;j++){
190                 printf("%d ",g[i][j]);
191             }
192             cout<<endl;
193         }*/
194         sx=mapx[stx];ex=mapx[endx];
195         sy=mapy[sty];ey=mapy[endy];
196         bfs();
197         int ans=inf;
198         for(i=0;i<4;i++){
199             ans=min(ans,dp[ex][ey][i]);
200         }
201         if(ans!=inf) printf("%d\n",ans);
202         else printf("-1\n");
203     }
204 }

时间: 2024-10-10 18:28:34

HDU 4444 离散化+bfs 注意细节的相关文章

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

离散化+BFS HDOJ 4444 Walk

题目传送门 1 /* 2 题意:问一个点到另一个点的最少转向次数. 3 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 4 关键理解坐标离散化,BFS部分可参考HDOJ_1728 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <queue> 10 #include <vector> 11 #include

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

hdu 1104 数论+bfs

题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k  输出最小路径与运算副 zsd:% 只是求余数 有正负 mod 是求模 无正负. yhd:对m*k求余对 对k求余不对 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5

HDU 1255 离散化+扫描线覆盖的面积

覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3571    Accepted Submission(s): 1753 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据

HDU 1495——非常可乐( BFS )

非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4663    Accepted Submission(s): 1868 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要

[ An Ac a Day ^_^ ] hdu 5925 Coconuts 离散化+BFS求连通块

东北赛根本就没看懂的的题目…… 也用到了离散化 1e9的x y范围 200个坏点 很典型的离散化数据范围 还是不太为什么离散化的遍历下标都要从1开始…… 所以说只做这道题对离散化的理解还不是很深刻…… 因为可能换一道题又不会了 还是要多做啊 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<