sgu 168 Matrix

31 篇文章 0 订阅
31 篇文章 0 订阅
168. Matrix
time limit per test: 1 sec.
memory limit per test: 16000 KB
input: standard
output: standard



You are given N*M matrix A. You are to find such matrix B, that B[i,j]=min{ A[x,y] : (y>=j) and (x>=i+j-y) }

Input
On the first line of the input there are two integer numbers, N and M (1<=N,M<=1000). Then matrix A follows: next N lines contains M integers each (not greater than 32000 by absolute value). The j-th number on then i-th of this lines is A[i,j].

Output
Write matrix B in the same format as matrix A, but without N and M.

Sample test(s)

Input
3 3
1 2 3
4 5 6
7 8 9

Output
1 2 3
2 3 6
3 6 9

Author:NNSU #2 team
Resource:
Date:









标准的斜线dp ,开始不知道为什么WA了,估计是写挫了,后来愤怒重写就A了。


观察 y> =j 且 x+y>=i+j ,那么dp(i,j) = min( dp[i-1,j+1) dp(i,j),dp(i+1,j+1) dp(i,j+) )


贴上代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 1010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

int a[nMax][nMax],b[nMax][nMax];
int n,m;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int k=m+n-2;k>=0;k--){
        for(int j=m-1;j>=0;j--){
            int i=k-j;
            if(i<0||i>=n) continue;
            b[i][j]=a[i][j]; 
            if(i+1<n)           b[i][j]=min(b[i][j],b[i+1][j]); 
            if((i-1>=0)&&(j+1<m))   b[i][j]=min(b[i][j],b[i-1][j+1]); 
            if(j+1<m)           b[i][j]=min(b[i][j],b[i][j+1]);  
            if((i+1<n)&&(j+1<m))    b[i][j]=min(b[i][j],b[i+1][j+1]);  
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(j)printf(" ");
            printf("%d",b[i][j]);
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值