牛客小白月赛61-C-小喵觅食

很经典的bfs,就是从猫咪和MM的坐标开始bfs搜索

不过这题有些小细节需要注意

1.认真审题,注意,猫一旦闻到小鱼干的味道,开始动,此时MM就不动了,一开始没仔细审题,很不好的习惯

2.注意移动的条件,vis,不是墙,距离是MM的移动距离范围内

3.这个猫咪的r2是闻味道的r2,不是移动距离的r2,还是审题的问题

4.猫闻到味道,开始动,此时是一直bfs,直到到达MM的坐标,因此需要对MM停下的位置做个标记

这道题很经典,实现起来也需要注意些细节,非常好的一道题,很有练习意义

// Problem: 小喵觅食
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/46597/C
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Date: 2024-03-14 20:47:16
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
using namespace std;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
int r1, r2, xc, yc, xm, ym, n, m;
int dis(int xb, int yb, int xed, int yed) {
	return abs(xb - xed) + abs(yb - yed);
}
char ches[1003][1003];
int vis[1003][1003];
int vis_c[1003][1003];
int dm[1003][1003];
int dc[1003][1003];
bool smell = false;
int ans = INT_MAX;
void bfs_m(int x, int y) {
	queue<pair<int, int>>q;
	q.push({ x,y });
	vis[x][y] = 1;
	dm[x][y] = 0;
	if (dis(x, y, xc, yc) <= r2) {
		smell = true;
		vis[x][y] = 1e9;
		return;
	}
	while (q.size()) {
		int u = q.front().first;
		int v = q.front().second; q.pop();
		for (int i = 0; i < 4; ++i) {
			int nx = u + dx[i];
			int ny = v + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m)continue;
			if (!vis[nx][ny] && ches[nx][ny] != '*' && dis(x, y, nx, ny) <= r1) {
				q.push({ nx,ny });
				vis[nx][ny] = 1;
				dm[nx][ny] = dm[u][v] + 1;
				if (dis(nx, ny, xc, yc) <= r2) {
					smell = true;
					vis[x][y] = 1e9;
					return;
				}
			}
		}
	}
}
void bfs_c(int x, int y) {
	queue<pair<int, int>>q;
	q.push({ x,y });
	vis_c[x][y] = 1;
	dc[x][y] = 0;
	while (q.size()) {
		int u = q.front().first;
		int v = q.front().second; q.pop();
		for (int i = 0; i < 4; ++i) {
			int nx = u + dx[i];
			int ny = v + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m)continue;
			if (!vis_c[nx][ny] && ches[nx][ny] != '*') {
				q.push({ nx,ny });
				vis_c[nx][ny] = 1;
				dc[nx][ny] = dc[u][v] + 1;
				if (vis[nx][ny] == 1e9) {
					ans = min(ans, dc[nx][ny] + dm[nx][ny]);
				}
			}
		}
	}
}
void solve() {
	cin >> n >> m;
	cin >> r1 >> r2;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> ches[i][j];
			if (ches[i][j] == 'P')xm = i, ym = j;
			if (ches[i][j] == 'M')xc = i, yc = j;
		}
	}
	bfs_m(xm, ym);
	if (!smell) cout << -1 << endl;
	else {
		bfs_c(xc, yc);
		if (ans != INT_MAX)
			cout << ans << endl;
		else
			cout << -1 << endl;
	}
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

相关推荐

  1. 61-C-觅食

    2024-03-15 14:36:03       21 阅读
  2. 58-C-

    2024-03-15 14:36:03       21 阅读
  3. 61-E-排队

    2024-03-15 14:36:03       16 阅读
  4. 83

    2024-03-15 14:36:03       39 阅读
  5. 84

    2024-03-15 14:36:03       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 14:36:03       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 14:36:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 14:36:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 14:36:03       18 阅读

热门阅读

  1. C# MG.CamCtrl 工业相机库(开源) 海康 大恒

    2024-03-15 14:36:03       24 阅读
  2. nginx升级版本

    2024-03-15 14:36:03       21 阅读
  3. 计算机网络 网络层设备的 冲突域和广播域

    2024-03-15 14:36:03       23 阅读
  4. H12-821_210

    2024-03-15 14:36:03       18 阅读
  5. openGauss SQL引擎插件开发指导

    2024-03-15 14:36:03       17 阅读
  6. ES6基础4

    2024-03-15 14:36:03       17 阅读
  7. MySQL 8.0 的 SQL 优化建议

    2024-03-15 14:36:03       18 阅读
  8. Qt 的d指针

    2024-03-15 14:36:03       19 阅读