题解 | #矩阵的最小路径和#
矩阵的最小路径和
https://www.nowcoder.com/practice/38ae72379d42471db1c537914b06d48e
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
int m,n,a[500][500],b[500][500];
cin>>n;
cin>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
if(i==0&&j==0)
{
b[0][0]=a[i][j];
}
if(i==0&&j>0)
{
b[0][j]=b[0][j-1]+a[i][j];
}
if(i>0&&j==0)
{
b[i][0]=b[i-1][0]+a[i][j];
}
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
b[i][j]=min(b[i-1][j],b[i][j-1])+a[i][j];
}
}
cout<<b[n-1][m-1];
}
// 64 位输出请用 printf("%lld")
#22求捞#
查看3道真题和解析
