Informatika gyűjtemény

Egy szinttel feljebb mb_szojatek.cs

2004050607080910

NézetNyomtat

mb_szojatek.cs (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 3 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Szotar
{
    class Program
    {
        static char mode;
        static int n, s, f;
        static string[] szotar;
        static int[] dist;
        static bool[,] connections;

        static int distance;

        static void Beolvas(string file1, string file2)
        {
            string line;

            StreamReader sr1 = new StreamReader(file1);
            line = sr1.ReadLine();
            n = Convert.ToInt32(line);
            szotar = new string[n];
            for (int i = 0; i != n; i++)
            {
                line = sr1.ReadLine();
                szotar[i] = line;
            }
            sr1.Close();

            string szo1, szo2;

            StreamReader sr2 = new StreamReader(file2);
            line = sr2.ReadLine();
            mode = Convert.ToChar(line);
            szo1 = sr2.ReadLine();
            szo2 = sr2.ReadLine();
            sr2.Close();

            for (int i = 0; i != n; i++)
            {
                if (szo1 == szotar[i]) s = i;
                if (szo2 == szotar[i]) f = i;
            }

        }

        static bool Connected(string szo1, string szo2)
        {
            if (szo1 == szo2) return false;
            if (mode == 'A') return ConnectedA(szo1, szo2);
            if (mode == 'B') return ConnectedB(szo1, szo2);
            throw new Exception("wrong mode");
        }

        static bool ConnectedA(string szo1, string szo2)
        {
            if (szo1.Length != szo2.Length)
                return false;

            int diff = 0;
            for (int i = 0; i != szo1.Length; i++)
                if (szo1[i] != szo2[i]) diff++;

            if (diff == 1) return true;
            return false;
        }

        static bool ConnectedB(string szo1, string szo2)
        {
            if (szo1.Length == szo2.Length) return ConnectedA(szo1, szo2);
            if (szo1.Length == szo2.Length - 1) return ConnectedA(szo1, szo2.Substring(0, szo2.Length - 1));
            return false;
        }

        static void Munka()
        {
            connections = new bool[n, n];

            for (int i = 0; i != n; i++)
                for (int j = 0; j != n; j++)
                    connections[i, j] = Connected(szotar[i], szotar[j]);

            dist = new int[n];
            for (int i = 0; i != n; i++) dist[i] = -2; //-2 : nem vótt ott még sönki; -1 : má majdnem
            dist[s] = 0;
            distance = MinDist(f);
        }

        static int MinDist(int id)
        {

            if (dist[id] >= 0 || dist[id] == -3) return dist[id]; //-3 hhö
            dist[id] = -1; //má majdnem állapot
            int d = -1;
            for (int i = 0; i != n; i++)
                if (dist[i] != -3 && dist[i] != -1 && connections[i, id]) //még sönki & ibő ibe
                {
                    int tmp = MinDist(i);
                    if (tmp != -3)
                    {
                        if (== -1) d = tmp;
                        else
                            d = Math.Min(d, tmp);
                    }
                }

            d++;
            if (<= 0)
            {
                dist[id] = -3;
                return -3;
            }
            return d;
        }

        static void Kiir(string file)
        {
            StreamWriter sw = new StreamWriter(file);
            if (distance >= 0)
            {
                sw.WriteLine("IGEN");
                sw.WriteLine(distance);
            }
            else
            {
                sw.Write("NEM");
            }
            Console.WriteLine("");
            for (int i = 0; i != n; i++)
                if (connections[s, i])
                    sw.Write(szotar[i] + " ");
            sw.Close();
        }

        static void Main(string[] args)
        {
            Beolvas("szotar.be", "jatek.be");
            Munka();
            Kiir("jatek.ki");
        }
    }
}
(Vissza)