社区讨论
90分求助
P9065 [yLOI2023] 云梦谣参与者 3已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo334vtw
- 此快照首次捕获于
- 2023/10/24 00:01 2 年前
- 此快照最后确认于
- 2023/10/24 00:01 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
int n, m, k, yu[3010][3010], vis[3010][3010], h[3010][3010], flag;
struct node{
int s, t, bu;
};
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
return x*f;
}
bool operator < (const node &a, const node &b){
return a.bu > b.bu;
}
priority_queue<node> q;
void bfs(){
q.push((node){1, 1, 0});
while(!q.empty()){
int a = q.top().s, b = q.top().t, bu = q.top().bu;
q.pop();
if(a == n && b == m){
cout << bu;
exit(0);
}
if(vis[a][b]){
continue;
}
vis[a][b] = 1;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for(int i = 0;i < 4;i++){
int aa = a + dir[i][0], bb = b + dir[i][1];
if(!vis[aa][bb] && h[aa][bb] && aa >= 1 && bb >= 1 && aa <= n && bb <= m){
q.push((node){aa, bb, bu + 1});
}
}
if(flag == 0 && yu[a][b]){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if((i != a || j != b) && yu[i][j]){
if(h[i][j] == h[a][b])
q.push((node){i, j, bu + 1});
else
q.push((node){i, j, bu + 2});
}
}
}
flag = 1;
}
}
}
int main(){
n = read(), m = read(), k = read();
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
h[i][j] = read();
}
}
while(k--){
int x, y;
x = read(), y = read();
yu[x][y] = 1;
}
bfs();
puts("-1");
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...