using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 蛮力法求解旅行商
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int vn;//顶点个数
int pathlong = 0;//初始化路径长度
int[,] matrix;//用来存放邻接矩阵
int min = 9999;//初始化最短路径长度
private void button1_Click(object sender, EventArgs e)
{
min = 9999;//初始化最短路径长度
string mx;//用来存放读取文件的信息
pathlong = 0;//初始化路径长度
string Chosen_File = "";
openFD.InitialDirectory = "C:";
openFD.Title = "Open a Text File";
openFD.FileName = "";
if (openFD.ShowDialog() != DialogResult.Cancel)//读取文件
{
Chosen_File = openFD.FileName;
System.IO.StreamReader objReader;
objReader = new System.IO.StreamReader(Chosen_File);
vn = Convert.ToInt32(objReader.ReadLine());//文本的第一行为矩阵大小
mx = objReader.ReadToEnd();//读取矩阵信息
Amatrix.Clear();//清空显示邻接矩阵的文本框
listBox1.Items.Clear();//清空记录
listBox2.Items.Clear();
string[] temp;//把mx的字符串转为矩阵信息
temp = mx.Split(' ');//读取矩阵信息
int k = 0;
matrix = new int[vn, vn]; //初始化邻接矩阵
for (int i = 0; i < vn; i++)
for (int j = 0; j < vn; j++)
{
matrix[i, j] = Convert.ToInt32((temp[k]));//写入邻接矩阵
k++;
}
Amatrix.Font = new Font("Verdana", 77 / vn, FontStyle.Regular);
print(matrix);
int[] a = new int[vn-1]; //初始化矩阵a
for (int i = 0; i < vn - 1; i++)//为数组a赋值从1到n的数字
a[i] = i + 1;
Permutation(a);//进行全排列
}
}
private void print(int[,] matrix)//打印邻接矩阵
{
for (int i = 0; i < vn; i++) //输出邻接矩阵
{
for (int j = 0; j < vn; j++)
Amatrix.Text += matrix[i, j] + " ";
Amatrix.Text += Environment.NewLine;
}
}
public void Permutation(int[] a)
{
while (true)
{
printArray(a);//输出当前排列
int i, j;
for (i = a.Length - 2; i >= 0; i--)//从最后第二个元素开始向前,如果后面的元素大于当前i所指向的元素则记录i的下标
{
if (a[i] < a[i + 1])//如果找到i跳出循环
break;
else if (i == 0)//说明是最大逆序数,排列完毕退出函数
return;
}
for (j = a.Length - 1; j > i; j--)//从后向前,记录比i大的第一个数
{
if (a[j] > a[i])//找到j跳出
break;
}
swap(a, i, j);//交换i,j
reverse(a, i + 1, a.Length - 1);//对当前的子串a[i+1]到a[a.length-1]进行反转
}
}
public void swap(int[] a, int i, int j)//交互数组元素
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public void reverse(int[] a, int i, int j)//将字符串进行反转
{
while (i < j)
swap(a, i++, j--);
}
public void printArray(int[] a) //输出排列结果
{
string per = "0=>"; //初始化路径信息
for (int i = 0; i < a.Length; i++)
{
per += a[i] + "=>"; //写入路径信息
}
per += "0"; //加入返回路径
listBox1.Items.Add(per + " 长度:" + citylength(a)); // 把per列入listBox1
if (citylength(a) < min) //计算最短路径,过滤所有路径筛选出最短路径
{
min = citylength(a); //发现更短路径就更新min
listBox2.Items.Clear(); //之前的路径清空
listBox2.Items.Add("最短路径为:" + per + " 长度:" + min);//重新列入当前最短路径
}
else if (citylength(a) == min) //如果相同则列入
listBox2.Items.Add("最短路径为:"+per + " 长度:" + min);
}
private int citylength(int[]city)//计算数组并返回路径长度
{
pathlong = 0;
for (int i = 0; i < city.Length - 1; i++)//对于数组中每一个元素,计算i到i+1的距离
pathlong += matrix[city[i], city[i + 1]];
pathlong += matrix[0, city[0]];//加上返回路径的长度
pathlong += matrix[city[city.Length - 1],0];//加上返回路径的长度
return pathlong;
}
}
}
蛮力法求解旅行商
最新推荐文章于 2024-02-08 14:40:31 发布