您的位置:首页 > 其它

poj2049(bfs) Finding Nemo

2013-07-13 10:38 92 查看
Finding Nemo
Time Limit: 2000MSMemory Limit: 30000K
Total Submissions: 6864Accepted: 1569
DescriptionNemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help. After checking the map, Marlin found that the sea is like a labyrinth with walls and doors. All the walls are parallel to the X-axis or to the Y-axis. The thickness of the walls are assumed to be zero. All the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent medusas near the doors), Marlin wants to go through as few doors as he could to find Nemo. Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.

We assume Marlin's initial position is at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum number of doors Marlin has to go through in order to reach Nemo.InputThe input consists of several test cases. Each test case is started by two non-negative integers M and N. M represents the number of walls in the labyrinth and N represents the number of doors. Then follow M lines, each containing four integers that describe a wall in the following format: x y d t (x, y) indicates the lower-left point of the wall, d is the direction of the wall -- 0 means it's parallel to the X-axis and 1 means that it's parallel to the Y-axis, and t gives the length of the wall. The coordinates of two ends of any wall will be in the range of [1,199]. Then there are N lines that give the description of the doors: x y d x, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted. The last line of each case contains two positive float numbers: f1 f2 (f1, f2) gives the position of Nemo. And it will not lie within any wall or door. A test case of M = -1 and N = -1 indicates the end of input, and should not be processed.OutputFor each test case, in a separate line, please output the minimum number of doors Marlin has to go through in order to rescue his son. If he can't reach Nemo, output -1.Sample Input
8 9
1 1 1 3
2 1 1 3
3 1 1 3
4 1 1 3
1 1 0 3
1 2 0 3
1 3 0 3
1 4 0 3
2 1 1
2 2 1
2 3 1
3 1 1
3 2 1
3 3 1
1 2 0
3 3 0
4 3 1
1.5 1.5
4 0
1 1 0 1
1 1 1 1
2 1 1 1
1 2 0 1
1.5 1.7
-1 -1

Sample Output
5
-1

Source

Beijing 2004

我被这道题深深地坑了,WA了一天,然后每天还拿出来看看,希望发现什么错误,结果今天李成明学长给我看了一遍也没发现什么问题,无奈之下就提交了,用了GCC结果AC了。。。汗,耽误学长那么长时间,要重重的感谢学长啊!!!

说说我的思路吧,把它看成网格,每个网格四条边,四个方向变量,0123分别表示上左下右四个方向。主要就是把题目数据转化成我们熟知的那种迷宫就好了,然后就是bfs就行了

Memory: 1896 KBTime: 47 MS

Language: GCCResult: Accepted
#include<stdio.h>
#include<string.h>

struct ste{
int x,y,s;
}r[1000000];
int m,n,map[210][210][4],map2[210][210][4],minx,miny,maxx,maxy,dx,dy,min;//0表示最上方,逆时针

void bfs(int x,int y,int d)
{
int i,j,t=0,w=1;
r[0].x=x;
r[0].y=y;
r[0].s=0;
if(map[x][y][d]==1)
r[0].s++;
memset(map2,0,sizeof(map2));
map2[x][y][d]=1;
while(t<w)
{
if(r[t].x<minx||r[t].x>maxx||r[t].y>maxy||r[t].y<miny)
{
t++;
continue;
}
if(r[t].x==dx&&r[t].y==dy)
{
if(min>r[t].s)
min=r[t].s;
t++;
continue;
}
if(r[t].s>=min)
{
t++;
continue;
}
for(i=0;i<4;i++)
{
//printf("i%dmap2[r[t].x][r[t].y][i]%dx%dy%ds%d\n",i,map2[r[t].x][r[t].y][i],r[t].x,r[t].y,r[t].s);
if(map[r[t].x][r[t].y][i]!=2&&map2[r[t].x][r[t].y][i]==0)
{//printf("2x%dy%ds%di%d\n",r[t].x,r[t].y,r[t].s,i);
switch(i)
{
case 0:if(r[t].x>=minx&&r[t].x<=maxx&&r[t].y>=miny&&r[t].y<maxy)
{
r[w].x=r[t].x;
r[w].y=r[t].y+1;
r[w].s=r[t].s;
if(map[r[t].x][r[t].y][i]==1)
r[w].s++;
w++;
map2[r[t].x][r[t].y][i]=1;
map2[r[t].x][r[t].y+1][2]=1;
//printf("0x%dy%ds%d\n",r[t].x,r[t].y+1,r[w-1].s);
}
break;
case 1:if(r[t].x>minx&&r[t].x<=maxx&&r[t].y>=miny&&r[t].y<=maxy)
{
r[w].x=r[t].x-1;
r[w].y=r[t].y;
r[w].s=r[t].s;
if(map[r[t].x][r[t].y][i]==1)
r[w].s++;
w++;
//printf("s-%d-",r[w-1].s);
map2[r[t].x][r[t].y][i]=1;
map2[r[t].x-1][r[t].y][3]=1;//printf("1x%dy%ds%d\n",r[t].x-1,r[t].y,r[w-1].s);
}
break;
case 2:if(r[t].x>=minx&&r[t].x<=maxx&&r[t].y>miny&&r[t].y<=maxy)
{//printf("21x%dy%ds%d\n",r[t].x,r[t].y-1,r[t].s);
r[w].x=r[t].x;
r[w].y=r[t].y-1;
r[w].s=r[t].s;
if(map[r[t].x][r[t].y][i]==1)
r[w].s++;
w++;
map2[r[t].x][r[t].y][i]=1;
map2[r[t].x][r[t].y-1][0]=1;
}
break;
case 3:if(r[t].x>=minx&&r[t].x<maxx&&r[t].y>=miny&&r[t].y<=maxy)
{
r[w].x=r[t].x+1;
r[w].y=r[t].y;
r[w].s=r[t].s;
if(map[r[t].x][r[t].y][i]==1)
r[w].s++;
w++;
map2[r[t].x][r[t].y][i]=1;
map2[r[t].x+1][r[t].y][1]=1;//printf("3x%dy%ds%d\n",r[t].x+1,r[t].y,r[w-1].s);
}
break;
}
}
}
t++;
}
}

int main()
{
int i,j,t,d,x,y;
double a,b;
while(1)
{
scanf("%d%d",&m,&n);
min=1000000;
if(n==-1&&m==-1)
break;
minx=miny=3002;
maxx=maxy=-100;
memset(map,0,sizeof(map));
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&x,&y,&d,&t);
if(minx>x)
minx=x;
if(miny>y)
miny=y;
if(maxy<y)
maxy=y;
if(maxx<x)
maxx=x;
if(d==1)
{
for(j=0;j<t;j++)
{
map[x][y+j][1]=2;
map[x-1][y+j][3]=2;
}
}
else
{
for(j=0;j<t;j++)
{
map[x+j][y][2]=2;//2表示墙
map[x+j][y-1][0]=2;
}
}
}
for(i=0;i<n;i++)
{
scanf("%d%d%d",&x,&y,&d);
if(minx>x)
minx=x;
if(miny>y)
miny=y;
if(maxy<y)
maxy=y;
if(maxx<x)
maxx=x;
if(d==1)
{

map[x][y][1]=1;//1表示门
map[x-1][y][3]=1;
}
else
{
map[x][y][2]=1;
map[x][y-1][0]=1;
}
}
maxx--;
maxy--;
scanf("%lf%lf",&a,&b);
dx=a;
dy=b;
//printf("%d-%d\n",dx,dy);
if(dx<minx||dx>maxx||dy<miny||dy>maxy)
{
printf("0\n");
continue;
}
for(j=minx;j<=maxx;j++)//上方
{
if(map[j][maxy][0]!=2)
bfs(j,maxy,0);
}
for(j=miny;j<=maxy;j++)//左方可进
{
if(map[minx][j][1]!=2)
bfs(minx,j,1);
}
for(j=minx;j<=maxx;j++)//下方
{
if(map[j][miny][2]!=2)
bfs(j,miny,2);
}
for(j=miny;j<=maxy;j++)//右方
{
if(map[maxx][j][3]!=2)
bfs(maxx,j,3);
}
//printf("minx%dminy%dmaxx%dmaxy%d\n",minx,miny,maxx,maxy);
if(min!=1000000)
printf("%d\n",min);
else printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: