标签:
#include<iostream>
#define N 100
#include<ctime>
using namespace std;
typedef struct
{
int d[N];
int a[N][N];
int x;
}A;
void set(A &shu, int x, int y)//x,y分别是行数和列数
{
shu.x = x*y;
srand((unsigned)time(NULL));
for(int i = 1; i <= shu.x; i++)
{
shu.d[i] = rand() % 10;
if(rand() % 2== 1)
shu.d[i] = shu.d[i] * (-1);
}//随机生成数组的数
for(int i = 1; i <= shu.x; i +=y)
{
for(int j = i; j <= i + y - 2; j++)
{
shu.a[j][j+1] = 1;
shu.a[j+1][j] = 1;
}
} for(int i = 1+ y;i<shu.x;i+=y)
{
for(int j = i; j <= i + x - 1; j++)
{
shu.a[j][j-y] = 1;
shu.a[j-y][j] = 1;
}
}//将随机生成的一维数组转换成二维的图的形式
}
void output(A shu)
{
for(int i = 1; i <= shu.x; i++)
{
cout <<shu.d[i] ;
if(shu.a[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.d[v];
if(max>=b)
b =max;
int a = 0, bo = 0;
for(int w = 1; w <= shu.x; w++)
{
for(int c = 1; c <= shu.x; c++)
{
if((visit[w] == 0) && (shu.a[c][w] == 1) && (visit[c] == 1))
{
a = w;
bo = 1;
break;
}
}
if(bo == 1) break;
}
for(int w = 1; w <= shu.x; w++)
{
for(int c = 1; c <= shu.x; c++)
{
if((visit[w] == 0) && (shu.a[c][w] == 1) && (visit[c] == 1))
{
if(shu.d[a]<shu.d[w])
a=w;
}
}
}
if(b+shu.d[a]<0)
{
shu.a[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.x; i++)
{
if(visit[i] == 0)
{
k =i;
break;
}
} return k;
}//判断图中没有visit的项
int main()
{
cout << "Please input the row and the col:"<<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.x; i++)
{
if(shu.d[i]<0)
{
b[i] =shu.d[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.x; i++)
{
if(b[i]>max) max =b[i];
}
cout << "The max array is:"<< max <<endl;
}
标签:
原文地址:http://www.cnblogs.com/seven-seven/p/5360208.html