Advertisement
Guest User

Nit$ hajó$kapitány vizei

a guest
Jan 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.IO;
  7.  
  8. namespace SearchForIsland
  9. {
  10.     class Position
  11.     {
  12.         public int x { get; private set; }
  13.         public int y { get; private set; }
  14.  
  15.         public Position(int a, int b)
  16.         {
  17.             x = a;
  18.             y = b;
  19.         }
  20.     }
  21.     class Water
  22.     {
  23.         public Position tl { get; private set; }
  24.         public Position br { get; private set; }
  25.  
  26.         public int GetSize()
  27.         {
  28.             return (br.x - tl.x) * (br.y - tl.y);
  29.         }
  30.  
  31.         public Water(Position a, Position b)
  32.         {
  33.             tl = a;
  34.             br = b;
  35.         }
  36.     }
  37.  
  38.     class Program
  39.     {
  40.         static void ReadFile(byte filenum)
  41.         {
  42.             string[] lines = File.ReadAllLines($"fel{filenum}.txt");
  43.             int linelength = lines.Length;
  44.             int columnlength = lines[0].Length;
  45.             source = new char[linelength + 1, columnlength + 1];
  46.             for (int i = 1; i <= linelength; i++)
  47.             {
  48.                 for (int j = 1; j <= columnlength; j++)
  49.                 {
  50.                     source[i, j] = lines[i - 1][j - 1];
  51.                 }
  52.             }
  53.         }
  54.  
  55.         static void FillHelper(string fn)
  56.         {
  57.             StreamWriter sw = new StreamWriter(fn);
  58.             helper = new int[source.GetLength(0), source.GetLength(1)];
  59.             for (int i = 1; i < source.GetLength(0); i++)
  60.             {
  61.                 for (int j = 1; j < source.GetLength(1); j++)
  62.                 {
  63.                     helper[i, j] = helper[i - 1, j] + helper[i, j - 1] - helper[i - 1, j - 1] + (source[i, j] == '#' ? 0 : 1);
  64.                     sw.Write($"{helper[i, j]}\t");
  65.                 }
  66.                 sw.WriteLine();
  67.             }
  68.             sw.Close();
  69.         }
  70.  
  71.         static int NumOfIslands(int r1, int c1, int r2, int c2)
  72.         {
  73.             return helper[r2, c2] + helper[r1 - 1, c1 - 1] - helper[r1 - 1, c2] - helper[r2, c1 - 1];
  74.         }
  75.  
  76.         static Water GetGreatest()
  77.         {
  78.             Water max = new Water(new Position(0, 0), new Position(0, 0));
  79.             for (int r1 = 1; r1 < helper.GetLength(0); r1++)
  80.             {
  81.                 for (int c1 = 1; c1 < helper.GetLength(1); c1++)
  82.                 {
  83.                     for (int r2 = r1; r2 < helper.GetLength(0); r2++)
  84.                     {
  85.                         for (int c2 = c1; c2 < helper.GetLength(1); c2++)
  86.                         {
  87.                             if (NumOfIslands(r1, c1, r2, c2) == 0)
  88.                             {
  89.                                 Water current = new Water(new Position(r1, c1), new Position(r2, c2));
  90.                                 if (max.GetSize() < current.GetSize())
  91.                                 {
  92.                                     max = current;
  93.                                 }
  94.                             }
  95.                             else break;
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             return max;
  101.         }
  102.  
  103.         static void SetGreatest()
  104.         {
  105.             for (int i = max.tl.x; i <= max.br.x; i++)
  106.             {
  107.                 for (int j = max.tl.y; j <= max.br.y; j++)
  108.                 {
  109.                     source[i, j] = 'M';
  110.                 }
  111.             }
  112.         }
  113.  
  114.         static void WriteMatrix()
  115.         {
  116.             for (int i = 0; i < source.GetLength(0); i++)
  117.             {
  118.                 for (int j = 0; j < source.GetLength(1); j++)
  119.                 {
  120.                     Console.Write($"{source[i, j]}");
  121.                 }
  122.                 Console.WriteLine();
  123.             }
  124.         }
  125.  
  126.         static void WriteEx(byte num, Water greatest, int ms)
  127.         {
  128.             if (greatest.GetSize() == 0)
  129.                 Console.WriteLine($"{num}.: Nincs víz!");
  130.             else
  131.                 Console.WriteLine($"{num}.: Bal felső: {greatest.tl.x}x{greatest.tl.y}; Jobb alsó: {greatest.br.x}x{greatest.br.y}; Méret: {greatest.GetSize()}; T = {ms}ms");
  132.         }
  133.  
  134.         static char[,] source;
  135.         static int[,] helper;
  136.         static Water max;
  137.  
  138.         static void DoEx(byte num, int ms)
  139.         {
  140.             source = null;
  141.             helper = null;
  142.             max = null;
  143.             ReadFile(num);
  144.             FillHelper($"Szigetek{num}.txt");
  145.             max = GetGreatest();
  146.             WriteEx(num, max, ms);
  147.         }
  148.  
  149.         static void Main(string[] args)
  150.         {
  151.             /*Console.WriteLine("Feladatszám: ");
  152.             byte n = byte.Parse(Console.ReadLine());
  153.             ReadFile(n);
  154.             FillHelper("Szigetek1.txt");
  155.             max = GetGreatest();
  156.             SetGreatest();
  157.             WriteMatrix();*/
  158.             Stopwatch st = new Stopwatch();
  159.             for (int i = 1; i < 10; i++)
  160.             {
  161.                 st.Start();
  162.                 DoEx((byte)i, (int)st.ElapsedMilliseconds);
  163.                 st.Stop();
  164.             }
  165.             Console.ReadLine();
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement