题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859
题目:给出一个矩阵,求出指定子矩阵中的最小元素。
我用的不是正规解法,纯属水过去的……这个是留着自己看的,大家就不要学了。
1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 300 + 10; 5 6 int map[MAXN][MAXN]; 7 int Mat[MAXN][MAXN]; 8 9 int n; 10 int INF = 2147483645; 11 12 int min( int a, int b ) 13 { 14 return a < b ? a : b; 15 } 16 17 int Findmin( int x1, int y1, int x2, int y2 ) 18 { 19 if ( x1 == 1 && y1 == 1 ) return Mat[x2][y2]; 20 if ( Mat[x2][y2] != Mat[x1][y2] && Mat[x2][y2] != Mat[x2][y1] ) return Mat[x2][y2]; 21 22 int mmin = INF; 23 for ( int i = x1; i <= x2; i++ ) 24 for ( int j = y1; j <= y2; j++ ) 25 if ( map[i][j] < mmin ) mmin = map[i][j]; 26 27 return mmin; 28 } 29 30 int main() 31 { 32 int T; 33 scanf( "%d", &T ); 34 while ( T-- ) 35 { 36 scanf( "%d", &n ); 37 38 for ( int i = 1; i <= n; i++ ) 39 { 40 for ( int j = 1; j <= n; j++ ) 41 { 42 scanf( "%d", &map[i][j] ); 43 if ( i == 1 && j == 1 ) 44 Mat[i][j] = map[i][j]; 45 else if ( i != 1 && j == 1 ) 46 Mat[i][j] = min( map[i][j], Mat[i - 1][j] ); 47 else if ( i == 1 && j != 1 ) 48 Mat[i][j] = min( map[i][j], Mat[i][j - 1] ); 49 else Mat[i][j] = min( map[i][j], min(Mat[i - 1][j], Mat[i][j - 1]) ); 50 } 51 } 52 53 int m; 54 scanf( "%d", &m ); 55 while ( m-- ) 56 { 57 int a, b, c, d; 58 scanf("%d%d%d%d", &a, &b, &c, &d); 59 printf("%d\n", Findmin( a, b, c, d ) ); 60 } 61 } 62 return 0; 63 }