SGU 168.Matrix

时间限制:0.5s

空间限制:15M

题意:

给出一个N*M的矩阵A,计算矩阵B,满足B[i][j]=min{ A[x][y]:(y>=j) and ( x>=i+j-y )}



Solution :

如图方式从右下角遍历矩阵,那么可令B[i][j]=min(A[i][j],B[i-1][j],B[i][j+1],B[i-1][j+1])

动态规划即可。

时间复杂度O(n*m)

code

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int A[1009][1009], B[1009][1009];
int n, m;

int main() {
	scanf ("%d %d", &n, &m);
	memset (B, 0x3f, sizeof B);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) scanf ("%d", &A[i][j]);
	int x = n, y = m, i = n, j = m;
	while (x >= 1 || y >= 1) {
		while (i <= n && j >= 1) {
			B[i][j]  = min (min (A[i][j], B[i][j + 1]), min (B[i + 1][j], B[i - 1][j + 1]) );
			i++, j--;
		}
		if (i > n || j < 1) {
			i = --x, j = y;
			if (i <= 0) i = 1, j = --y;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++)
			printf ("%d ", B[i][j]);
		putchar (10);
	}
	return 0;
}

  

  

SGU 168.Matrix

时间: 2024-10-07 18:33:37

SGU 168.Matrix的相关文章

[SGU 196] Matrix Multiplication

196. Matrix Multiplication time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard output: standard Description Let us consider an undirected graph G = <V, E> which has N vertices and M edges. Incidence matrix of this graph is

SGU 249.Matrix(Gray码)

题意: 用0到2^(n+m-1)这2^(n+m-1)个数填在一个2^n*2^m的矩阵里,使得所有相邻的数的二进制表示只有一位不同. Solution: Gray码.对于第i行第j列的数,由i的Gray码左移m位并上j的Gray码得到. #include <cstdio> using namespace std; int n, m, x; int main() { scanf("%d %d",&n,&m); for (int i = 0; i < (1

算法导论-求(Fibonacci)斐波那契数列算法对比

目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 .自底向上算法(Bottom-up) 5. 递归平方算法(Recursive squaring) 6.完整代码(c++) 7.参考资料 内容 1.斐波那契数列(Fibonacci)介绍 Fibonacci数列应该也算是耳熟能详,它的递归定义如上图所示. 下面2-6分别说明求取Fibonacci数列的

[题解]UVa 11082 Matrix Decompressing

开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且题目中然而并没有什么要求,所以说可以考虑思考一下这道题有木有什么"套路"之类的通法) 比如说有这么一组数据 原矩阵 1 2 3 4 7 8 9 5 6 输入 3 3 6 25 45 14 28 45 然后将每一行的和写在每一列对应的行上(很明显有问题) 6 0 0 19 0 0 20 0

(最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

You're given a matrix A of size n?×?n. Let's call the matrix with nonnegative elements magic if it is symmetric (so aij?=?aji), aii?=?0 and aij?≤?max(aik,?ajk) for all triples i,?j,?k. Note that i,?j,?k do not need to be distinct. Determine if the ma

51. 顺时针打印矩阵[print matrix in clockwise direction]

[题目] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 例如:如果输入如下矩阵: 1            2            3            4 5            6            7            8 9            10          11           12 13          14          15           16 则依次打印出数字1, 2, 3, 4, 8, 12, 16, 15, 14

《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数008,matrix,矩阵函数

1 ** 3dFromFundamentalMatrix( Rows1, Cols1, Rows2, Cols2, CovRR1, CovRC1, CovCC1, CovRR2, CovRC2, CovCC2, FMatrix, CovFMat, out X, out Y, out Z, out W, out CovXYZW); 2 说明, 从矩阵建立3d对象 3 4 ** AbsMatrix( MatrixID, out MatrixAbsID); 5 说明, abs_matrix ( : :

sgu 463 - Walking around Berhattan

K - Walking around Berhattan Time Limit:250MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 463 Description As you probably know, Berhattan is a district of Berland's largest city and it consists of equal squar

Fast Matrix Operations(UVA)11992

UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素add上val: 2 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素set为val: 3 x1 y1 x2 y2 val 表示输