说在前面
首先,不是真的路由器,只是一些节点,但是使用的是OSPF协议,主要就是建立了一百多个节点,然后形成了一个连通图网络(深搜形成),接着找到最重要的十个节点(邻居最多的),最后输出这十个节点的路由表(老师的要求是路由表要有目的地,下一跳,以及路径长)。具有的功能直接看下面的截图就知道了。
截图
蓝色的就是最重要的十个节点 这是十个路由器的路由表文件
下一跳ip,目的地ip,路径,ip前三个数是乱写的,第四个是节点编号
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"QLCDNumber"
#include"QTextEdit"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
const unsigned int maxdis=1147483640;
struct location{
int x,y;
int ip;
int jiedian[15];
bool life;
bool **LSDB;
location(){
x=y=0;
life=false;
ip=-1;
LSDB = new bool*[200];
for (int i = 0; i < 200; i++){
LSDB[i] = new bool[200];
}
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
LSDB[i][j]=false;
}
}
for(int i=0;i<15;i++){
jiedian[i]=-1;
}
}
};
struct stru
{
int dis;
int xiabiao;
stru(){
dis=xiabiao=maxdis;
}
};
struct stru2{
int dis;
QVector<int> lujing;
stru2(){
dis=maxdis;
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void chushihua();
void liantongtu();
void paintEvent(QPaintEvent*);
void luyoubiao();
int pb1;
int topflag[10];
int tempa=0;
int xiabiaoshu1=-1;
int zongshuluyou=180;
location* luyou;
void top10();
void timerEvent(QTimerEvent *event);
int eventid1,shijian2;
double miaoshu1=0,miaoshu2=0,miaoshu3=0,miaoshu4=0,miaoshu5=0;
void pingjunlujing();
void wangluomidu();
void juleixishu();
bool zengjiajiedian();
bool zengjiabian(int a,int b);
bool liantongxing();
bool itstime=false;
private:
Ui::MainWindow *ui;
void onTimeOut(QLCDNumber * __ptr64 a);
};
#endif
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QTime"
#include"QPainter"
#include<algorithm>
#include <stdio.h>
#include<QVector>
#include<stdlib.h>
#include<QDebug>
#include"QFile"
#include"QDir"
#include"QStack"
#include"QMessageBox"
#include"QTimer"
#include <typeinfo>
#include<cmath>
#include <math.h>
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pb1=0;
zongshuluyou=180;
ui->textEdit->setReadOnly(true);
ui->textEdit->setEnabled(false);
ui->lineEdit_5->setReadOnly(true);
ui->lineEdit_5->setEnabled(false);
ui->lineEdit_6->setReadOnly(true);
ui->lineEdit_6->setEnabled(false);
ui->lineEdit_7->setReadOnly(true);
ui->lineEdit_7->setEnabled(false);
ui->lineEdit_8->setReadOnly(true);
ui->lineEdit_8->setEnabled(false);
setWindowTitle("me");
shijian2=0;
QTimer *pTimer = new QTimer(this);
pTimer->setInterval(1000);
QDir dir("F:/dasanshang/qt/shuxuejianmoFIRST/luyou");
dir.setFilter(QDir::Files);
int fileCount = dir.count();
for (int i = 0; i < fileCount; i++)
dir.remove(dir[i]);
connect(ui->pushButton,&QPushButton::clicked,[&](){
eventid1=startTimer(1000);
QDir dir2("F:/dasanshang/qt/shuxuejianmoFIRST/luyou");
dir2.setFilter(QDir::Files);
int fileCount2 = dir2.count();
for (int i = 0; i < fileCount2; i++)
dir2.remove(dir2[i]);
QString str=ui->lineEdit->text();
int a=0;
pb1=1;
a=str.toInt();
tempa=a;
luyou=new location[zongshuluyou];
for(int i=0;i<zongshuluyou;i++){
luyou[i].ip=i;
}
chushihua();
liantongtu();
top10();
luyoubiao();
pingjunlujing();
wangluomidu();
juleixishu();
ui->lineEdit_7->setText(QString::number(miaoshu1, 'f',3));
ui->lineEdit_5->setText(QString::number(miaoshu3, 'f',3)+"/"+QString::number(miaoshu2, 'f',3));
ui->lineEdit_6->setText(QString::number(miaoshu4, 'f',3));
ui->lineEdit_8->setText(QString::number(miaoshu5, 'f',3));
repaint();
});
connect(ui->pushButton_2,&QPushButton::clicked,[&](){
QString str=ui->lineEdit_2->text();
int a=-1;
a=str.toInt();
if(a>=tempa||a<0){
QMessageBox::critical(NULL, "error", "未找到该节点", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
if(pb1==1){
QDir dir2("F:/dasanshang/qt/shuxuejianmoFIRST/luyou");
dir2.setFilter(QDir::Files);
int fileCount2 = dir2.count();
for (int i = 0; i < fileCount2; i++)
dir2.remove(dir2[i]);
luyou[a].life=false;
for(int i=0;i<tempa;i++){
for(int j=0;j<tempa;j++){
if(luyou[i].life){
luyou[i].LSDB[a][j]=false;
luyou[i].LSDB[j][a]=false;
}
}
}
top10();
luyoubiao();
pingjunlujing();
wangluomidu();
juleixishu();
ui->lineEdit_7->setText(QString::number(miaoshu1, 'f',3));
ui->lineEdit_5->setText(QString::number(miaoshu3, 'f',3)+"/"+QString::number(miaoshu2, 'f',3));
ui->lineEdit_6->setText(QString::number(miaoshu4, 'f',3));
ui->lineEdit_8->setText(QString::number(miaoshu5, 'f',3));
repaint();
}
});
connect(ui->pushButton_3,&QPushButton::clicked,[&](){
QString str=ui->lineEdit_3->text();
QString str2=ui->lineEdit_4->text();
int a=-1,b=-1,c=-1,d=-1;
a=str.toInt();
b=str2.toInt();
qDebug()<<a<<b;
if(b>=tempa||a>=tempa||b<0||a<0||!luyou[a].life||!luyou[b].life){
}
else{
for(int i=0;i<15;i++){
if(luyou[a].jiedian[i]==b){
luyou[a].jiedian[i]=-1;
for(int j=i;j<14;j++){
luyou[a].jiedian[j]=luyou[a].jiedian[j+1];
}
luyou[a].jiedian[14]=-1;
}
if(luyou[b].jiedian[i]==a){
luyou[b].jiedian[i]=-1;
for(int j=i;j<14;j++){
luyou[b].jiedian[j]=luyou[b].jiedian[j+1];
}
luyou[b].jiedian[14]=-1;
}
}
for(int i=0;i<tempa;i++){
luyou[i].LSDB[a][b]=false;
luyou[i].LSDB[b][a]=false;
}
top10();
luyoubiao();
pingjunlujing();
wangluomidu();
juleixishu();
ui->lineEdit_7->setText(QString::number(miaoshu1, 'f',3));
ui->lineEdit_5->setText(QString::number(miaoshu3, 'f',3)+"/"+QString::number(miaoshu2, 'f',3));
ui->lineEdit_6->setText(QString::number(miaoshu4, 'f',3));
ui->lineEdit_8->setText(QString::number(miaoshu5, 'f',3));
repaint();
}
});
connect(ui->pushButton_5,&QPushButton::clicked,[&](){
QString as=ui->lineEdit_11->text();
int asd=as.toInt();
bool panduani=zengjiajiedian();
if(!panduani){
QMessageBox::question(this,"Great","加入节点失败");
}
else{
luyou[tempa-1].ip=asd;
QDir dir2("F:/dasanshang/qt/shuxuejianmoFIRST/luyou");
dir2.setFilter(QDir::Files);
int fileCount2 = dir2.count();
for (int i = 0; i < fileCount2; i++)
dir2.remove(dir2[i]);
top10();
luyoubiao();
pingjunlujing();
wangluomidu();
juleixishu();
repaint();
}
});
connect(ui->pushButton_4,&QPushButton::clicked,[&](){
QString str=ui->lineEdit_9->text();
QString str2=ui->lineEdit_10->text();
int a=-1,b=-1;
a=str.toInt();
b=str2.toInt();
for(int i=0;i<tempa;i++){
if(luyou[i].ip==a)
a=i;
if(luyou[i].ip==b)
b=i;
}
if(a<0||a>tempa||b<0||b>tempa){
QMessageBox::question(this,"sorry","输入错误");
}
else{
bool panduani=zengjiabian(a,b);
if(!panduani){
QMessageBox::question(this,"sorry","无法加入该边,可能是邻接数组已满");
}
else{
QDir dir2("F:/dasanshang/qt/shuxuejianmoFIRST/luyou");
dir2.setFilter(QDir::Files);
int fileCount2 = dir2.count();
for (int i = 0; i < fileCount2; i++)
dir2.remove(dir2[i]);
top10();
luyoubiao();
pingjunlujing();
wangluomidu();
juleixishu();
repaint();
QMessageBox::question(this,"Great","加入成功");
}
}
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent*){
QPainter painter(this);
if(pb1==1){
for(int i=0;i<tempa;i++){
if(luyou[i].life){
QString str=QString::number(i);
QBrush brush(Qt::red);
painter.setBrush(brush);
painter.drawEllipse(luyou[i].x,luyou[i].y,15,15);
painter.setBrush(Qt::NoBrush);
QColor green(0,0, 0);
QPen pen(green,15);
painter.setPen(pen);
int jiejie = 5;
QString saa = QString::number(luyou[i].ip);
painter.drawText(luyou[i].x+7,luyou[i].y+7,saa);
QPen pen2(Qt::black);
painter.setPen(pen2);
for(int j=0;j<15;j++){
if(luyou[i].jiedian[j]>0&&luyou[i].jiedian[j]<tempa&&luyou[luyou[i].jiedian[j]].life&&luyou[i].jiedian[j]!=-1){
painter.drawLine(luyou[i].x,luyou[i].y,luyou[luyou[i].jiedian[j]].x,luyou[luyou[i].jiedian[j]].y);
}
}
}
}
for(int i=0;i<10;i++){
QBrush brush(Qt::blue);
painter.setBrush(brush);
painter.drawEllipse(luyou[topflag[i]].x,luyou[topflag[i]].y,15,15);
painter.setBrush(Qt::NoBrush);
}
}
}
void MainWindow::chushihua(){
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(int i=0;i<tempa;i++){
int x=qrand()%860;
int y=qrand()%860;
for(int j=0;j<i;j++){
int x1,y1;
if(luyou[j].x>=x){
x1=luyou[j].x-x;
}
else{
x1=x-luyou[j].x;
}
if(luyou[j].y>=y){
y1=luyou[j].y-y;
}
else{
y1=y-luyou[j].y;
}
if((x1*x1+y1*y1)<230){
x=qrand()%860;
y=qrand()%860;
j=-1;
}
}
luyou[i].x=x;
luyou[i].y=y;
luyou[i].life=true;
}
}
int cmp( const void *a ,const void *b)
{
return (*(stru *)a).dis > (*(stru *)b).dis ? 1 : -1;
}
void MainWindow::liantongtu(){
int* jilux=new int[tempa];
int* jiluy=new int[tempa];
for(int i1=0;i1<tempa;i1++){
jilux[i1]=luyou[i1].x;
jiluy[i1]=luyou[i1].y;
}
QVector<stru> dis2;
for(int i=0;i<tempa;i++){
for(int j=i+1;j<tempa;j++){
int x1,y1;
if(luyou[i].x>=luyou[j].x){
x1=luyou[i].x-luyou[j].x;
}
else{
x1=luyou[j].x-luyou[i].x;
}
if(luyou[i].y>=luyou[j].y){
y1=luyou[i].y-luyou[j].y;
}
else{
y1=luyou[j].y-luyou[i].y;
}
stru xy3;
xy3.dis=x1*x1+y1*y1;
xy3.xiabiao=j;
dis2.push_back(xy3);
}
stru* dist2=new stru[dis2.size()];
for(int k=0;k<dis2.size();k++){
dist2[k].dis=dis2[k].dis;
dist2[k].xiabiao=dis2[k].xiabiao;
}
if(dis2.size()>0){
qsort(dist2,dis2.size(),sizeof(dist2[0]),cmp);
}
int jishu=0,jishu2=dis2.size();
if(dis2.size()>0){
while(jishu2>0&&jishu<3){
luyou[i].jiedian[jishu]=dist2[jishu].xiabiao;
jishu2--;
jishu++;
}
}
for(int i1=0;i1<tempa;i1++){
if(luyou[i1].x!=jilux[i1])
luyou[i1].x=jilux[i1];
if(luyou[i1].y!=jiluy[i1])
luyou[i1].y=jiluy[i1];
}
delete [] dist2;
dis2.clear();
}
bool* flag=new bool[tempa];
for(int i=0;i<tempa;i++){
flag[i]=false;
}
bool quanlian=false;
QStack<int> bianhao;
bianhao.push_back(0);
flag[0]=true;
bool diyici=false;
int de=0;
while(quanlian==false){
de++;
bool waidian=true;
int waidian2=-1;
while(!bianhao.isEmpty()){
int a=bianhao.top();
bianhao.pop();
if(diyici&&flag[a]==true)
continue;
for(int i=0;i<15;i++){
if(luyou[a].jiedian[i]!=-1&&flag[luyou[a].jiedian[i]]==false&&luyou[a].jiedian[i]>0&&luyou[a].jiedian[i]<tempa){
bianhao.push_back(luyou[a].jiedian[i]);
flag[luyou[a].jiedian[i]]=true;
}
}
diyici=true;
}
for(int i=0;i<tempa;i++){
if(flag[i]==false){
waidian=false;
waidian2=i;
flag[waidian2]=true;
break;
}
}
if(waidian==true||de>tempa){
quanlian=true;
break;
}
if(waidian2!=-1){
stru* dist2=new stru[tempa];
int zuijindian=maxdis,zuijindian2=-1;
for(int i=0;i<tempa;i++){
if(flag[i]){
int x1=abs(jilux[i]-jilux[waidian2]),y1=abs(jiluy[i]-jiluy[waidian2]);
bool weizhi=false;
for(int j=0;j<15;j++){
if(luyou[i].jiedian[j]==-1){
weizhi=true;
}
}
if(weizhi&&i<waidian2&&(x1*x1+y1*y1)<zuijindian){
zuijindian=x1*x1+y1*y1;
zuijindian2=i;
dist2[i].dis=zuijindian;
dist2[i].xiabiao=i;
}
}
}
if(dist2[0].xiabiao!=maxdis){
qsort(dist2,tempa,sizeof(dist2[0]),cmp);
qDebug()<<"sort";
}
for(int i=0;i<tempa;i++){
bool isfangru=false;
if(dist2[i].dis!=maxdis&&dist2[i].xiabiao!=maxdis){
for(int j=0;j<15;j++){
if(luyou[dist2[i].xiabiao].jiedian[j]==-1){
luyou[dist2[i].xiabiao].jiedian[j]=waidian2;
isfangru=true;
break;
}
}
}
if(isfangru){
break;
}
}
bianhao.push_back(waidian2);
flag[waidian2]=true;
delete [] dist2;
}
}
delete [] jilux;
delete [] jiluy;
qDebug()<<"ss";
}
void MainWindow::top10(){
if(tempa<10){
return;
}
for(int i=0;i<10;i++){
topflag[i]=-1;
}
int* flag=new int[tempa];
for(int i=0;i<tempa;i++){
flag[i]=0;
}
for(int i=0;i<tempa;i++){
for(int j=0;j<15;j++){
if(luyou[i].jiedian[j]<tempa&&luyou[i].jiedian[j]>0&&luyou[i].jiedian[j]!=-1&&luyou[i].life&&luyou[luyou[i].jiedian[j]].life){
flag[luyou[i].jiedian[j]]++;
}
}
}
for(int i=0;i<10;i++){
int max=0,loc=0;
for(int j=0;j<tempa;j++){
if(max<flag[j]){
max=flag[j];
loc=j;
}
}
topflag[i]=loc;
flag[loc]=0;
}
delete [] flag;
}
void MainWindow::luyoubiao(){
int **juli = new int*[tempa];
for (int i = 0; i < tempa; i++){
juli[i] = new int[tempa];
}
for(int i=0;i<tempa;i++){
for(int j=0;j<tempa;j++){
juli[i][j]=maxdis;
}
}
for(int i=0;i<tempa;i++){
int j=0;
while(j<15&&luyou[i].jiedian[j]!=-1&&luyou[i].life&&luyou[luyou[i].jiedian[j]].life){
int x1,y1;
if(luyou[i].x>=luyou[luyou[i].jiedian[j]].x){
x1=luyou[i].x-luyou[luyou[i].jiedian[j]].x;
}
else{
x1=luyou[luyou[i].jiedian[j]].x-luyou[i].x;
}
if(luyou[i].y>=luyou[luyou[i].jiedian[j]].y){
y1=luyou[i].y-luyou[luyou[i].jiedian[j]].y;
}
else{
y1=luyou[luyou[i].jiedian[j]].y-luyou[i].y;
}
juli[i][luyou[i].jiedian[j]]=x1*x1+y1*y1;
juli[luyou[i].jiedian[j]][i]=x1*x1+y1*y1;
j++;
}
}
for(int i=0;i<tempa;i++){
for (int j=0;j<tempa;j++){
for(int k=0;k<tempa;k++){
if(luyou[i].life&&juli[j][k]!=maxdis){
luyou[i].LSDB[j][k]=true;
}
}
}
}
for(int i=0;i<10;i++){
int jiedian=topflag[i];
QString doc=QString::number(jiedian);
doc+=".txt";
QFile file("F:/dasanshang/qt/shuxuejianmoFIRST/luyou/"+doc);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write("next ip\t end ip\n");
int u=0;
bool* flag=new bool[tempa];
for(int j=0;j<tempa;j++){
flag[j]=false;
}
stru2 *juli2 = new stru2[tempa];
for(int i2=0;i2<tempa;i2++){
juli2[i2].dis=maxdis;
if(juli2[i2].dis>juli[jiedian][i2]){
juli2[i2].dis=juli[jiedian][i2];
}
juli2[i2].lujing.push_back(jiedian);
juli2[i2].lujing.push_back(i2);
}
flag[jiedian]=true;
for(int k=0;k<tempa-1;k++){
int min1=maxdis;
for(int k2=0;k2<tempa;k2++){
if (flag[k2] == false && juli2[k2].dis < min1){
min1 = juli2[k2].dis;
u = k2;
}
}
flag[u]=true;
int mudidi=0;
for(int iii=0;iii<tempa;iii++){
if(luyou[iii].ip==u)
mudidi=luyou[iii].ip;
}
QTime time;
time= QTime::currentTime();
qsrand(time.msec()+time.second()*1000);
int nn = qrand() % 500;
QString name="ip:161.253.188."+QString::number(luyou[juli2[u].lujing[1]].ip)+"\t "+"\tip:161.253.188."+QString::number(luyou[juli2[u].lujing[juli2[u].lujing.size()-1]].ip)+"\t "+QString::number(juli[juli2[u].lujing[1]][juli2[u].lujing[0]]);
QString name2="";
file.write(name.toUtf8()+'\n');
for(int l3=0;l3<juli2[u].lujing.size();l3++){
QString name3=QString::number(luyou[juli2[u].lujing[l3]].ip)+" ";
name2+=name3;
if(l3==0)
break;
}
for(int v=0;v<tempa;v++){
if(juli[u][v]<maxdis){
if(juli2[v].dis>juli2[u].dis+juli[u][v]){
juli2[v].dis=juli2[u].dis+juli[u][v];
juli2[v].lujing.clear();
for(int l2=0;l2<juli2[u].lujing.size();l2++){
juli2[v].lujing.push_back(juli2[u].lujing[l2]);
}
juli2[v].lujing.push_back(v);
}
}
}
}
file.close();
delete [] flag;
delete [] juli2;
}
delete [] juli;
}
void MainWindow::timerEvent(QTimerEvent *event){
shijian2++;
if(shijian2==90){
shijian2=0;
if(itstime){
luyoubiao();
}
}
QString a=QString::number(shijian2);
ui->textEdit->setText(a);
}
void MainWindow::pingjunlujing(){
int **juli = new int*[tempa];
for (int i = 0; i < tempa; i++){
juli[i] = new int[tempa];
}
for(int i=0;i<tempa;i++){
for(int j=0;j<tempa;j++){
juli[i][j]=maxdis;
}
}
for(int i=0;i<tempa;i++){
int j=0;
while(j<15&&luyou[i].jiedian[j]!=-1&&luyou[i].life&&luyou[luyou[i].jiedian[j]].life&&luyou[i].jiedian[j]<tempa&&luyou[i].jiedian[j]>0){
int x1,y1;
if(luyou[i].x>=luyou[luyou[i].jiedian[j]].x){
x1=luyou[i].x-luyou[luyou[i].jiedian[j]].x;
}
else{
x1=luyou[luyou[i].jiedian[j]].x-luyou[i].x;
}
if(luyou[i].y>=luyou[luyou[i].jiedian[j]].y){
y1=luyou[i].y-luyou[luyou[i].jiedian[j]].y;
}
else{
y1=luyou[luyou[i].jiedian[j]].y-luyou[i].y;
}
juli[i][luyou[i].jiedian[j]]=x1*x1+y1*y1;
juli[luyou[i].jiedian[j]][i]=x1*x1+y1*y1;
j++;
}
}
double alldist=0;
for(int i=0;i<tempa;i++){
for(int j=0;j<tempa;j++){
if(juli[i][j]!=maxdis&&juli[i][j]>0){
alldist+=(double)(sqrt(juli[i][j])*((double)(1/(0.5*tempa*(tempa-1)))));
}
}
}
qDebug()<<alldist*7;
miaoshu1=alldist*7;
miaoshu5=miaoshu1*tempa;
delete [] juli;
}
void MainWindow::wangluomidu(){
int alledges=0,alledges2=0;
char **flag = new char*[tempa];
for (int i = 0; i < tempa; i++){
flag[i] = new char[tempa];
}
for(int i=0;i<tempa;i++){
for(int j=0;j<tempa;j++){
flag[i][j]=false;
}
}
for(int i=0;i<tempa;i++){
alledges+=i;
}
for(int i=0;i<tempa;i++){
for(int j=0;j<15;j++){
if(flag[i][j]==false&&luyou[i].jiedian[j]!=-1&&luyou[i].jiedian[j]<tempa&&luyou[i].life&&luyou[i].jiedian[j]>0){
alledges2++;
flag[i][j]=true;
flag[j][i]=true;
}
}
}
qDebug()<<alledges<<alledges2;
miaoshu2=alledges;
miaoshu3=alledges2;
}
void MainWindow::juleixishu(){
double pingjunzhi=0;
for(int i=0;i<tempa;i++){
int lingju[15];
int lingjushu=0,lingjushu2=0;
for(int k=0;k<15;k++){
lingju[k]=-1;
}
for(int j=0;j<15;j++){
if(luyou[i].life&&luyou[i].jiedian[j]>0&&luyou[i].jiedian[j]<tempa&&luyou[luyou[i].jiedian[j]].life){
lingju[j]=luyou[i].jiedian[j];
lingjushu++;
}
}
for(int j=0;j<15;j++){
if(lingju[j]!=-1){
for(int h=0;h<15;h++){
for(int l=0;l<15;l++){
if(luyou[lingju[j]].jiedian[h]>0&&luyou[lingju[j]].jiedian[h]<tempa&&luyou[lingju[j]].jiedian[h]==lingju[l]&&luyou[luyou[lingju[j]].jiedian[h]].life){
lingjushu2++;
}
}
}
}
}
if(lingjushu<=0||lingjushu2<=0){
pingjunzhi+=0;
}
else{
pingjunzhi+=2*lingjushu2/(lingjushu*(lingjushu-1));
}
}
qDebug()<<pingjunzhi;
miaoshu4=pingjunzhi;
}
bool MainWindow::zengjiajiedian(){
luyou[tempa].life=true;
if(tempa+1>=180)
return false;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
int x=qrand()%860;
int y=qrand()%860;
for(int j=0;j<tempa;j++){
int x1,y1;
if(luyou[j].x>=x){
x1=luyou[j].x-x;
}
else{
x1=x-luyou[j].x;
}
if(luyou[j].y>=y){
y1=luyou[j].y-y;
}
else{
y1=y-luyou[j].y;
}
if((x1*x1+y1*y1)<230){
x=qrand()%860;
y=qrand()%860;
j=-1;
}
}
luyou[tempa].x=x;
luyou[tempa].y=y;
luyou[tempa].life=true;
tempa++;
stru* dist2=new stru[tempa-1];
for(int i=0;i<tempa-1;i++){
int x1=abs(luyou[i].x-luyou[tempa-1].x),y1=abs(luyou[i].y-luyou[tempa-1].y);
dist2[i].dis=x1*x1+y1*y1;
dist2[i].xiabiao=i;
}
if(dist2[0].xiabiao!=maxdis){
qsort(dist2,tempa-1,sizeof(dist2[0]),cmp);
}
for(int i=0;i<tempa-1;i++){
bool isfangru=false;
if(dist2[i].dis!=maxdis&&dist2[i].xiabiao!=maxdis){
for(int j=0;j<15;j++){
if(luyou[dist2[i].xiabiao].jiedian[j]==-1){
luyou[dist2[i].xiabiao].jiedian[j]=tempa-1;
isfangru=true;
break;
}
}
}
if(isfangru){
break;
}
}
delete [] dist2;
return true;
}
bool MainWindow::zengjiabian(int a,int b){
if(a==b||luyou[a].life==false||luyou[b].life==false){
return false;
}
for(int i=0;i<tempa;i++){
if(luyou[i].ip==a)
a=i;
if(luyou[i].ip==b)
b=i;
qDebug()<<a<<b;
}
bool kong1=false,kong2=false;
int xiabiao1=-1,xiabiao2=-1;
for(int i=0;i<15;i++){
if(luyou[a].jiedian[i]==-1&&xiabiao1==-1){
kong1=true;
xiabiao1=i;
}
if(luyou[b].jiedian[i]==-1&&xiabiao2==-1){
kong2=true;
xiabiao2=i;
}
}
if(!kong1||!kong2){
return false;
}
else{
if(a<b&&kong1){
luyou[a].jiedian[xiabiao1]=b;
}
else{
luyou[b].jiedian[xiabiao2]=a;
}
}
return true;
}
bool MainWindow::liantongxing(){
bool* flag=new bool[tempa];
for(int i=0;i<tempa;i++){
flag[i]=false;
}
bool quanlian=false;
QStack<int> bianhao;
bianhao.push_back(0);
flag[0]=true;
bool diyici=false;
int de=0;
bool jieguo=true;
while(quanlian==false){
de++;
while(!bianhao.isEmpty()){
int a=bianhao.top();
bianhao.pop();
if(diyici&&flag[a]==true)
continue;
for(int i=0;i<15;i++){
if(luyou[a].jiedian[i]!=-1&&flag[luyou[a].jiedian[i]]==false&&luyou[a].jiedian[i]>0&&luyou[a].jiedian[i]<tempa){
bianhao.push_back(luyou[a].jiedian[i]);
flag[luyou[a].jiedian[i]]=true;
}
}
diyici=true;
}
for(int i=0;i<tempa;i++){
if(flag[i]==false){
jieguo=false;
break;
}
}
break;
}
return jieguo;
}
|