网易c++笔试牛牛分田问题
//暴力剪枝,哪位大神有好的方法请告知啊
#include<iostream>
#include<vector>
#define nu 4
#define MIN 10000
using namespace std;
int matrix(int i1, int j1, int i2, int j2,
vector<vector<int>> &sum)
{
if (i1==0&&j1==0)return sum[i2-1][j2-1];
if (i1 == 0)return sum[i2 - 1][j2 - 1] - sum[i2 - 1][j1 -
1];
if (j1 == 0)return sum[i2 - 1][j2 - 1] - sum[i1 - 1][j2 -
1];
return sum[i2-1][j2-1] + sum[i1-1][j1-1] -
sum[i2-1][j1-1] - sum[i1-1][j2-1];
}
void getsum(int n, int m, vector<vector<int>>&a,
vector<vector<int>>&sum)
{
if (a.size() == 0)return ;
sum = a;
for (size_t i = 0; i < sum.size(); i++)
{
for (size_t j =1; j < sum[0].size(); j++)
{
sum[i][j] += sum[i][j - 1];
}
for (size_t j = 0; i>0&&j < sum[0].size();
j++)
{
sum[i][j] += sum[i - 1][j];
}
}
}
int digui(vector<vector<int>>&sum,int r[],int
c[],int i,int *max)
{
int local_max = 0;
if (sum.size()==0||i == nu)return MIN;
for (r[i] = r[i - 1] + 1; r[i] <= sum.size() - nu +
i;r[i]++)
for (c[i] = c[i - 1] + 1; c[i] <= sum[0].size() - nu +
i; c[i]++)
{
int min = MIN;
int tt = i;
if (i == 3)tt++;
for (int k = 1; k <= tt; k++)
{
if (min < *max)break;
for (int m = 1; m <= tt; m++)
{
int temp = matrix(r[k - 1], c[m - 1], r[k], c[m], sum);
if (temp < min)min = temp;
if (min < *max)break;
}
}
if (min < *max)continue;
int t = digui(sum, r, c, i + 1, max);
if (t < min)min = t;
if (min>local_max)local_max = min;
if (i==1&&local_max>*max)*max =local_max;
}
return local_max;
}
int main()
{
int n, m;
vector<vector<int>> a;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
vector<int>temp(m,0);
int t;
for (int j = 0; j < m; j++)
{
cin >> t;
temp[j] = t;
}
a.push_back(temp);
}
vector<vector<int>> sum;
getsum(n, m, a, sum);
int max=0;
int r[nu+1], c[nu+1];
r[0] = 0;
c[0] = 0;
r[nu] = n;
c[nu] = m;
digui(sum, r, c, 1, &max);
cout << max << endl;
return 0;
}