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.IO;
- namespace _7
- {
- class Program
- {
- static int CortSize = 52;
- static bool[,] Cort = new bool[CortSize, CortSize];
- static bool IsSolidWithout(int x, int y)
- {
- Cort[x, y] = false;
- bool[,] ConnectionMap = new bool[CortSize, CortSize];
- bool IsSolid = false;
- int WaysBefore = 0;
- WaysBefore += Cort[x + 1, y] ? 1 : 0;
- WaysBefore += Cort[x - 1, y] ? 1 : 0;
- WaysBefore += Cort[x, y + 1] ? 1 : 0;
- WaysBefore += Cort[x, y - 1] ? 1 : 0;
- if (WaysBefore > 1)
- {
- if (Cort[x + 1, y]) setConnection(ref ConnectionMap, x + 1, y);
- else if (Cort[x - 1, y]) setConnection(ref ConnectionMap, x - 1, y);
- else setConnection(ref ConnectionMap, x, y + 1);
- int WaysAfter = 0;
- WaysAfter += ConnectionMap[x + 1, y] ? 1 : 0;
- WaysAfter += ConnectionMap[x - 1, y] ? 1 : 0;
- WaysAfter += ConnectionMap[x, y + 1] ? 1 : 0;
- WaysAfter += ConnectionMap[x, y - 1] ? 1 : 0;
- IsSolid = WaysAfter != WaysBefore;
- }
- Cort[x, y] = true;
- return IsSolid;
- }
- static void setConnection(ref bool[,] Map, int x, int y)
- {
- Map[x, y] = true;
- if (Cort[x + 1, y] && !Map[x + 1, y]) setConnection(ref Map, x + 1, y);
- if (Cort[x - 1, y] && !Map[x - 1, y]) setConnection(ref Map, x - 1, y);
- if (Cort[x, y + 1] && !Map[x, y + 1]) setConnection(ref Map, x, y + 1);
- if (Cort[x, y - 1] && !Map[x, y - 1]) setConnection(ref Map, x, y - 1);
- }
- static void Main(string[] args)
- {
- int result = 0;
- StreamReader SR = new StreamReader("Input.txt");
- string[] buf = SR.ReadLine().Split(' ');
- int n = int.Parse(buf[0]), m = int.Parse(buf[1]);
- for (int i = 1; i <= n; i++)
- {
- string line = SR.ReadLine();
- for (int j = 1; j <= m; j++)
- {
- if (line[j - 1] == '#')
- {
- Cort[i, j] = true;
- result++;
- }
- }
- }
- SR.Close();
- if (result > 2)
- {
- for (int i = 1; i <= n && result != 1; i++)
- {
- for (int j = 1; j <= m && result != 1; j++)
- {
- result = Cort[i, j] && IsSolidWithout(i, j) ? 1 : 2;
- }
- }
- }
- else
- {
- result = -1;
- }
- StreamWriter SW = new StreamWriter("Output.txt");
- SW.Write(result);
- SW.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement