poj2049(bfs) Finding Nemo

Finding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 6864 Accepted: 1569

Description

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

Input

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

Output

For 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

我被这道题深深地坑了,WA了一天,然后每天还拿出来看看,希望发现什么错误,结果今天李成明学长给我看了一遍也没发现什么问题,无奈之下就提交了,用了GCC结果AC了。。。汗,耽误学长那么长时间,要重重的感谢学长啊!!!
说说我的思路吧,把它看成网格,每个网格四条边,四个方向变量,0123分别表示上左下右四个方向。主要就是把题目数据转化成我们熟知的那种迷宫就好了,然后就是bfs就行了
Memory: 1896 KB Time: 47 MS
Language: GCC Result:  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;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值