mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-17 21:42:25 +00:00
feat: 添加P8435.cpp和P4554.cpp解题代码
P4554.cpp实现了一个基于双端队列的BFS算法,用于解决网格图中的最短路径问题
This commit is contained in:
parent
e66f06db69
commit
1f4768391d
62
src/10/2/P4554.cpp
Normal file
62
src/10/2/P4554.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
#define sl static inline
|
||||
const ll maxn = 505;
|
||||
ll n,m,x1,y1,x2,y2,inf=1e9+7;
|
||||
char c[maxn][maxn];
|
||||
ll ans[maxn][maxn];
|
||||
|
||||
struct P{
|
||||
ll x,y,s;
|
||||
};
|
||||
|
||||
ll dis[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
|
||||
|
||||
sl int solve(){
|
||||
std::cin>>n>>m;
|
||||
if(n==0 && m==0)return 1;
|
||||
for(ll i=0;i<n;i++){
|
||||
for(ll j=0;j<m;j++){
|
||||
std::cin>>c[i][j];
|
||||
ans[i][j]=inf;
|
||||
}
|
||||
}
|
||||
// printf("line = %d\n",__LINE__);
|
||||
std::cin>>x1>>y1>>x2>>y2;
|
||||
std::deque<P> dq;
|
||||
dq.emplace_back(x1,y1,0);
|
||||
while(dq.size()){
|
||||
auto [x,y,s] = dq.front();
|
||||
dq.pop_front();
|
||||
for(ll i=0;i<4;i++){
|
||||
ll nx=x+dis[i][0],ny=y+dis[i][1];
|
||||
if(nx<0||nx>n-1||ny<0||ny>m-1)continue;
|
||||
ll ns = s+(c[x][y]!=c[nx][ny]);
|
||||
if(nx==x2&&ny==y2){
|
||||
std::cout<<ns<<"\n";
|
||||
return 0;
|
||||
}
|
||||
if(ns<ans[nx][ny]){
|
||||
ans[nx][ny]=ns;
|
||||
if(c[x][y]!=c[nx][ny]){
|
||||
dq.emplace_back(nx,ny,ns);
|
||||
}else{
|
||||
dq.emplace_front(nx,ny,s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
while (!solve());
|
||||
}
|
9
src/10/2/P8435.cpp
Normal file
9
src/10/2/P8435.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include <cstdint>
|
||||
|
||||
using ll = int64_t;
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user