「杂题乱刷」AT_abc007_3

发布时间 2024-01-06 17:32:23作者: wangmarui

传送门(at)

传送门(luogu)

深搜 & 广搜的模板题。

这题深搜比较简单,只需要记忆化即可,我们来考虑一下广搜,实际上这题广搜的思路与记忆化差不多,开个结构体分别记录 \(x,y,minn\) 表示 \(x,y\) 坐标及到这个坐标的最小次数,容易证明每次搜到的一定就是这个坐标的最小值,时间复杂度 \(O(n \times m)\),可以通过此题。

参考代码:

点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
*/
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll n,m,stx,sty,enx,eny,vis[110][110],fx[]={0,0,1,-1},fy[]={1,-1,0,0};
char a[110][110];
struct node{
	ll x,y,minn;
};
queue<node>q;
void bfs()
{
	vis[stx][sty]=1;
	q.push({stx,sty,0});
	while(!q.empty())
	{
		node now=q.front();
		if(now.x==enx && now.y==eny)
		{
			cout<<now.minn<<endl;
			return ;
		}
		q.pop();
		forl(i,0,3)
			if(a[now.x+fx[i]][now.y+fy[i]]=='.' && !vis[now.x+fx[i]][now.y+fy[i]])
			{
				vis[now.x+fx[i]][now.y+fy[i]]=1;
				if(now.x+fx[i]==enx && now.y+fy[i]==eny)
				{
					cout<<now.minn+1<<endl;
					return ;
				}
				q.push({now.x+fx[i],now.y+fy[i],now.minn+1});
			}
	}
}
int main()
{
	IOS;
	cin>>n>>m>>stx>>sty>>enx>>eny;
	forl(i,1,n)
		forl(j,1,m)
			cin>>a[i][j];
	bfs();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}