其他oj的题
  • 板块学术版
  • 楼主chenshenqi
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/4 11:30
  • 上次更新2024/5/4 13:45:26
查看原帖
其他oj的题
1063799
chenshenqi楼主2024/5/4 11:30
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int a[105][105];
int n,m;
struct pos{
    int x,y,s,cost;
    pos (int ax=0,int ay=0,int as=0,int acost=0){
        x=ax,y=ay,s=as,cost=acost;
    }
};
int s=inf,cost=inf;
queue<pos> q;
void bfs(){
    q.push(pos(1,1,0,0));
    while (!q.empty()){
        pos now=q.front();
        q.pop();
        if (now.s>s) continue;
        if (now.x<1||now.y<1||now.x>n||now.y>m) continue;
        if (now.x==n&&now.y==m){
            if (s>now.s) s=now.s,cost=now.cost;
            else{
                if (s==now.s){
                    if (cost>now.cost){
                        cost=now.cost;
                    }
                }    
            }

        }
        now.cost+=a[now.x][now.y];
        q.push(pos(now.x+1,now.y,now.s+1,now.cost));
        q.push(pos(now.x,now.y+1,now.s+1,now.cost));
    }
    cout << cost+a[n][m];
    return;
}
int main(){
    cin >> n >> m;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){
            cin >> a[i][j];
        }
    }
    bfs();
}
2024/5/4 11:30
加载中...