NYOJ 104 매트릭스 최대 와-단순 동적 계획

 
 //http://acm.nyist.net/JudgeOnline/problem.php?pid=104
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define COL nn
#define ROW n
const int M = 1e2 +10;
const int inf = -(1<<30);

int m[M][M];

int main(){
	//freopen("in.txt","r",stdin);
	//freopen("Input (4).txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int nn,n,Ncase;cin >> Ncase;
	while(Ncase--){
		cin >> n >> nn;
		for(int i = 1 ; i <= ROW ; i ++){
			for(int j = 1 ; j <= COL ; j ++)
				scanf("%d",&m[i][j]);
		}
		for(int i = 1 ; i <= COL ; i++){
			for(int j = 2 ; j <= ROW ; j++){
				 m[j][i] = m[j][i] + m[j-1][i];	
			}
		}
		int tmp = inf ,max = inf, sum;
		for(int i = 0 ; i <= ROW -1 ; i++){
			for(int j = i +1 ; j <= ROW ; j++){
				sum = inf;
				for(int k = 1 ; k <= COL ; k++){
					int tmp = m[j][k] - m[i][k];
					sum = ((sum > 0)?(sum):(0)) + tmp;
					if(max < sum)
						max = sum;
				}
			}	
		}
		cout << max << endl;
	}
	return 0;
}                


 

좋은 웹페이지 즐겨찾기