本文主要是介绍牛客小白月赛-D-游戏购买,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
仅记录做此题时出现的问题
1.审错题,一开始是想错了,想着连续到点购买,结果写成了正确的bfs,后面又把自己绕进去,以为是需要连续购买,没注意题目中说的一个,还是得注意审题啊
2.写的细节点错误太多
比如>x的x和下标x弄混,d和d_fat乱用
其实整体思路没问题,就是实现的时候把自己搞混,读题没读明白的问题
// Problem: 游戏购买!
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/45670/D
// Memory Limit: 1048576 MB
// Time Limit: 10000 ms
// Date: 2024-03-15 09:09:22
//
// Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
using namespace std;
int n, m, xx, sx, sy, ex, ey;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
int d[2005][2005];
int d_fat[2005][2005];
int vis[2005][2005];
int vis_fat[2005][2005];
int ches[2005][2005];
int ans = INT_MAX;
void bfs(int x, int y) {queue<pair<int, int>>q;vis[x][y] = 1;d[x][y] = 0;q.push({ x,y });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] != -1) {vis[nx][ny] = 1;d[nx][ny] = d[u][v] + 1;q.push({ nx,ny });}}}
}
void bfs_fat(int x, int y) {queue<pair<int, int>>q;vis_fat[x][y] = 1;d_fat[x][y] = 0;q.push({ x,y });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_fat[nx][ny] && ches[nx][ny] != -1) {vis_fat[nx][ny] = 1;d_fat[nx][ny] = d_fat[u][v] + 1;q.push({ nx,ny });if (ches[nx][ny] > xx && vis[nx][ny]) {ans = min(ans, d_fat[nx][ny] + d[nx][ny]);}}}}
}
void solve() {cin >> n >> m >> xx;cin >> sx >> sy >> ex >> ey;for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {cin >> ches[i][j];}}bfs(sx, sy);bfs_fat(ex, ey);// for (int i = 1; i <= n; ++i) {// for (int j = 1; j <= m; ++j) {// cout << d[i][j] << " ";// }// cout << endl;// }// cout << endl;// for (int i = 1; i <= n; ++i) {// for (int j = 1; j <= m; ++j) {// cout << d_fat[i][j] << " ";// }// cout << endl;// }// cout << endl;if (ans == INT_MAX)cout << -1 << endl;else cout << ans << endl;
}
signed main() {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int t = 1;//cin >> t;while (t--) {solve();}return 0;
}
这篇关于牛客小白月赛-D-游戏购买的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!