Walk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 832 Accepted Submission(s): 138
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.
What’s the minimum number of turns Biaoge need to make?
![](https://i-blog.csdnimg.cn/blog_migrate/a2f102da2a95873f3b50fe2fe27cf74c.jpeg)
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 -10 8 to 10 8.
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 -10 8 to 10 8.
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 2HintIn 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.
Source
#include<cstdio>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
const int N=503;
map<int,int> hsx,hsy;
bool inQ[N][N][4];
int maze[N][N],dis[N][N][4];
int n,m,k,x[N],y[N],stx,sty,edx,edy,tx,ty,r,c;
int podir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//正方向:上、下、左、右
int opdir[4]={1,0,3,2};//正方向的反方向
int obdir[4][2]={{-1,-1},{-1,1},{1,-1},{1,1}};//斜方向:左上、右上、左下、右下
int corner[4][2]={{2,3},{0,1},{1,3},{0,2}};//判断以上下左右来,可以往哪拐
int turn[4][2]={{2,3},{2,3},{0,1},{0,1}};//以上下左右来,拐向{{左,右},{左,右},{上,下},{上,下}}
struct point
{
int x,y,d;
point(){}
point(int _x,int _y,int _d){x=_x;y=_y;d=_d;}
}st,ed;
struct mar
{
int x1,y1,x2,y2;
void read(){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);}
}p[55];
void make_graph()
{
int i,j,k;
memset(maze,0,sizeof(maze));
for(i=1;i<=n;i++)
{
int x1=hsx[p[i].x1],y1=hsy[p[i].y1];
int x2=hsx[p[i].x2],y2=hsy[p[i].y2];
for(j=x1+1;j<x2;j++)for(k=y1+1;k<y2;k++)maze[j][k]=4;
for(j=x1;j<=x2;j++){maze[j][y1]++;maze[j][y2]++;}
for(k=y1+1;k<y2;k++){maze[x1][k]++;maze[x2][k]++;}
}
for(i=1;i<r;i++)for(j=1;j<c;j++)
{
if(maze[i][j]==2||maze[i][j]==3)
{
int cnt=0;
for(k=0;k<4;k++)
{
if(maze[i+podir[k][0]][j+podir[k][1]]==0){maze[i][j]=1;break;}
else cnt++;
}
if(maze[i][j]==1)continue;
for(k=0;k<4;k++)if(maze[i+obdir[k][0]][j+obdir[k][1]])cnt++;
if(cnt==8)maze[i][j]=4;
}
}
stx=hsx[st.x];sty=hsy[st.y];
edx=hsx[ed.x];edy=hsy[ed.y];
}
void spfa()
{
int i,j,tmx,tmy,tmp;
queue<point> Q;
memset(inQ,0,sizeof(inQ));
memset(dis,-1,sizeof(dis));
for(i=0;i<4;i++){Q.push(point(stx,sty,i));dis[stx][sty][i]=0;}
while(!Q.empty())
{
point t=Q.front();Q.pop();inQ[t.x][t.y][t.d]=0;
if(maze[t.x][t.y]>1)
{
for(j=0;j<2;j++)
{
tmx=t.x+obdir[corner[t.d][j]][0];
tmy=t.y+obdir[corner[t.d][j]][1];
if(!maze[tmx][tmy])
{
tmx=t.x+podir[turn[t.d][j]][0];
tmy=t.y+podir[turn[t.d][j]][1];
tmp=dis[t.x][t.y][t.d]+1;
i=turn[t.d][j];
if(dis[tmx][tmy][i]==-1||tmp<dis[tmx][tmy][i])
{
dis[tmx][tmy][i]=tmp;
if(!inQ[tmx][tmy][i]){Q.push(point(tmx,tmy,i));inQ[tmx][tmy][i]=1;}
}
}
}
continue;
}
for(i=0;i<4;i++)
{
if(t.d==opdir[i])continue;
tmx=t.x+podir[i][0];
if(tmx<0||tmx>r)continue;
tmy=t.y+podir[i][1];
if(tmy<0||tmy>c)continue;
if(maze[tmx][tmy]==4)continue;
tmp=dis[t.x][t.y][t.d]+((i==t.d)?0:1);
if(dis[tmx][tmy][i]==-1||tmp<dis[tmx][tmy][i])
{
dis[tmx][tmy][i]=tmp;
if(!inQ[tmx][tmy][i]){Q.push(point(tmx,tmy,i));inQ[tmx][tmy][i]=1;}
}
}
}
int ans=-1;
for(i=0;i<4;i++)
{
if(dis[edx][edy][i]!=-1)
{
if(ans==-1||ans>dis[edx][edy][i])ans=dis[edx][edy][i];
}
}
printf("%d\n",ans);
}
int main()
{
int i;
while(scanf("%d%d%d%d",&st.x,&st.y,&ed.x,&ed.y),st.x||st.y||ed.x||ed.y)
{
scanf("%d",&n);
tx=ty=0;
x[++tx]=st.x;y[++ty]=st.y;
x[++tx]=ed.x;y[++ty]=ed.y;
for(i=1;i<=n;i++)
{
p[i].read();
x[++tx]=min(p[i].x1,p[i].x2);y[++ty]=min(p[i].y1,p[i].y2);
x[++tx]=max(p[i].x1,p[i].x2);y[++ty]=max(p[i].y1,p[i].y2);
}
sort(x+1,x+tx+1);sort(y+1,y+ty+1);
tx=unique(x+1,x+tx+1)-(x+1);
ty=unique(y+1,y+ty+1)-(y+1);
for(i=1,r=1;i<=tx;i++,r+=2)hsx[x[i]]=r;
for(i=1,c=1;i<=ty;i++,c+=2)hsy[y[i]]=c;
make_graph();
spfa();
}
return 0;
}