This commit is contained in:
Zengtudor 2024-10-11 16:43:27 +08:00
parent 6195d14fc3
commit 877f112296
2 changed files with 82 additions and 1 deletions

View File

@ -18,7 +18,7 @@ cal(a,b)
5)a<b/2 && b is odd return cal(a,b/2)+1; 5)a<b/2 && b is odd return cal(a,b/2)+1;
6)a<b/2 && b is even return cal(a,b-1)+1; 6)a<b/2 && b is even return cal(a,b-1)+1;
*/ */
ull cal(ull a,ull b){ ull cal(const ull a,const ull b)noexcept{
if(a==b){ if(a==b){
return 0; return 0;
}else if(a>b){ }else if(a>b){

81
src/oj4980/oj4980.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdio>
#include <iostream>
#include <limits>
#include <algorithm>
#include <queue>
#include <bitset>
typedef long long ll;
auto &is = std::cin;
auto &os = std::cout;
struct Point{
ll x,y;
};
struct Status{
Point now;
ll step;
bool operator<(const Status &that)const noexcept{
return this->step > that.step;
}
};
const ll max_nm {200+5};
ll n, h, w, ans{std::numeric_limits<decltype(ans)>::max()};
char map[max_nm][max_nm];
Point me;
const int to_next[4][2] {
{1,0},
{0,1},
{-1,0},
{0,-1},
};
void bfs(const Point start)noexcept{
std::bitset<max_nm> vis[max_nm];
std::priority_queue<Status> q;
ans = std::numeric_limits<decltype(ans)>::max();
vis[start.x][start.y] = true;
q.push({start,0});
while(q.empty()==false){
const auto status = q.top();
const auto nowchar = [&status]()->char{return map[status.now.x][status.now.y];};
q.pop();
if(nowchar()=='a'){
ans = std::min(ans,status.step);
return;
}
for(ll i{0};i<4;i++){
const Point next {status.now.x+to_next[i][0],status.now.y+to_next[i][1]};
if(vis[next.x][next.y])continue;
const auto nextchar = [&next]()->char{return map[next.x][next.y];};
ll cost {1};
if(next.x>h || next.x<=0 || next.y > w || next.y<=0
|| nextchar()=='#')continue;
if(nextchar()=='x')cost++;
const Status next_status {next,status.step+cost};
vis[next_status.now.x][next_status.now.y] = true;
q.push(next_status);
}
}
return;
}
int main(){
is>>n;
for(ll _{0};_<n;_++){
is>>h>>w;
for(ll i{1};i<=h;i++){
for(ll j{1};j<=w;j++){
is>>map[i][j];
if(map[i][j]=='r')me.x=i,me.y=j;
}
}
bfs(me);
os<<ans<<'\n';
}
}