我真的服了
原题链接:27869.最短挖钻石路径2024-08-12 22:31:03
发布于:广东
这题很难,我都遇到了问题。问题是地图不同步,就很烦——2424 . 8 . 12
全部评论 1
下面是我的代码:
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int vis[105][105],dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1};struct node{
int x,y,step;
};int main(){
vector<int> fa;
queue<node> q;
int n,m,sx,sy,ix,iy,ist,cx,cy,zsv = 0,ans = 1000;
cin >> n >> m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin >> a[i][j];
if(a[i][j] == 's'){
sx = i,sy = j;
}
if(a[i][j] == '@'){
zsv ++;
}
}
}
q.push({sx,sy,0});
while(!q.empty()){
ix = q.front().x, iy = q.front().y, ist = q.front().step;
q.pop();
if(a[ix][iy] == '@'){
zsv --;
}
if(zsv == 0){
fa.push_back(ist);
}
for(int i = 0;i < 4;i ++){
cx = ix + dx[i];
cy = iy + dy[i];
if(vis[cx][cy] == 0 and cx >= 0 and cx < n and cy >= 0 and cy < m){
vis[cx][cy] = 1;
if(a[cx][cy] == '#' or a[cx][cy] == '@'){
q.push({cx,cy,ist + 2});
} else {
q.push({cx,cy,ist + 1});
}
}
}
}
for(auto it : fa){
if(it < ans){
ans = it;
}
}
cout << ans;
return 0;
}2024-08-12 来自 广东
0
有帮助,赞一个