【问题描述】国王奖励给屠龙勇士4块连续的封地,请在nn的封地(每块封地为边长11的正方形)范围内,选择具有至少一条公共边与其他封地相连的4块封地。每块封地有一个财富值,请你帮助勇士挑选封地,使得财富值最大。【输入形式】第一行一个整数n;下面n行,每行由空格隔开的n个正整数表示的封地财富值;【输出形式】1个整数,4块封地的最大的财富值之和。
【样例输入】5
1 1 1 1 1
1 1 1 2 1
1 1 2 2 3
1 1 1 1 1
1 1 1 1 1
【样例输出】
9
【样例说明】
四块封地的样式可以参考“俄罗斯方块”。
【评分标准】2<=n<20;数据保证4块封地的财富之和小于2^31。
这个近似于最短路径问题,涉及到算法了,初学c++做这么难的题可还行
这个题目很重要的一点就是它的提示。俄罗斯方块。
#include <stdio.h>
#include <stdlib.h>
#define N 1000 //根据情况自己调
int cntFun(int (*a)[N], int n, int h, int l) {
int max=a[h][l], res;
int hx, ly; //
hx = h + 3;
ly = l + 3;
if (hx >= n)
hx = n - 1;
if (ly >= n)
ly = n - 1;
// 7种情况
// 1
//**
//**
if (hx > h && ly > l) {
res = a[h][l] + a[h + 1][l] + a[h][l + 1] + a[h + 1][l + 1];
if (max < res)
max = res;
}
// 2
//*
//*
//*
//*
if (h + 3 == hx) {
res = a[h][l] + a[h + 1][l] + a[h + 2][l] + a[h + 3][l];
if (max < res)
max = res;
}
// 3
//****
if (l + 3 == ly) {
res = a[h][l] + a[h][l + 1] + a[h][l + 2] + a[h][l + 3];
if (max < res)
max = res;
}
// 4
//***
// *
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l] + a[h][l + 1] + a[h][l + 2] + a[h + 1][l + 2];
if (max < res)
max = res;
}
// 5
//***
//*
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l] + a[h][l + 1] + a[h][l + 2] + a[h + 1][l];
if (max < res)
max = res;
}
// 6
//*
//*
//**
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l] + a[h + 1][l] + a[h + 2][l] + a[h + 2][l + 1];
if (max < res)
max = res;
}
// 7
//**
//*
//*
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l] + a[h + 1][l] + a[h + 2][l] + a[h][l + 1];
if (max < res)
max = res;
}
// 8
//***
// *
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l] + a[h][l + 1] + a[h][l + 2] + a[h + 1][l + 1];
if (max < res)
max = res;
}
// 9
// *
//***
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h + 1][l] + a[h + 1][l + 1] + a[h + 1][l + 2] + a[h][l + 1];
if (max < res)
max = res;
}
// A
//*
//**
//*
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l] + a[h + 1][l] + a[h + 2][l] + a[h + 1][l + 1];
if (max < res)
max = res;
}
// B
// *
//**
// *
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l + 1] + a[h + 1][l + 1] + a[h + 2][l + 1] + a[h + 1][l];
if (max < res)
max = res;
}
// C
//*
//**
// *
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l] + a[h + 1][l] + a[h + 2][l + 1] + a[h + 1][l + 1];
if (max < res)
max = res;
}
// D
// *
//**
//*
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l + 1] + a[h + 1][l + 1] + a[h + 1][l] + a[h + 2][l];
if (max < res)
max = res;
}
// E
//**
// **
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l] + a[h][l + 1] + a[h + 1][l + 1] + a[h + 1][l + 2];
if (max < res)
max = res;
}
// F
// **
//**
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l + 1] + a[h][l + 2] + a[h + 1][l] + a[h + 1][l + 1];
if (max < res)
max = res;
}
// G
//**
// *
// *
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l] + a[h][l + 1] + a[h + 1][l + 1] + a[h + 2][l + 1];
if (max < res)
max = res;
}
// H
// *
// *
//**
if (l + 1 <= ly && h + 2 <= hx) {
res = a[h][l + 1] + a[h + 1][l + 1] + a[h + 2][l + 1] + a[h + 2][l];
if (max < res)
max = res;
}
// I
//*
//***
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l] + a[h + 1][l] + a[h + 1][l + 1] + a[h + 1][l + 2];
if (max < res)
max = res;
}
// J
// *
//***
if (l + 2 <= ly && h + 1 <= hx) {
res = a[h][l + 2] + a[h + 1][l] + a[h + 1][l + 1] + a[h + 1][l + 2];
if (max < res)
max = res;
}
return max;
}
int main() {
int n;
int a[N][N];
int i, j;
int max = 0, res;
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
res = cntFun(a, n, i, j);
if (max < res)
max = res;
}
printf("%d", max);
return 0;
}
/*
5
1 1 1 1 1
1 1 1 2 1
1 1 2 2 3
1 1 1 1 1
1 1 1 1 1
*/
```c
```