具有数组的城市之间的搜索算法 [英] Search algorithm between cities with an array

查看:63
本文介绍了具有数组的城市之间的搜索算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,需要搜索算法的帮助,我创建了一个Windows窗体应用程序,您可以从组合框中选择一个城市,该程序必须遍历所有城市,而无需重复同一城市两次,它应该选择最短的路线.
然后,它应该在列表框中显示最短路线和总距离.

到目前为止,我有以下代码,请帮助.

button1将初始化搜索.

Hi, I''m new to C# and need help with a search algorithm, I have created a windows form application where you select a city from a combobox, this program has to go through all the cities without repeating the same city twice and it should select the shortest route.
It should then display the shortest routes and the total distances on a listbox.

I have the following code so far, please, I need help.

The button1 is what will initialize the search.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CycleTour
{
    public partial class Form1 : Form
    {
        //Adding the list of arrays to be used
        string[] ArrayCities = new string[7];
        int[] arrDistance1 = new int[7];
        int[] arrDistance2 = new int[6];
        int[] arrDistance3 = new int[5];
        int[] arrDistance4 = new int[5];
        int[] arrDistance5 = new int[3];
        int[] arrDistance6 = new int[2];
        int[] arrDistance7 = new int[1];
        int[] arrDistance8 = new int[1];
        int[] arrBestDis = new int[6];

        //Variables declared
        int counter1 = 0;
        int counter2 = 0;
        int counter3 = 0;
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0;
        int input = 0;
        int total = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //List cities index
            ArrayCities[0] = "Aberdeen";
            ArrayCities[1] = "Ayr";
            ArrayCities[2] = "Fort William";
            ArrayCities[3] = "Glasgow";
            ArrayCities[4] = "Inverness";
            ArrayCities[5] = "St Andrews";
            ArrayCities[6] = "Stirling";
            //Add the cities to the combobox
            comboBox1.Items.AddRange(ArrayCities);
            listBox1.Items.AddRange(ArrayCities);

            //Distances
            arrDistance1[0] = 179; //Aberdeen to Ayr
            arrDistance1[1] = 129; //Aberdeen to Edinburgh
            arrDistance1[2] = 157; //Aberdeen to Fort William
            arrDistance1[3] = 146; //Aberdeen to Glasgow
            arrDistance1[4] = 105; //Aberdeen to Inverness
            arrDistance1[5] = 79;  //Aberdeen to St Andrews
            arrDistance1[6] = 119; //Aberdeen to Stirling

            arrDistance2[0] = 79;  //Ayr to Edinburgh
            arrDistance2[1] = 141; //Ayr to Fort William
            arrDistance2[2] = 33;  //Ayr to Glasgow
            arrDistance2[3] = 207; //Ayr to Inverness
            arrDistance2[4] = 118; //Ayr to St Andrews
            arrDistance2[5] = 64;  //Ayr to Stirling

            arrDistance3[0] = 131; //Edinburgh to Fort William
            arrDistance3[1] = 43;  //Edinburgh to Glasgow
            arrDistance3[2] = 154; //Edinburgh to Inverness
            arrDistance3[3] = 50;  //Edinburgh to St Andrews
            arrDistance3[4] = 36;  //Edinburgh to Stirling

            arrDistance4[0] = 116; //Fort William to Glasgow
            arrDistance4[1] = 64;  //Fort William to Inverness, roadworks
            arrDistance4[2] = 74;  //Fort William to Inverness
            arrDistance4[3] = 134; //Fort William to St Andrews
            arrDistance4[4] = 96;  //Fort William to Stirling

            arrDistance5[0] = 175; //Glasgow to Inverness
            arrDistance5[1] = 81;  //Glasgow to St Andrews
            arrDistance5[2] = 27;  //Glasgow to Stirling

            arrDistance6[0] = 145; //Inverness to St Andrews
            arrDistance6[1] = 143; //Inverness to Stirling

            arrDistance7[0] = 52;  //St Andrews to Stirling

            arrDistance8[0] = 0;   //Stirling
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)//== ArrayCities.ToString())
            {
                while(counter1 <= 7)

                if (arrDistance1[counter1] < arrDistance2[counter1])
                {

                }
                else
                {
                    counter1 = counter1 + 1;
                }

            }
            else
            {
                MessageBox.Show("Please select a city", "Select city", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }



        private void button3_Click(object sender, EventArgs e)
        {
            //Creates a MessageBox confirming an exit
            DialogResult result = MessageBox.Show("Would you like to exit the program?", "Continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            //if Yes is selected the program will close
            if (result == DialogResult.Yes)
            {
                DialogResult = MessageBox.Show("Thank you for using the program", "Goodbye!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Application.Exit();
            }

            //if No is selected the program will remain running
            if (result == DialogResult.No)
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = -1;
            comboBox1.SelectedText = "";
            label6.Text = "Cities";
            label9.Text = "0000";
        }



    }
}

推荐答案

在您进一步进行操作之前,我可以建议一些更改吗?

首先,不要对任何东西使用Visual Studio默认名称!当您拥有一个充满button1,button2,button3,textBox1,textBox2等的应用程序时,很难确定正在发生的事情.如果改用描述性名称,那么您的代码既易于阅读,又(通常)更可靠,因为您不使用包含城市名称的文本框作为出发日期! :laugh:

其次,设置您的城市-通过单独的数组进行城市距离比较麻烦,如果您将它们都作为单独的数组进行循环,则很难通过循环实现自动化.考虑更改为带有城市索引的二维数组:

Can I suggest a couple of changes, before you go any further?

Firstly, don''t use the Visual Studio default names for anything! When you have an application full of button1, button2, button3, textBox1, textBox2, etc., it is very difficult to work out what is going on. If you use descriptive names instead, then your code is both easier to read, and (generally) more reliable, because you don''t use the textbox containing the city name as a departure date! :laugh:

Secondly, Setting up your city - city distances via separate arrays is a bit of a pain for you to work with - it is dfifficult to automate via loops if you =have they all as separate arrays. Consider changing to a two dimensional array, with indexes for cities:

     0     1     2
0    0   100   150
1  100     0    50
2  150    50     0

然后,当您寻找距离时,它只是cityDistance [city1,city2],而不必计算出city1使用哪个数组,然后为city2偏移它. >
考虑了一段时间后,您应该发现完成作业相对简单! :D

Then when you are looking for distances, it''s just cityDistance[city1, city2] rather than having to work out which array to use for city1, then offset into it for city2.

When you have thought about that for a while, you should find it relatively simple to complete your homework! :D


您可以查看 Wikipedia上的旅行推销员问题 [ ^ ]以获得一些见识.
在线免费 [
You may look at Travelling Salesman Problem at Wikipedia[^] for some insight.
Old "Numerical recipes in C", freely available online[^] shows how to use Simulated Annealing method to deal with such a problem.


这篇关于具有数组的城市之间的搜索算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆