题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1047
水过。。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <cmath>
#include <ctime>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define eps 1e-8
#define LL long long
#define mp make_pair
const int maxn=1000+5;
int dp_max[maxn][maxn],dp_min[maxn][maxn];
int mx[maxn][maxn],mn[maxn][maxn];
int A[maxn][maxn];
void read_int(int&x)
{
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
x=ch-'0'; ch=getchar();
while('0'<=ch&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
}
inline int min(int a,int b) { return a<b?a:b; }
inline int max(int a,int b) { return a>b?a:b; }
int main()
{
// freopen("in.txt","r",stdin);
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)==3) {
rep(i,0,a) rep(j,0,b) {
read_int(A[i][j]);
dp_min[i][j]=dp_max[i][j]=A[i][j];
mx[i][j]=mn[i][j]=A[i][j];
}
rep(i,2,n+1) {
rep(r,i-1,a) rep(c,i-1,b) {
int x1=A[r-i+1][c],x2=A[r][c-i+1];
if(x1>x2) swap(x1,x2);
if(x2>mx[r][c]) mx[r][c]=x2;
if(x1<mn[r][c]) mn[r][c]=x1;
}
rrep(r,a-1,i-1) rrep(c,b-1,i-1) {
dp_min[r][c]=min(dp_min[r-1][c-1],mn[r][c]);
dp_max[r][c]=max(dp_max[r-1][c-1],mx[r][c]);
}
}
int ans=(1<<31)-1;
rep(i,n-1,a) rep(j,n-1,b)
if(dp_max[i][j]-dp_min[i][j]<ans) ans=dp_max[i][j]-dp_min[i][j];
printf("%lld\n",ans);
}
}