Walk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1172 Accepted Submission(s): 209
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 -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
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.
Source
2012 Asia JinHua Regional Contest
数组开小了,伤不起啊!卡了好长时间,才发现,唉,这里,用3*3的格子来代表一个点,这样,一个bfs,就好了,但要注意,有很多细节要注意啊,确实,写起来好复杂,但原理还算简单吧!
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define MAXN 550
struct node
{
int x,y,step,pre,flag;
}temp;
int dir[4][2]={{-3,0},{0,3},{3,0},{0,-3}};
int dir2[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
int startx,starty,endx,endy,n,len[2];
int pos[4][MAXN],temppos[2][MAXN],map[MAXN][MAXN],visit[MAXN][MAXN][4];
node queue[10000005];
bool cmp(int a,int b)
{
return a<b;
}
int fmin(int a,int b){if(a<b)return a;return b;}
int fmax(int a,int b){if(a>b)return a;return b;}
int findpos(int state,int x)
{
int s,e,mid;
s=0,e=len[state]-1;//?
while(s<e)
{
mid=(s+e)>>1;
if(s==e-1)
{
if(temppos[state][e]==x)
return e;
else
return s;
}
if(temppos[state][mid]>x)
{
e=mid;
}
else if(temppos[state][mid]<x)
{
s=mid;
}
else if(temppos[state][mid]==x)
{
return mid;
}
}
return 0;
}
bool can(int i)
{
int x,y;
x=temp.x;
y=temp.y;
if(x<0||y<0||x>3*len[0]||y>3*len[1])
return false;
if(temp.flag==2)//是解落
{
if((temp.pre==2&&i==3)||(temp.pre==1&&i==0)||(temp.pre==0&&i==1)||(temp.pre==3&&i==2))
{
return true;
}
return false;
}
else if(temp.flag==3)
{
if((temp.pre==0&&i==3)||(temp.pre==1&&i==2)||(temp.pre==2&&i==1)||(temp.pre==3&&i==0))
{
return true;
}
return false;
}
return true;
}
int canmove(int x,int y,int i)
{
//printf("%d %d \n",x,y);
int xx,yy;
xx=x+dir2[i][0];
yy=y+dir2[i][1];
if(map[xx][yy])
return 0;
if(map[x+1][y]&&map[x-1][y])
return 0;
if(map[x][y+1]&&map[x][y-1])
return 0;
if(map[x+1][y+1]&&map[x-1][y-1])
return 3;
if(map[x+1][y-1]&&map[x-1][y+1])//??
return 2;
return 1;
}
int bfs()
{
int s,e,i;
s=e=0;
node qfront;
if(startx==endx&&starty==endy)
return 0;
queue[s].x=startx,queue[s].y=starty,queue[s].step=-1,queue[s].flag=0,queue[s].pre=-1;
while(s<=e)
{
qfront=queue[s];
if(qfront.x==endx&&qfront.y==endy)
{
return qfront.step;
}
//printf("%d %d \n",qfront.x,qfront.y);
for(i=0;i<4;i++)
{
temp=qfront;
int x=temp.x,y=temp.y;
temp.x=temp.x+dir[i][0];
temp.y=temp.y+dir[i][1];
int k;
while(can(i)&&(!visit[temp.x][temp.y][i]&&(k=canmove(temp.x,temp.y,i))!=0))//不越界
{
//if
{
visit[temp.x][temp.y][i]=1;
temp.step=qfront.step+1;
temp.pre=i;
bool flag;
flag=true;
if(k==2)
{
temp.flag=2;//记录是角落
flag=false;
}
else if(k==3)
{
temp.flag=3;
flag=false;
}
else
temp.flag=0;
queue[e++]=temp;
if(!flag)
break;
}
temp.x=temp.x+dir[i][0];
temp.y=temp.y+dir[i][1];
}//while
}//for
s++;
}
return -1;
}
int main()
{
int i,j,t,k;
while(scanf("%d%d%d%d",&startx,&starty,&endx,&endy)!=EOF)
{
if(startx==0&&starty==0&&endx==0&&endy==0)
break;
temppos[0][0]=startx;
temppos[0][1]=endx;
temppos[1][0]=starty;
temppos[1][1]=endy;
scanf("%d",&n);
for(i=0,t=2;i<n;i++,t=t+2)
{
for(j=0;j<4;j++)
{
scanf("%d",&pos[j][i]);
}
temppos[0][t]=pos[0][i];
temppos[0][t+1]=pos[2][i];
temppos[1][t]=pos[1][i];
temppos[1][t+1]=pos[3][i];
}
sort(temppos[0],temppos[0]+n+n+2,cmp);
sort(temppos[1],temppos[1]+n+n+2,cmp);
for(int state=0;state<=1;state++)
{
for(i=1,j=0;i<n+n+2;i++)
{
if(temppos[state][i]!=temppos[state][j])
temppos[state][++j]=temppos[state][i];
}
len[state]=j+1;
//printf("%d ",len[state]);
}
startx=3*findpos(0,startx)+1;//??
endx=3*findpos(0,endx)+1;
starty=3*findpos(1,starty)+1;
endy=3*findpos(1,endy)+1;
memset(map,0,sizeof(map));
memset(visit,0,sizeof(visit));
// map[startx][starty]=2;
// map[endx][endy]=3;
for(i=0;i<n;i++)
// for(j=0;j<4;j++)
{
int x1,x2,y1,y2;
x1=3*findpos(0,fmin(pos[0][i],pos[2][i]))+1;
x2=3*findpos(0,fmax(pos[0][i],pos[2][i]))+1;
y1=3*findpos(1,fmin(pos[1][i],pos[3][i]))+1;
y2=3*findpos(1,fmax(pos[1][i],pos[3][i]))+1;
//printf("%d %d %d %d\n",x1,x2,y1,y2);
for(k=x1+1;k<=x2-1;k++)
{
map[k][y2-1]=1;//右方
}
for(k=x1+1;k<=x2-1;k++)
{
map[k][y1+1]=1;//左方
}
for(k=y1+1;k<=y2-1;k++)
{
map[x1+1][k]=1;//上方
}
for(k=y1+1;k<=y2-1;k++)
{
map[x2-1][k]=1;//下方
}
}
printf("%d\n",bfs());
}
return 0;
}