⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointview.cpp

📁 中国所有大中城市的TSP问题实现。图形演示。采用最近邻法则
💻 CPP
字号:
// pointView.cpp : implementation of the CPointView class
//

#include "stdafx.h"
#include "point.h"

#include "pointDoc.h"
#include "pointView.h"
#include "iostream.h"
#include "math.h"
#include "stdio.h"
#include "conio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPointView

IMPLEMENT_DYNCREATE(CPointView, CView)

BEGIN_MESSAGE_MAP(CPointView, CView)
	//{{AFX_MSG_MAP(CPointView)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_Run, OnRun)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPointView construction/destruction
Stsp::init()
{
   CT[0].x=3639;  CT[0].y=1315;
	CT[1].x=4177;  CT[1].y=2244;
	CT[2].x=3712;  CT[2].y=1399;
	CT[3].x=3569;  CT[3].y=1438;
	CT[4].x=3757;  CT[4].y=1187;
	CT[5].x=3493;  CT[5].y=1696;
	CT[6].x=3904;  CT[6].y=1289;
	CT[7].x=3488;  CT[7].y=1535;
	CT[8].x=3791;  CT[8].y=1339;
	CT[9].x=3506;  CT[9].y=1221;
	CT[10].x=3374;  CT[10].y=1750;
	CT[11].x=3376;  CT[11].y=1306;
	CT[12].x=3237;  CT[12].y=1764;
	CT[13].x=3326;  CT[13].y=1556;
	CT[14].x=3188;  CT[14].y=1881;
	CT[15].x=3089;  CT[15].y=1251;
	CT[16].x=3258;  CT[16].y=911;
	CT[17].x=3814;  CT[17].y=261;
	CT[18].x=3238;  CT[18].y=1229;
	CT[19].x=3646;  CT[19].y=234;
	CT[20].x=3583;  CT[20].y=864;
	CT[21].x=4172;  CT[21].y=1125;
	CT[22].x=4089;  CT[22].y=1387;
	CT[23].x=4297;  CT[23].y=1218;
	CT[24].x=4020;  CT[24].y=1142;
	CT[25].x=4196;  CT[25].y=1044;
	CT[26].x=4116;  CT[26].y=1187;
	CT[27].x=4095;  CT[27].y=626;
	CT[28].x=4312;  CT[28].y=790;
	CT[29].x=4252;  CT[29].y=882;
	CT[30].x=4403;  CT[30].y=1022;
	CT[31].x=4685;  CT[31].y=830;
	CT[32].x=4386;  CT[32].y=570;
	CT[33].x=4361;  CT[33].y=73;
	CT[34].x=4720;  CT[34].y=557;
	CT[35].x=4643;  CT[35].y=404;
	CT[36].x=4634;  CT[36].y=654;
	CT[37].x=4153;  CT[37].y=426;
	CT[38].x=4784;  CT[38].y=270;
	CT[39].x=2846;  CT[39].y=1951;
	CT[40].x=2831;  CT[40].y=2099;
	CT[41].x=3007;  CT[41].y=1970;
	CT[42].x=3054;  CT[42].y=1710;
	CT[43].x=3086;  CT[43].y=1516;
	CT[44].x=1828;  CT[44].y=1210;
	CT[45].x=2562;  CT[45].y=1756;
	CT[46].x=2716;  CT[46].y=1924;
	CT[47].x=2061;  CT[47].y=1277;
	CT[48].x=2291;  CT[48].y=1403;
	CT[49].x=2751;  CT[49].y=1559;
	CT[50].x=2788;  CT[50].y=1491;
	CT[51].x=2012;  CT[51].y=1552;
	CT[52].x=1779;  CT[52].y=1626;
	CT[53].x=2381;  CT[53].y=1676;
	CT[54].x=682;   CT[54].y=825;
	CT[55].x=1478;  CT[55].y=267;
	CT[56].x=1777;  CT[56].y=892;
	CT[57].x=518;   CT[57].y=1251;
	CT[58].x=278;   CT[58].y=890;
	CT[59].x=1064;  CT[59].y=284;
	CT[60].x=1332;  CT[60].y=695;
	CT[61].x=3715;  CT[61].y=1678;
	CT[62].x=3688;  CT[62].y=1818;
	CT[63].x=4016;  CT[63].y=1715;
	CT[64].x=4181;  CT[64].y=1574;
	CT[65].x=3896;  CT[65].y=1656;
	CT[66].x=4087;  CT[66].y=1546;
	CT[67].x=3929;  CT[67].y=1692;
	CT[68].x=3918;  CT[68].y=2179;
	CT[69].x=4062;  CT[69].y=2220;
	CT[70].x=3751;  CT[70].y=1945;
	CT[71].x=3972;  CT[71].y=2136;
	CT[72].x=4061;  CT[72].y=2370;
	CT[73].x=4207;  CT[73].y=2533;
	CT[74].x=4029;  CT[74].y=2498;
	CT[75].x=4201;  CT[75].y=2397;
	CT[76].x=4139;  CT[76].y=2615;
	CT[77].x=3766;  CT[77].y=2364;
	CT[78].x=3777;  CT[78].y=2095;
	CT[79].x=3780;  CT[79].y=2212;
	CT[80].x=3896;  CT[80].y=2443;
	CT[81].x=3888;  CT[81].y=2261;
	CT[82].x=3594;  CT[82].y=2900;
	CT[83].x=3796;  CT[83].y=2499;
	CT[84].x=3678;  CT[84].y=2463;
	CT[85].x=3676;  CT[85].y=2578;
	CT[86].x=3478;  CT[86].y=2705;
	CT[87].x=3789;  CT[87].y=2620;
	CT[88].x=4029;  CT[88].y=2838;
	CT[89].x=3810;  CT[89].y=2969;
	CT[90].x=3862;  CT[90].y=2839;
	CT[91].x=3928;  CT[91].y=3029;
	CT[92].x=4167;  CT[92].y=3206;
	CT[93].x=4263;  CT[93].y=2931;
	CT[94].x=4186;  CT[94].y=3037;
	CT[95].x=3486;  CT[95].y=1755;
	CT[96].x=3492;  CT[96].y=1901;
	CT[97].x=3322;  CT[97].y=1916;
	CT[98].x=3334;  CT[98].y=2107;
	CT[99].x=3479;  CT[99].y=2198;
	CT[100].x=3429; CT[100].y=1908;
	CT[101].x=3587; CT[101].y=2417;
	CT[102].x=3318; CT[102].y=2408;
	CT[103].x=3176; CT[103].y=2150;
	CT[104].x=3507; CT[104].y=2376;
	CT[105].x=3296; CT[105].y=2217;
	CT[106].x=3229; CT[106].y=2367;
	CT[107].x=3264; CT[107].y=2551;
	CT[108].x=3394; CT[108].y=2643;
	CT[109].x=3402; CT[109].y=2912;
	CT[110].x=3360; CT[110].y=2792;
	CT[111].x=3101; CT[111].y=2721;
	CT[112].x=3402; CT[112].y=2510;
	CT[113].x=3439; CT[113].y=3201;
	CT[114].x=3792; CT[114].y=3156;
	CT[115].x=3468; CT[115].y=3018;
	CT[116].x=3526; CT[116].y=3263;
	CT[117].x=3142; CT[117].y=3421;
	CT[118].x=3356; CT[118].y=3212;
	CT[119].x=3012; CT[119].y=3394;
	CT[120].x=3130; CT[120].y=2973;
	CT[121].x=3044; CT[121].y=3081;
	CT[122].x=2935; CT[122].y=3240;
	CT[123].x=2765; CT[123].y=3321;
	CT[124].x=3140; CT[124].y=3558;
	CT[125].x=3053; CT[125].y=3739;
	CT[126].x=2545; CT[126].y=2357;
	CT[127].x=2769; CT[127].y=2492;
	CT[128].x=2284; CT[128].y=2803;
	CT[129].x=2611; CT[129].y=2275;
	CT[130].x=2348; CT[130].y=2652;
	CT[131].x=2577; CT[131].y=2574;
	CT[132].x=2860; CT[132].y=2862;
	CT[133].x=2778; CT[133].y=2826;
	CT[134].x=2592; CT[134].y=2820;
	CT[135].x=2801; CT[135].y=2700;
	CT[136].x=2126; CT[136].y=2896;
	CT[137].x=2401; CT[137].y=3164;
	CT[138].x=2370; CT[138].y=2975;
	CT[139].x=1890; CT[139].y=3033;
	CT[140].x=1304; CT[140].y=2312;
	CT[141].x=1084; CT[141].y=2313;
	CT[142].x=3538; CT[142].y=3298;
	CT[143].x=3470; CT[143].y=3304;
		for(int i=0;i<144;i++)
	   CT[i].init=i;
		CT[144]=CT[0];
	for(i=0;i<144;i++)
	{
		for(int j=0;j<144;j++)
		{
			dis[i][j]=(int)sqrt((CT[i].x-CT[j].x)*(CT[i].x-CT[j].x)+(CT[i].y-CT[j].y)*(CT[i].y-CT[j].y));
		}
	}
	return 0;
}

Stsp::sort()
{
	M=10295;
	
	int i=0,j=0;
    for(int m=0;m<144;m++)
	{
		for(int n=0;n<m;n++)
		{
			
				dissort[i].x1=CT[m].x;
				dissort[i].x2=CT[n].x;
				dissort[i].y1=CT[m].y;
				dissort[i].y2=CT[n].y;
				dissort[i].length=dis[m][n];
				dissort[i].u=CT[m].init;
				dissort[i].v=CT[n].init;
				i++;
		}

	}

	for(j=0;j<10295;j++)
	{
		for(i=0;i<M;i++)
		{
			if(dissort[i].length>dissort[i+1].length)
			{
				temp.length=dissort[i].length;
				dissort[i].length=dissort[i+1].length;
				dissort[i+1].length=temp.length;
				temp.u=dissort[i].u;
				dissort[i].u=dissort[i+1].u;
				dissort[i+1].u=temp.u;
				temp.v=dissort[i].v;
				dissort[i].v=dissort[i+1].v;
				dissort[i+1].v=temp.v;
			}
		
		}
		M--;
	}
return 0;
}
Stsp:: tsp()
{

     int m=0;
	
    int cycle=0;
	for(int i=0;i<144;i++)
	{
		E[i].x=145;
		E[i].y=145;
		E[i].sign=0;
		C[i]=145;
		T[i]=0;
	}
	
         int vertex=0;
		for(i=0;i<10296;i++)
		{
		/*	for(j=0;j<=vertex;j++)
			{
				if(C[j]==dissort[i].u)
				{
					sameu++;
				}
				if(C[j]==dissort[i].v)
				{
					samev++;
				}
			}*/
			if((T[dissort[i].u]==0)&&(T[dissort[i].v]==0))
			{
				E[m].x=dissort[i].u;
				E[m].y=dissort[i].v;
				C[vertex++]=dissort[i].u;
				C[vertex++]=dissort[i].v;
				T[dissort[i].u]++;
				length=length+dissort[i].length;
				//cout<<"dissort[i].u="<<dissort[i].u<<endl;
				//cout<<"T[dissort[i].u]="<<T[dissort[i].u]<<endl;
				T[dissort[i].v]++;
				
				m++;
                
			}
			 else if((T[dissort[i].u]==0)&&(T[dissort[i].v]==1))
			{
				E[m].x=dissort[i].u;
				E[m].y=dissort[i].v;
                C[vertex++]=dissort[i].u;
                T[dissort[i].u]++;
                 T[dissort[i].v]++;
				
				length=length+dissort[i].length;
				m++;
				
			}
			 else if((T[dissort[i].u]==1)&&(T[dissort[i].v]==0))
			{
				E[m].x=dissort[i].u;
				E[m].y=dissort[i].v;
				C[vertex++]=dissort[i].v;
				T[dissort[i].u]++;
				T[dissort[i].v]++;
			
				length=length+dissort[i].length;
				m++;
			
			}
			 else if((T[dissort[i].u]==1)&&(T[dissort[i].v]==1))
			 {
				 E[m].x=dissort[i].u;
				 E[m].y=dissort[i].v;
				 for(int j=0;j<m+1;j++)
				 {
					 if((E[j].x==E[m].x)&&E[j].sign==0)
					 {
						 E[j].sign=1;
						 if(E[j].y==E[m].y)
					 { 
						 cycle=1;
						 break;
					 }
					 else
					 {
						E[m].x=E[j].y;
						 j=0;
						 continue;
					 }
					 }
					 if((E[j].y==E[m].x)&&(E[j].sign==0))
					 {
						 E[j].sign=1;
						 if(E[j].x==E[m].y)
					 {
						 cycle=1;
						 break;
					 }
					 else
					 {
						 E[m].x=E[j].x;
						 j=0;
						continue;
					 }
					 }
				 }
				 if(cycle)
					 cycle=0;
				 else
				 {
                    E[m].x=dissort[i].u;
				    E[m].y=dissort[i].v;
					T[dissort[i].u]++;
				    T[dissort[i].v]++;
				
	              
					 m++;
				 }

			 } 
			 for(int s=0;s<m;s++)
				 E[s].sign=0;

			 

		}
		for(i=0;i<144;i++)
		{
			if(T[E[i].y]==1)
			{
				E[143].x=E[i].y;
			    T[E[i].y]++;
				
				break;
			}
			
		}
		for(i=0;i<144;i++)
		{
			if(T[E[i].y]==1)
			{
				E[143].y=E[i].y;
			    T[E[i].y]++;
				break;
			}
			
		}
		
	return 0;
}          
CPointView::CPointView()
{
	// TODO: add construction code here

}

CPointView::~CPointView()
{
}

BOOL CPointView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPointView drawing

void CPointView::OnDraw(CDC* pDC)
{
	
init();
		

	CPointDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	/*CString str;
	str.Format("%d",E[0].x);
	MessageBox(str);*/
	CBrush newbrush,*pOldBrush;
	newbrush.CreateSolidBrush(RGB(255,0,0));
	pOldBrush=pDC->SelectObject(&newbrush);

	for(int i=0;i<144;i++)
	{
	//	CRect rcView; 
      // GetClientRect (rcView); 
		pDC->SelectStockObject (WHITE_PEN);
        
		pDC->Ellipse(CT[i].x/8-3,CT[i].y/8-3,CT[i].x/8+3,CT[i].y/8+3);
	}
	pDC->SelectObject(pOldBrush);
	if(tsped)
	{
		CPen newpen,*pOldpen;
	newpen.CreatePen(PS_SOLID,1,RGB(255,0,0));
	pOldpen=pDC->SelectObject(&newpen);
		for(int s=0;s<144;s++)
		{
			
			pDC->MoveTo(CT[E[s].x].x/8,CT[E[s].x].y/8);
			pDC->LineTo(CT[E[s].y].x/8,CT[E[s].y].y/8);
			
			_sleep(100);
			
			
		}
		
			pDC->SelectObject(pOldpen);
		
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CPointView printing

BOOL CPointView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPointView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPointView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPointView diagnostics

#ifdef _DEBUG
void CPointView::AssertValid() const
{
	CView::AssertValid();
}

void CPointView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPointDoc* CPointView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPointDoc)));
	return (CPointDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPointView message handlers

int CPointView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
		
	// TODO: Add your specialized creation code here
tsped=FALSE;
		
	return 0;
}

void CPointView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

		
	CView::OnLButtonDown(nFlags, point);
}

void CPointView::OnRun() 
{
	// TODO: Add your command handler code here
	sort();
		tsp();
//sort();
//tsp();
tsped=TRUE;
Invalidate();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -