Advertisement
maxlarin2

Kutep

Jun 11th, 2014
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _7
  8. {
  9.  
  10.     class Program
  11.     {
  12.         static int CortSize = 52;
  13.         static bool[,] Cort = new bool[CortSize, CortSize];
  14.  
  15.         static bool IsSolidWithout(int x, int y)
  16.         {
  17.             Cort[x, y] = false;
  18.             bool[,] ConnectionMap = new bool[CortSize, CortSize];
  19.             bool IsSolid = false;
  20.  
  21.             int WaysBefore = 0;
  22.             WaysBefore += Cort[x + 1, y] ? 1 : 0;
  23.             WaysBefore += Cort[x - 1, y] ? 1 : 0;
  24.             WaysBefore += Cort[x, y + 1] ? 1 : 0;
  25.             WaysBefore += Cort[x, y - 1] ? 1 : 0;
  26.  
  27.             if (WaysBefore > 1)
  28.             {
  29.                 if (Cort[x + 1, y]) setConnection(ref ConnectionMap, x + 1, y);
  30.                 else if (Cort[x - 1, y]) setConnection(ref ConnectionMap, x - 1, y);
  31.                 else setConnection(ref ConnectionMap, x, y + 1);
  32.  
  33.  
  34.                 int WaysAfter = 0;
  35.                 WaysAfter += ConnectionMap[x + 1, y] ? 1 : 0;
  36.                 WaysAfter += ConnectionMap[x - 1, y] ? 1 : 0;
  37.                 WaysAfter += ConnectionMap[x, y + 1] ? 1 : 0;
  38.                 WaysAfter += ConnectionMap[x, y - 1] ? 1 : 0;
  39.  
  40.                 IsSolid = WaysAfter != WaysBefore;
  41.             }
  42.  
  43.             Cort[x, y] = true;
  44.  
  45.             return IsSolid;
  46.         }
  47.  
  48.         static void setConnection(ref bool[,] Map, int x, int y)
  49.         {
  50.             Map[x, y] = true;
  51.             if (Cort[x + 1, y] && !Map[x + 1, y]) setConnection(ref Map, x + 1, y);
  52.             if (Cort[x - 1, y] && !Map[x - 1, y]) setConnection(ref Map, x - 1, y);
  53.             if (Cort[x, y + 1] && !Map[x, y + 1]) setConnection(ref Map, x, y + 1);
  54.             if (Cort[x, y - 1] && !Map[x, y - 1]) setConnection(ref Map, x, y - 1);
  55.         }      
  56.  
  57.         static void Main(string[] args)
  58.         {
  59.             int result = 0;
  60.  
  61.             StreamReader SR = new StreamReader("Input.txt");
  62.             string[] buf = SR.ReadLine().Split(' ');
  63.             int n = int.Parse(buf[0]), m = int.Parse(buf[1]);
  64.  
  65.             for (int i = 1; i <= n; i++)
  66.             {
  67.                 string line = SR.ReadLine();
  68.                 for (int j = 1; j <= m; j++)
  69.                 {
  70.                     if (line[j - 1] == '#')
  71.                     {
  72.                         Cort[i, j] = true;
  73.                         result++;
  74.                     }
  75.                 }
  76.             }
  77.             SR.Close();
  78.  
  79.             if (result > 2)
  80.             {
  81.                 for (int i = 1; i <= n && result != 1; i++)
  82.                 {
  83.                     for (int j = 1; j <= m && result != 1; j++)
  84.                     {
  85.                         result = Cort[i, j] && IsSolidWithout(i, j) ? 1 : 2;
  86.                     }
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 result = -1;
  92.             }
  93.  
  94.             StreamWriter SW = new StreamWriter("Output.txt");
  95.             SW.Write(result);
  96.             SW.Close();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement