Honeycomb——BFS

【题目描述】

传送门

【题目分析】

看起来很复杂好像还要建图什么的,其实直接在原图上BFS就可以了,设置一下方向数组,然后直接跑就可以了。

【AC代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cctype>
#include<queue>
#include<set>

using namespace std;

typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=6e3+15;
char ss[MAXN][MAXN];
int ans;
int n,m;

struct node
{
	int x,y,step;
	node(int _x=0,int _y=0,int _step=0):x(_x),y(_y),step(_step){}
}p,S,T;
const int drc[6][2]={-2,0,-1,3,1,3,2,0,1,-3,-1,-3};

void BFS()
{
	queue<node> q;
	S.step=1;
	T.step=-1;
	q.push(S);
	while(!q.empty())
	{
		p=q.front(); q.pop();
		if(p.x==T.x && p.y==T.y)
		{
			T.step=p.step;
			break;
		}
		int u,v;
		for(int i=0;i<6;i++)
		{
			u=p.x+drc[i][0]; v=p.y+drc[i][1];
			if(ss[u][v]==' ')
			{
				u+=drc[i][0]; v+=drc[i][1];
				if(ss[u][v]=='#') continue;
				ss[u][v]='#';
				q.push(node(u,v,p.step+1));
			}
		}
	}
}

int main()
{
	int TT;
	scanf("%d",&TT);
	while(TT--)
	{
		scanf("%d%d",&n,&m);
		getchar();
		for(int i=0,limit=4*n+3;i<limit;i++)
		{
			fgets(ss[i],MAXN,stdin);
			int len=strlen(ss[i]);
			for(int j=0;j<len;j++)
			{
				if(ss[i][j]=='S')
				{
					S.x=i; S.y=j;
				}
				if(ss[i][j]=='T')
				{
					T.x=i; T.y=j;
				}
			}
		}
        BFS();
		printf("%d\n",T.step);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值