본문 바로가기

C#/공부

C# 실력 늘리기 10일차

188~190. 최단거리 구하기(플로이드): 지하철을 이용하여 최단거리로 가는 방법을 구하자.

 

플로이드 알고리즘을 구현하여 지하철 역 사이의 최단경로를 보여주는 문제인데, 사실 가장 큰 복병은 데이터 입력이다.

모든 역마다 다른 역에 대한 거리 정보를 입력해줘야 되기 때문에 (역의 개수)^2 크기의 배열을 노선도를 보면서 입력해야 한다.심지어 교차되는 역은 따로 고려해야 되기 때문에 반복도 제한적으로만 사용가능하다. (아래의 코드도 역은 16개인데 예외가 10개이다...)

 

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 Subway
{
    public partial class Form1 : Form
    {
        Form2 allPath;
        private String[] station = { "대구역","중앙로","반월당","명덕","교대","영대병원"
                         ,"내당","반고개","신남","경대병원","대구은행"
                         ,"달성공원","서문시장","남산","건들바위","대봉교"};
        private int[,] path;
        private Floyd floyd;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = station;
            comboBox2.DataSource = station.Clone();
            richTextBox1.Text = "1호선 - 0.대구역 1.중앙로 2.반월당 3.명덕 4.교대 5.영대병원\n"
                    + "2호선 - 6.내당 7.반고개 8.신남 9.경대병원 10.대구은행\n"
                    + "3호선 - 11.달성공원 12.서문시장 13.남산 14.건들바위 15.대봉교";
            makePath();
            floyd = new Floyd(station, path);
            floyd.Distance();
        }
        private void makePath()
        {
            path = new int[station.Length, station.Length];
            for (int i = 0; i < path.GetLength(0); i++)
            {
                for (int j = 0; j < path.GetLength(0); j++)
                {
                    if (i == j)
                        path[i, j] = 0;
                    else
                        path[i, j] = 99;
                }
            }
            List<int> except = new List<int>(); //앞뒤에 역이 하나만 있는 곳
            except.Add(0);  //except(0)
            except.Add(5);  //except(1)
            except.Add(6);
            except.Add(10);
            except.Add(11);
            except.Add(15);
            except.Add(9);  //여기서부터 교차역 설정
            except.Add(8);
            except.Add(14);
            except.Add(12);

            for (int i = 0; i < path.GetLength(0); i++)
            {
                for (int j = 0; j < path.GetLength(0); j++)
                {
                    if (i == 13)
                        continue;
                    if (except.Contains(i))
                    {
                        if (except.IndexOf(i) % 2 == 0)
                            path[i, i + 1] = 1;
                        else
                            path[i, i - 1] = 1;
                    }
                    else
                    {
                        path[i, i - 1] = 1;
                        path[i, i + 1] = 1;
                    }
                }
            }
            cross(2, 8);    //교차역 설정
            cross(2, 9);
            cross(3, 13);
            cross(3, 14);
            cross(8, 12);
            cross(8, 13);
        }
        public void cross(int i, int j)
        {
            path[i, j] = 1;
            path[j, i] = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            floyd.result = "";
            lbResult.Text = "";
            floyd.Path(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
            lbResult.Text = floyd.name[comboBox1.SelectedIndex] + " -> " + floyd.result
                + floyd.name[comboBox2.SelectedIndex];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            allPath = new Form2();
            for (int i = 0; i < path.GetLength(0); i++)
            {
                for (int j = 0; j < path.GetLength(0); j++)
                {
                    if (i == j)
                        continue;
                    floyd.result = "";
                    floyd.Path(i, j);
                    allPath.richTextBox1.AppendText("["+floyd.name[i]+"->"+ floyd.name[j]+"] : "
                    +floyd.name[i] + "->" + floyd.result + floyd.name[j] + "\r\n");
                }
            }
            allPath.richTextBox1.SelectionStart = 0;
            allPath.Show();
        }
    }
    public class Floyd
    {
        private int[,] data;
        private int[,] p;
        public string[] name;
        public string result;

        public Floyd(string[] name, int[,] data)
        {
            this.data = data;
            p = new int[data.GetLength(0), data.GetLength(0)];
            this.name = name;
        }
        public void Distance()
        {
            for (int i = 0; i < data.GetLength(0); i++)
                for (int j = 0; j < data.GetLength(0); j++)
                    p[i, j] = -1;
            for (int k = 0; k < data.GetLength(0); k++)
                for (int i = 0; i < data.GetLength(0); i++)
                    for (int j = 0; j < data.GetLength(0); j++)
                        if (data[i, j] > data[i, k] + data[k, j])
                        {
                            p[i, j] = k;
                            data[i, j] = data[i, k] + data[k, j];
                        }
        }
        public void Path(int q, int r)
        {
            if (p[q, r] != -1)
            {
                Path(q, p[q, r]);
                result += name[p[q, r]] + " -> ";
                Path(p[q, r], r);
            }
        }
        public void PrintPath()
        {
            int count = data.GetLength(0);
            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                    Console.Write("{0}  ", data[i, j]);
                Console.WriteLine();
            }
        }
    }
}

 

Form2의 버튼이벤트

 

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

 

 

 

193.  웹 브라우저 만들기: 웹 브라우저를 만들어 보자.

 

Progress Bar 빼고는 최대한 크롬이랑 비슷하게 만들려 했는데...

 

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 WebBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            webBrowser1.CanGoBackChanged += new EventHandler(webBrowser1_CanGoBackChanged);
            webBrowser1.CanGoForwardChanged += new EventHandler(webBrowser1_CanGoForwardChanged);
            webBrowser1.Navigate("https://ggaebap.tistory.com/");
        }
        private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)Keys.Enter == e.KeyChar)
            {
                webBrowser1.Navigate(toolStripTextBox1.Text);
            }
        }
        private void webBrowser1_CanGoBackChanged(object sender, EventArgs e)
        {
            if (webBrowser1.CanGoBack == true)
                toolStripButton1.Enabled = true;
            else
                toolStripButton1.Enabled = false;
        }
        private void webBrowser1_CanGoForwardChanged(object sender, EventArgs e)
        {
            if (webBrowser1.CanGoForward == true)
                toolStripButton2.Enabled = true;
            else
                toolStripButton2.Enabled = false;
        }
        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            long total = e.MaximumProgress;
            long current = e.CurrentProgress;
            toolStripProgressBar1.Value = (int)(100.0 * current / total);
        }
        private void toolStripButton1_Click(object sender, EventArgs e) //뒤로가기
        {
            if (webBrowser1.CanGoBack)
            {
                webBrowser1.GoBack();
                toolStripTextBox1.Text = webBrowser1.Url.ToString();
            }
        }
        private void toolStripButton2_Click(object sender, EventArgs e) //앞으로가기
        {
            if (webBrowser1.CanGoForward)
            {
                webBrowser1.GoForward();
                toolStripTextBox1.Text = webBrowser1.Url.ToString();
            }
        }
        private void toolStripButton3_Click(object sender, EventArgs e) //새로고침
        {
            webBrowser1.Refresh();
        }
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            toolStripTextBox1.Text = webBrowser1.Url.ToString();
        }
    }
}

 

'C# > 공부' 카테고리의 다른 글

C# 실력 늘리기 11일차  (4) 2022.08.02
C# 실력 늘리기 9일차  (0) 2022.07.29
C# 실력 늘리기 8일차  (0) 2022.07.28
C# 실력 늘리기 7일차  (0) 2022.07.27
C# 실력 늘리기 6일차  (0) 2022.07.26