Problem Description
一个N*M的矩阵A.你要获得一个B矩阵.
B[i][j] = min { A[x][y] | y >= j && x >= i+j-y}
Input
第一行N和M.表示N行M列.(1<=N,M<=1000)
接下来N行每行M个数字.数字为小于10^8的正整数.
第i行第j列的数字用A[i][j] 表示。
Output
输出B矩阵。数字间用空格隔开。(行末无空格)
Sample Input
3 3
1 2 3
4 5 6
7 8 9
Sample Output
1 2 3
2 3 6
3 6 9
解题思路
dp[i][j]要从一个类似梯形的位置(具体方向看dir数组)来转移状态…
参考代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1010
#define inf 0x3f3f3f3f
using namespace std;
int dp[N][N];
int dir[5][2]={{0,0},{1,0},{1,1},{0,1},{-1,1}};
int main()
{
int n,m;
scanf("%d %d",&n,&m);
memset(dp,inf,sizeof(dp));
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
scanf("%d",&dp[i][j]);
for (int i = m;i > 0;--i)
for (int j = n;j > 0;j--)
for (int k = 0;k < 5;k++)
dp[j][i] = min(dp[j][i],dp[j+dir[k][0]][i+dir[k][1]]);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
printf("%d%s",dp[i][j],(j == m?"\n":" "));
return 0;
}