牛客手速月赛61 C(题意没仔细读,输麻了)

题目
题意: 太长了,略。
思路: 两遍bfs。输麻了,P那个点bfs预处理的时候是不能到达距离>r1的点的,根据题意,而我没有注意到,直接输麻。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e3+10;
int n,m,k,T;
int r1,r2;
char a[N][N];
int dist1[N][N];
int dist2[N][N];
int sx,sy,ex,ey;
PII q[(int)1e6+10];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
void bfs1(int stx,int sty,int r)
{
	int hh = 0,tt = 0;
	dist1[stx][sty] = 0;
	queue<PII>q;
    q.push({stx,sty});
	while(q.size())
	{
		auto tmp = q.front(); q.pop();
		int x = tmp.first;
		int y = tmp.second;
        if(dist1[x][y] == r) continue;
		for(int i=0;i<4;++i)
		{
			int tx = x+dx[i];
			int ty = y+dy[i];
			if(tx<1||ty<1||tx>n||ty>m||dist1[tx][ty]<1e9||a[tx][ty]=='*') continue;
			dist1[tx][ty] = dist1[x][y]+1;
			q.push({tx,ty});
		}
	}
}
void bfs2(int stx,int sty)
{
	int hh = 0,tt = 0;
	dist2[stx][sty] = 0;
	queue<PII>q;
    q.push({stx,sty});
	while(q.size())
	{
		auto tmp = q.front(); q.pop();
		int x = tmp.first;
		int y = tmp.second;
		for(int i=0;i<4;++i)
		{
			int tx = x+dx[i];
			int ty = y+dy[i];
			if(tx<1||ty<1||tx>n||ty>m||dist2[tx][ty]<1e9||a[tx][ty]=='*') continue;
			dist2[tx][ty] = dist2[x][y]+1;
			q.push({tx,ty});
		}
	}
}
void solve()
{
	int ans = 1e9;
	cin>>n>>m;
	cin>>r1>>r2;
// 	cin.tie();
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
            dist1[i][j] = dist2[i][j] = 1e9;
			cin>>a[i][j];
			if(a[i][j]=='P') sx = i,sy = j;
			if(a[i][j]=='M') ex = i,ey = j;
		}
	}
	bfs1(sx,sy,r1);
	bfs2(ex,ey);
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            int sum = abs(i-sx)+abs(j-sy);
			int sum2 = abs(i-ex)+abs(j-ey);
			if(sum>r1||sum2>r2||a[i][j]=='*') continue;
            int res1 = dist1[i][j];
			int res2 = dist2[i][j];
// 			if(res1==1e9||res2==1e9) continue;
			ans = min(ans,res1+res2);
        }
    }
// 	for(int i=sx-r1;i<=sx+r1;++i)
// 	{
// 		for(int j=sy-r1;j<=sy+r1;++j)
// 		{
// 			int sum = abs(i-sx)+abs(j-sy);
// 			int sum2 = abs(i-ex)+abs(j-ey);
// 			if(sum>r1||sum2>r2||i<1||j<1||i>n||j>m||a[i][j]=='*') continue;
// 			int res1 = dist1[i][j];
// 			int res2 = dist2[i][j];
// // 			if(res1==1e9||res2==1e9) continue;
// 			ans = min(ans,res1+res2);
// 		}
// 	}
	if(ans==1e9) ans = -1;
	cout<<ans;
}
signed main(void)
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	T = 1;
//	cin>>T;
	while(T--)
	solve();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值