Advertisement
Guest User

Laser Maze

a guest
Jan 11th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. class LazerMaze
  6. {
  7.     static string[,] matrix;
  8.     static int maxCount;
  9.     static void Main()
  10.     {
  11.         StreamReader input = new StreamReader("input.txt");
  12.         StreamWriter output = new StreamWriter("output.txt");
  13.  
  14.         int mazeCount = int.Parse(input.ReadLine());
  15.  
  16.         for (int index = 0; index < mazeCount; index++)
  17.         {
  18.             string[] dimensions = input.ReadLine().Split();
  19.             int rows = int.Parse(dimensions[0]);
  20.             int cols = int.Parse(dimensions[1]);
  21.  
  22.             matrix = new string[rows, cols];
  23.             List<Turret> turrets = new List<Turret>();
  24.             maxCount = int.MaxValue;
  25.             int startingRow = 0;
  26.             int startingCol = 0;
  27.  
  28.             for (int row = 0; row < rows; row++)
  29.             {
  30.                 for (int col = 0; col < cols; col++)
  31.                 {
  32.                     string element = ((char)input.Read()).ToString();
  33.                     matrix[row, col] = element;
  34.  
  35.                     switch (element)
  36.                     {
  37.                         case ">":
  38.                             turrets.Add(new Turret(row, col, "right"));
  39.                             break;
  40.                         case "v":
  41.                             turrets.Add(new Turret(row, col, "down"));
  42.                             break;
  43.                         case "<":
  44.                             turrets.Add(new Turret(row, col, "left"));
  45.                             break;
  46.                         case "^":
  47.                             turrets.Add(new Turret(row, col, "up"));
  48.                             break;
  49.                         case "S":
  50.                             startingRow = row;
  51.                             startingCol = col;
  52.                             break;
  53.                     }
  54.                 }
  55.                 input.ReadLine();
  56.             }
  57.             // TODO: check visited routes when hit
  58.  
  59.             breadthFirstSearch(startingRow, startingCol, 0, turrets);
  60.             if (maxCount != int.MaxValue)
  61.             {
  62.                 output.WriteLine("Case #{0}: {1}", index + 1, maxCount);
  63.             }
  64.             else
  65.             {
  66.                 output.WriteLine("Case #{0}: impossible", index + 1);
  67.             }
  68.         }
  69.     }
  70.  
  71.     private static void breadthFirstSearch(int row, int col, int count, List<Turret> turrets)
  72.     {
  73.  
  74.         if (!inRange(row, col))
  75.         {
  76.             return;
  77.         }
  78.  
  79.         if (matrix[row, col] != "S" && turretHit(row, col, turrets))
  80.         {
  81.             return;
  82.         }
  83.         if (matrix[row, col] == "G")
  84.         {
  85.             checkShortestRoute(count);
  86.             return;
  87.         }
  88.         if (matrix[row, col] != "." && matrix[row, col] != "S")
  89.         {
  90.             return;
  91.         }
  92.  
  93.         matrix[row, col] = "v";
  94.         count++;
  95.         breadthFirstSearch(row, col + 1, count, turrets); // right
  96.         breadthFirstSearch(row, col - 1, count, turrets); // left
  97.         breadthFirstSearch(row - 1, col, count, turrets); // top
  98.         breadthFirstSearch(row + 1, col, count, turrets); // bottom
  99.         matrix[row, col] = ".";
  100.     }
  101.  
  102.     private static void clearVisitedAreas()
  103.     {
  104.         for (int row = 0; row < matrix.GetLength(0); row++)
  105.         {
  106.             for (int col = 0; col < matrix.GetLength(1); col++)
  107.             {
  108.                 if (matrix[row, col] == "v")
  109.                 {
  110.                     matrix[row, col] = ".";  
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     private static bool turretHit(int row, int col, List<Turret> turrets)
  117.     {
  118.         if (matrix[row, col] != "S")
  119.         {
  120.             Turret.changeDirection(turrets);
  121.         }
  122.         for (int index = 0; index < turrets.Count; index++)
  123.         {
  124.             switch (turrets[index].direction)
  125.             {
  126.                 case "right":
  127.                     if (row == turrets[index].row && col > turrets[index].col &&
  128.                         !checkObstacles(row, turrets[index].col, col, "horizontal"))
  129.                     {
  130.                         return true;
  131.                     }
  132.                     break;
  133.                 case "down":
  134.                     if (col == turrets[index].col && row > turrets[index].row &&
  135.                         !checkObstacles(col, turrets[index].row, row, "vertical"))
  136.                     {
  137.                         return true;
  138.                     }
  139.                     break;
  140.                 case "left":
  141.                     if (row == turrets[index].row && col < turrets[index].col &&
  142.                         !checkObstacles(row, col, turrets[index].col, "horizontal"))
  143.                     {
  144.                         return true;
  145.                     }
  146.                     break;
  147.                 case "up":
  148.                     if (col == turrets[index].col && row < turrets[index].row &&
  149.                         !checkObstacles(col, row, turrets[index].row, "vertical"))
  150.                     {
  151.                         return true;
  152.                     }
  153.                     break;
  154.             }
  155.         }
  156.  
  157.         return false;
  158.     }
  159.  
  160.     private static bool checkObstacles(int constPos, int start, int end, string type)
  161.     {
  162.         if (type == "horizontal")
  163.         {
  164.             for (int index = start + 1; index < end; index++)
  165.             {
  166.                 if (matrix[constPos, index] != "." &&
  167.                     matrix[constPos, index] != "v" &&
  168.                     matrix[constPos, index] != "G" &&
  169.                     matrix[constPos, index] != "S")
  170.                 {
  171.                     return true;
  172.                 }
  173.             }
  174.         }
  175.         else
  176.         {
  177.             for (int index = start + 1; index < end; index++)
  178.             {
  179.                 if (matrix[index, constPos] != "." &&
  180.                     matrix[index, constPos] != "v" &&
  181.                     matrix[index, constPos] != "G" &&
  182.                     matrix[index, constPos] != "S")
  183.                 {
  184.                     return true;
  185.                 }
  186.             }
  187.         }
  188.  
  189.         return false;
  190.     }
  191.  
  192.     private static void checkShortestRoute(int count)
  193.     {
  194.         if (maxCount > count)
  195.         {
  196.             maxCount = count;
  197.         }
  198.     }
  199.  
  200.     private static bool inRange(int row, int col)
  201.     {
  202.         if (row < 0 || row >= matrix.GetLength(0) ||
  203.             col < 0 || col >= matrix.GetLength(1))
  204.         {
  205.             return false;
  206.         }
  207.  
  208.         return true;
  209.     }
  210.  
  211.     private static void printMatrix(object[,] matrix)
  212.     {
  213.         string output = "";
  214.         for (int row = 0; row < matrix.GetLength(0); row++)
  215.         {
  216.             for (int col = 0; col < matrix.GetLength(1); col++)
  217.             {
  218.                 if (col != matrix.GetLength(1) - 1)
  219.                 {
  220.                     output += matrix[row, col];
  221.                 }
  222.                 else
  223.                 {
  224.                     output += matrix[row, col] + "\n";
  225.                 }
  226.             }
  227.         }
  228.  
  229.         Console.WriteLine(output);
  230.         Console.WriteLine(new string('-', 20));
  231.     }
  232. }
  233.  
  234. public class Turret
  235. {
  236.     public int row;
  237.     public int col;
  238.     public string direction;
  239.  
  240.     public Turret(int row, int col, string direction)
  241.     {
  242.         this.row = row;
  243.         this.col = col;
  244.         this.direction = direction;
  245.     }
  246.  
  247.     public static void changeDirection(List<Turret> turrets)
  248.     {
  249.         for (int index = 0; index < turrets.Count; index++)
  250.         {
  251.             switch (turrets[index].direction)
  252.             {
  253.                 case "right":
  254.                     turrets[index].direction = "down";
  255.                     break;
  256.                 case "down":
  257.                     turrets[index].direction = "left";
  258.                     break;
  259.                 case "left":
  260.                     turrets[index].direction = "up";
  261.                     break;
  262.                 case "up":
  263.                     turrets[index].direction = "right";
  264.                     break;
  265.             }
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement