SGU 168

时间:2023-01-04 04:41:43

SGU 168,寻找矩阵中右上方,右方,下方最小的元素,采用动态规划解答。

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <string.h>
using namespace std; int n, m;
int A[][];
int f[][]; int B(int u, int v) {
int move[][] = {-, , , , , };
if (f[u][v] != ) {
return f[u][v];
}
int ans = A[u][v];
//f[u][v] = A[u][v];
for (int i = ; i < ; i++) {
int uu = u + move[i][];
int vv = v + move[i][];
if (uu >= && uu < n && vv >= && vv < m) {
ans = min(ans, B(uu, vv));
}
}
return f[u][v] = ans;
} int main() {
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
scanf("%d", &A[i][j]);
f[i][j] = ;
}
} for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
printf("%d ", B(i, j));
}
printf("\n");
} //system("pause");
}