Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- namespace SearchForIsland
- {
- class Position
- {
- public int x { get; private set; }
- public int y { get; private set; }
- public Position(int a, int b)
- {
- x = a;
- y = b;
- }
- }
- class Water
- {
- public Position tl { get; private set; }
- public Position br { get; private set; }
- public int GetSize()
- {
- return (br.x - tl.x) * (br.y - tl.y);
- }
- public Water(Position a, Position b)
- {
- tl = a;
- br = b;
- }
- }
- class Program
- {
- static void ReadFile(byte filenum)
- {
- string[] lines = File.ReadAllLines($"fel{filenum}.txt");
- int linelength = lines.Length;
- int columnlength = lines[0].Length;
- source = new char[linelength + 1, columnlength + 1];
- for (int i = 1; i <= linelength; i++)
- {
- for (int j = 1; j <= columnlength; j++)
- {
- source[i, j] = lines[i - 1][j - 1];
- }
- }
- }
- static void FillHelper(string fn)
- {
- StreamWriter sw = new StreamWriter(fn);
- helper = new int[source.GetLength(0), source.GetLength(1)];
- for (int i = 1; i < source.GetLength(0); i++)
- {
- for (int j = 1; j < source.GetLength(1); j++)
- {
- helper[i, j] = helper[i - 1, j] + helper[i, j - 1] - helper[i - 1, j - 1] + (source[i, j] == '#' ? 0 : 1);
- sw.Write($"{helper[i, j]}\t");
- }
- sw.WriteLine();
- }
- sw.Close();
- }
- static int NumOfIslands(int r1, int c1, int r2, int c2)
- {
- return helper[r2, c2] + helper[r1 - 1, c1 - 1] - helper[r1 - 1, c2] - helper[r2, c1 - 1];
- }
- static Water GetGreatest()
- {
- Water max = new Water(new Position(0, 0), new Position(0, 0));
- for (int r1 = 1; r1 < helper.GetLength(0); r1++)
- {
- for (int c1 = 1; c1 < helper.GetLength(1); c1++)
- {
- for (int r2 = r1; r2 < helper.GetLength(0); r2++)
- {
- for (int c2 = c1; c2 < helper.GetLength(1); c2++)
- {
- if (NumOfIslands(r1, c1, r2, c2) == 0)
- {
- Water current = new Water(new Position(r1, c1), new Position(r2, c2));
- if (max.GetSize() < current.GetSize())
- {
- max = current;
- }
- }
- else break;
- }
- }
- }
- }
- return max;
- }
- static void SetGreatest()
- {
- for (int i = max.tl.x; i <= max.br.x; i++)
- {
- for (int j = max.tl.y; j <= max.br.y; j++)
- {
- source[i, j] = 'M';
- }
- }
- }
- static void WriteMatrix()
- {
- for (int i = 0; i < source.GetLength(0); i++)
- {
- for (int j = 0; j < source.GetLength(1); j++)
- {
- Console.Write($"{source[i, j]}");
- }
- Console.WriteLine();
- }
- }
- static void WriteEx(byte num, Water greatest, int ms)
- {
- if (greatest.GetSize() == 0)
- Console.WriteLine($"{num}.: Nincs víz!");
- else
- 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");
- }
- static char[,] source;
- static int[,] helper;
- static Water max;
- static void DoEx(byte num, int ms)
- {
- source = null;
- helper = null;
- max = null;
- ReadFile(num);
- FillHelper($"Szigetek{num}.txt");
- max = GetGreatest();
- WriteEx(num, max, ms);
- }
- static void Main(string[] args)
- {
- /*Console.WriteLine("Feladatszám: ");
- byte n = byte.Parse(Console.ReadLine());
- ReadFile(n);
- FillHelper("Szigetek1.txt");
- max = GetGreatest();
- SetGreatest();
- WriteMatrix();*/
- Stopwatch st = new Stopwatch();
- for (int i = 1; i < 10; i++)
- {
- st.Start();
- DoEx((byte)i, (int)st.ElapsedMilliseconds);
- st.Stop();
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement