First, the topic requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
The maximum value for the and of all sub-arrays.
Design idea: This problem feels very difficult, the first idea is to find out the maximum number of sub-arrays of each row and find their coordinates, compare whether they are on the same line, and later found it difficult to achieve
By traversing the graph, you can find
The result: It is not what you do, the problem is still difficult.
Second, the source code
#include <iostream>
#include <ctime>
using namespace Std;
#define N 100
typedef struct
{
int dian[n];
int xian[n][n];
int Dianx, Xianx;
}a;
void set (A &shu, int x, int y)
{
Shu.dianx = X*y;
Srand ((unsigned) time (NULL));
for (int i = 1; I <= Shu.dianx; i++)
{
Shu.dian[i] = rand ()% 10;
if (rand ()% 2 = = 1)
Shu.dian[i] = shu.dian[i] * (-1);
}
for (int i = 1; I <= shu.dianx; i + = y)
{
for (int j = i; J <= i + y-2; j + +)
{
Shu.xian[j][j + 1] = 1;
Shu.xian[j + 1][j] = 1;
}
}
for (int i = 1 + y; i<shu.dianx; i + = y)
{
for (int j = i; J <= i + x-1; j + +)
{
SHU.XIAN[J][J-Y] = 1;
SHU.XIAN[J-Y][J] = 1;
}
}
}
void output (A shu)
{
for (int i = 1; I <= Shu.dianx; i++)
{
cout << Shu.dian[i];
if (shu.xian[i][i + 1] = = 1)
cout << "";
Else
cout << Endl;
}
}
void Bianli (A &shu, int v, int visit[], int &b, int &max, int x)
{
VISIT[V] = 1;
Max + = Shu.dian[v];
if (max >= b)
b = max;
int a = 0, bo = 0;
for (int w = 1; w <= Shu.dianx; w++)
{
for (int c = 1; c <= Shu.dianx; C + +)
{
if ((visit[w] = = 0) && (shu.xian[c][w] = = 1) && (visit[c] = = 1))
{
A = W; Bo = 1; Break
}
}
if (bo = = 1)
Break
}
for (int w = 1; w <= Shu.dianx; w++)
{
for (int c = 1; c <= Shu.dianx; C + +)
{
if ((visit[w] = = 0) && (shu.xian[c][w] = = 1) && (visit[c] = = 1))
{
if (Shu.dian[a]<shu.dian[w])
A = W;
}
}
}
if (b + shu.dian[a]<0)
{
Shu.xian[v][a] = 0;
}
Else
Bianli (Shu, a, visit, B, max, x);
}
int novisit (int visit[], A Shu)
{
int k = 0, I;
for (i = 1; I <= Shu.dianx; i++)
{
if (visit[i] = = 0)
{
K = i;
Break
}
}
return k;
}
int main ()
{
cout << "Please enter the number of rows in the array:" << Endl;
int x, y;
Cin >> x >> y;
A Shu;
Set (Shu, x, y);
Output (SHU);
int v = 1, b[n] = {0}, h = 0;
for (int i = 1; I <= Shu.dianx; i++)
{
if (shu.dian[i]<0)
{
B[i] = Shu.dian[i];
}
Else
{
int Visit[n] = {0};
int max = 0;
Bianli (Shu, I, visit, B[i], Max, x);
}
}
int max = b[1];
for (int i = 2; I <= Shu.dianx; i++)
{
if (B[i]>max)
max = B[i];
}
cout << "Maximum of Unicom sub-arrays:" << max << Endl;
}
Returns the and of a maximum number of interconnected sub-arrays