Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- class LazerMaze
- {
- static string[,] matrix;
- static int maxCount;
- static void Main()
- {
- StreamReader input = new StreamReader("input.txt");
- StreamWriter output = new StreamWriter("output.txt");
- int mazeCount = int.Parse(input.ReadLine());
- for (int index = 0; index < mazeCount; index++)
- {
- string[] dimensions = input.ReadLine().Split();
- int rows = int.Parse(dimensions[0]);
- int cols = int.Parse(dimensions[1]);
- matrix = new string[rows, cols];
- List<Turret> turrets = new List<Turret>();
- maxCount = int.MaxValue;
- int startingRow = 0;
- int startingCol = 0;
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- string element = ((char)input.Read()).ToString();
- matrix[row, col] = element;
- switch (element)
- {
- case ">":
- turrets.Add(new Turret(row, col, "right"));
- break;
- case "v":
- turrets.Add(new Turret(row, col, "down"));
- break;
- case "<":
- turrets.Add(new Turret(row, col, "left"));
- break;
- case "^":
- turrets.Add(new Turret(row, col, "up"));
- break;
- case "S":
- startingRow = row;
- startingCol = col;
- break;
- }
- }
- input.ReadLine();
- }
- // TODO: check visited routes when hit
- breadthFirstSearch(startingRow, startingCol, 0, turrets);
- if (maxCount != int.MaxValue)
- {
- output.WriteLine("Case #{0}: {1}", index + 1, maxCount);
- }
- else
- {
- output.WriteLine("Case #{0}: impossible", index + 1);
- }
- }
- }
- private static void breadthFirstSearch(int row, int col, int count, List<Turret> turrets)
- {
- if (!inRange(row, col))
- {
- return;
- }
- if (matrix[row, col] != "S" && turretHit(row, col, turrets))
- {
- return;
- }
- if (matrix[row, col] == "G")
- {
- checkShortestRoute(count);
- return;
- }
- if (matrix[row, col] != "." && matrix[row, col] != "S")
- {
- return;
- }
- matrix[row, col] = "v";
- count++;
- breadthFirstSearch(row, col + 1, count, turrets); // right
- breadthFirstSearch(row, col - 1, count, turrets); // left
- breadthFirstSearch(row - 1, col, count, turrets); // top
- breadthFirstSearch(row + 1, col, count, turrets); // bottom
- matrix[row, col] = ".";
- }
- private static void clearVisitedAreas()
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row, col] == "v")
- {
- matrix[row, col] = ".";
- }
- }
- }
- }
- private static bool turretHit(int row, int col, List<Turret> turrets)
- {
- if (matrix[row, col] != "S")
- {
- Turret.changeDirection(turrets);
- }
- for (int index = 0; index < turrets.Count; index++)
- {
- switch (turrets[index].direction)
- {
- case "right":
- if (row == turrets[index].row && col > turrets[index].col &&
- !checkObstacles(row, turrets[index].col, col, "horizontal"))
- {
- return true;
- }
- break;
- case "down":
- if (col == turrets[index].col && row > turrets[index].row &&
- !checkObstacles(col, turrets[index].row, row, "vertical"))
- {
- return true;
- }
- break;
- case "left":
- if (row == turrets[index].row && col < turrets[index].col &&
- !checkObstacles(row, col, turrets[index].col, "horizontal"))
- {
- return true;
- }
- break;
- case "up":
- if (col == turrets[index].col && row < turrets[index].row &&
- !checkObstacles(col, row, turrets[index].row, "vertical"))
- {
- return true;
- }
- break;
- }
- }
- return false;
- }
- private static bool checkObstacles(int constPos, int start, int end, string type)
- {
- if (type == "horizontal")
- {
- for (int index = start + 1; index < end; index++)
- {
- if (matrix[constPos, index] != "." &&
- matrix[constPos, index] != "v" &&
- matrix[constPos, index] != "G" &&
- matrix[constPos, index] != "S")
- {
- return true;
- }
- }
- }
- else
- {
- for (int index = start + 1; index < end; index++)
- {
- if (matrix[index, constPos] != "." &&
- matrix[index, constPos] != "v" &&
- matrix[index, constPos] != "G" &&
- matrix[index, constPos] != "S")
- {
- return true;
- }
- }
- }
- return false;
- }
- private static void checkShortestRoute(int count)
- {
- if (maxCount > count)
- {
- maxCount = count;
- }
- }
- private static bool inRange(int row, int col)
- {
- if (row < 0 || row >= matrix.GetLength(0) ||
- col < 0 || col >= matrix.GetLength(1))
- {
- return false;
- }
- return true;
- }
- private static void printMatrix(object[,] matrix)
- {
- string output = "";
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (col != matrix.GetLength(1) - 1)
- {
- output += matrix[row, col];
- }
- else
- {
- output += matrix[row, col] + "\n";
- }
- }
- }
- Console.WriteLine(output);
- Console.WriteLine(new string('-', 20));
- }
- }
- public class Turret
- {
- public int row;
- public int col;
- public string direction;
- public Turret(int row, int col, string direction)
- {
- this.row = row;
- this.col = col;
- this.direction = direction;
- }
- public static void changeDirection(List<Turret> turrets)
- {
- for (int index = 0; index < turrets.Count; index++)
- {
- switch (turrets[index].direction)
- {
- case "right":
- turrets[index].direction = "down";
- break;
- case "down":
- turrets[index].direction = "left";
- break;
- case "left":
- turrets[index].direction = "up";
- break;
- case "up":
- turrets[index].direction = "right";
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement