Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace HumanLegacy
- {
- class MainClass
- {
- static bool find(int[][] matrix, int cX, int cY, int pX, int pY, bool[,] check)
- {
- if (cX == matrix.Length - 2 && cY == matrix[matrix.Length - 2].Length - 2)
- return true;
- else
- {
- if (check[cX + 1, cY] == false)
- {
- check[cX + 1, cY] = true;
- if (find(matrix, cX + 1, cY, cX, cY, check) == true)
- return true;
- }
- if (check[cX - 1, cY] == false)
- {
- check[cX - 1, cY] = true;
- if (find(matrix, cX - 1, cY, cX, cY, check) == true)
- return true;
- }
- if (check[cX, cY + 1] == false)
- {
- check[cX, cY + 1] = true;
- if (find(matrix, cX, cY + 1, cX, cY, check) == true)
- return true;
- }
- if (check[cX, cY - 1] == false)
- {
- check[cX, cY - 1] = true;
- if (find(matrix, cX, cY - 1, cX, cY, check) == true)
- return true;
- }
- }
- return false;
- }
- public static void Main(string[] args)
- {
- int[] TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
- int n = TempArray[0] + 2, m = TempArray[1] + 2;
- bool[,] Checked = new bool[n + 2, m + 2];
- int[][] Field = new int[n][];
- for (int i = 0; i < n; i++)
- Field[i] = new int[m];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- {
- if (i == 0 || i == n - 1 || j == 0 || j == m - 1) Field[i][j] = 1;
- else Field[i][j] = 0;
- }
- for (int i = 1; i < n - 1; i++)
- {
- TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
- for (int j = 1; j < m - 1; j++)
- {
- Field[i][j] = TempArray[j - 1];
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- if (Field[i][j] == 1)
- Checked[i, j] = true;
- else
- Checked[i, j] = false;
- }
- }
- if (Field[Field.Length - 2][Field[Field.Length - 2].Length - 2] == 1)
- Console.WriteLine("doesn't exist");
- else if (find(Field, 1, 1, -1, -1, Checked)) Console.WriteLine("exists");
- else Console.WriteLine("doesn't exist");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement