题解 | 矩阵的最小路径和
矩阵的最小路径和
https://www.nowcoder.com/practice/4e5e75f52b594f7ea6122029b3b5ff6b
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using PII=pair<ll,ll>;
using PIII=pair<int,pair<int,int>>;
const ld ESP = 1e-10;
const ld PI = acosl(-1);
const int N=1e5+10;
const int M=2e5+10;
// const int mod = 1000000007;
const int mod = 998244353;
//随机化
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 1000000000);
// cout<<fixed<<setprecision(10);
void solve(){
int n,m;
cin>>n>>m;
int a[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>=2&&j>=2){
a[i][j]+=min(a[i][j-1],a[i-1][j]);
}else{
if(i==1&&j==1)continue;
else if(i==1){
a[i][j]+=a[i][j-1];
}else {
a[i][j]+=a[i-1][j];
}
}
}
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<'\n';
// }
cout<<a[n][m];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _=1;
// cin>>_;
while(_--){
solve();
}
return 0;
}