Advertisement
GrandtherAzaMarks

Search for the exit from maze

Apr 28th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace HumanLegacy
  6. {
  7.     class MainClass
  8.     {
  9.         static bool find(int[][] matrix, int cX, int cY, int pX, int pY, bool[,] check)
  10.         {
  11.             if (cX == matrix.Length - 2 && cY == matrix[matrix.Length - 2].Length - 2)
  12.                 return true;
  13.             else
  14.             {
  15.  
  16.                 if (check[cX + 1, cY] == false)
  17.                 {
  18.                     check[cX + 1, cY] = true;
  19.                     if (find(matrix, cX + 1, cY, cX, cY, check) == true)
  20.                         return true;
  21.                    
  22.                 }
  23.                 if (check[cX - 1, cY] == false)
  24.                 {
  25.                     check[cX - 1, cY] = true;
  26.                     if (find(matrix, cX - 1, cY, cX, cY, check) == true)
  27.                         return true;
  28.                 }
  29.                 if (check[cX, cY + 1] == false)
  30.                 {
  31.                     check[cX, cY + 1] = true;
  32.                     if (find(matrix, cX, cY + 1, cX, cY, check) == true)
  33.                         return true;
  34.                 }
  35.                 if (check[cX, cY - 1] == false)
  36.                 {
  37.                     check[cX, cY - 1] = true;
  38.                     if (find(matrix, cX, cY - 1, cX, cY, check) == true)
  39.                         return true;
  40.                 }
  41.             }
  42.             return false;
  43.         }
  44.  
  45.         public static void Main(string[] args)
  46.         {
  47.  
  48.             int[] TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
  49.             int n = TempArray[0] + 2, m = TempArray[1] + 2;
  50.             bool[,] Checked = new bool[n + 2, m + 2];
  51.             int[][] Field = new int[n][];
  52.             for (int i = 0; i < n; i++)
  53.                 Field[i] = new int[m];
  54.             for (int i = 0; i < n; i++)
  55.                 for (int j = 0; j < m; j++)
  56.                 {
  57.                     if (i == 0 || i == n - 1 || j == 0 || j == m - 1) Field[i][j] = 1;
  58.                     else Field[i][j] = 0;
  59.                 }
  60.             for (int i = 1; i < n - 1; i++)
  61.             {
  62.                 TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
  63.                 for (int j = 1; j < m - 1; j++)
  64.                 {
  65.                     Field[i][j] = TempArray[j - 1];
  66.                 }
  67.             }
  68.             for (int i = 0; i < n; i++)
  69.             {
  70.                 for (int j = 0; j < m; j++)
  71.                 {
  72.                     if (Field[i][j] == 1)
  73.                         Checked[i, j] = true;
  74.                     else
  75.                         Checked[i, j] = false;
  76.                 }
  77.             }
  78.             if (Field[Field.Length - 2][Field[Field.Length - 2].Length - 2] == 1)
  79.                 Console.WriteLine("doesn't exist");
  80.             else if (find(Field, 1, 1, -1, -1, Checked)) Console.WriteLine("exists");
  81.             else Console.WriteLine("doesn't exist");
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement