2.4.2(extra)

/*
ID:18861501
LANG:C++
TASK:maze1
*/
/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 9901
/*---------------------Work-----------------------*/
int W,H;
char maze[250][100];
int cnt[250][100];
bool vis[250][100];
struct node
{
	int x,y;
};
queue<node>myqueue;
int exit1x,exit1y,exit2x,exit2y;
void getanswer(int x,int y)
{
	node h,k;
	cnt[x][y]=1;
	h.x=x,h.y=y;
	myqueue.push(h);
	while(!myqueue.empty())  //用函数递归实现BFS超时,手写队列BFS
			          //还有重要的一点就是从终点BFS,如果从每个点开始BFS太麻烦
	{
		h=myqueue.front();
		myqueue.pop();
		x=h.x,y=h.y;
		if (y!=1 && maze[x][y-1]==' ' && cnt[x][y-2]>cnt[x][y]+1)  //关键在于最后一个式子,只有满足才走
								//这条路,否则不走
		{
			k.x=x;
			k.y=y-2;
			cnt[x][y-2]=cnt[x][y]+1;
			myqueue.push(k);
		}
		if (y!=2*W-1 && maze[x][y+1]==' ' && cnt[x][y+2]>cnt[x][y]+1)
		{
			k.x=x;
			k.y=y+2;
			cnt[x][y+2]=cnt[x][y]+1;
			myqueue.push(k);
		}
		if (x!=1 && maze[x-1][y]==' ' && cnt[x-2][y]>cnt[x][y]+1)
		{
			k.x=x-2;
			k.y=y;
			cnt[x-2][y]=cnt[x][y]+1;
			myqueue.push(k);
		}
		if (x!=2*H-1 && maze[x+1][y]==' ' && cnt[x+2][y]>cnt[x][y]+1)
		{
			k.x=x+2;
			k.y=y;
			cnt[x+2][y]=cnt[x][y]+1;
			myqueue.push(k);
		}
	}
}
void work()
{
	scanf("%d%d",&W,&H);
	getchar();
	for(int i=0; i<=2*H; i++)
	{
		for(int j=0; j<=2*W; j++)
			scanf("%c",&maze[i][j]);
		getchar();
	}
	for(int i=1; i<=2*H-1; i+=2)
		for(int j=1; j<=2*W-1; j+=2)
			cnt[i][j]=INF;
	int _count=0;
	for(int i=1; i<=2*H-1; i+=2)
	{
		for(int j=1; j<=2*W-1; j+=2)
		{
			if((maze[i][j-1]==' '&&j-1==0)||(maze[i-1][j]==' '&&i-1==0)||(maze[i][j+1]==' '&&j+1==2*W)||(maze[i+1][j]==' '&&i+1==2*H))
			{
				if(_count==0) exit1x=i,exit1y=j;
				else if(_count==1) exit2x=i,exit2y=j;
				_count++;
			}
			if(_count==2) goto here;
		}
	}
here:
	while(!myqueue.empty()) myqueue.pop();
	getanswer(exit1x,exit1y);
	getanswer(exit2x,exit2y);
	int m=0;
	for(int i=1; i<=2*H-1; i+=2)
		for(int j=1; j<=2*W-1; j+=2)
			if(cnt[i][j]>m) m=cnt[i][j];
	cout<<m<<endl;
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	freopen("maze1.out","w",stdout);
	freopen("maze1.in","r",stdin);
	work();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值