Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class Snake
- {
- private static List<char> snake = new List<char>();
- private static int snakeSize = 0;
- private static int snakesFound = 0;
- private static HashSet<string> uniqueSnakes = new HashSet<string>();
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n * 2, n * 2];
- for (int i = 0; i < n * 2; i++)
- {
- for (int j = 0; j < n * 2; j++)
- {
- matrix[i, j] = ' ';
- }
- }
- FindSnakes(n, n, 'S', matrix);
- Console.WriteLine("Snakes count = {0}", snakesFound);
- }
- private static void FindSnakes(int row, int col, char direction, char[,] matrix)
- {
- if (snakeSize >= matrix.GetLength(0))
- {
- return;
- }
- if ((col < 0) || (row < 0) || (col >= matrix.GetLength(1)) || (row >= matrix.GetLength(0)))
- {
- return;
- }
- if (matrix[row, col] != ' ')
- {
- return;
- }
- snakeSize++;
- if (snakeSize == matrix.GetLength(1) / 2)
- {
- snake.Add(direction);
- if (!SnakeAlreadyExists(string.Join("", snake)))
- {
- snakesFound++;
- Console.WriteLine(string.Join("", snake));
- }
- snakeSize--;
- snake.RemoveAt(snake.Count - 1);
- return;
- }
- snake.Add(direction);
- matrix[row, col] = 'X';
- FindSnakes(row, col + 1, 'R', matrix);
- if (snakeSize > 1)
- {
- FindSnakes(row + 1, col, 'D', matrix);
- }
- if (snakeSize > 2)
- {
- FindSnakes(row, col - 1, 'L', matrix);
- }
- if (snakeSize > 3)
- {
- FindSnakes(row - 1, col, 'U', matrix);
- }
- matrix[row, col] = ' ';
- snake.RemoveAt(snake.Count - 1);
- snakeSize--;
- }
- private static bool SnakeAlreadyExists(string snake)
- {
- var reversedSnake = ReverseFlow(snake);
- while (reversedSnake[1] != 'R')
- {
- reversedSnake = Rotate(reversedSnake);
- }
- if (uniqueSnakes.Contains(reversedSnake))
- {
- return true;
- }
- reversedSnake = GetVerticalMirrorImage(reversedSnake);
- if (uniqueSnakes.Contains(reversedSnake))
- {
- return true;
- }
- reversedSnake = ReverseFlow(reversedSnake);
- while (reversedSnake[1] != 'R')
- {
- reversedSnake = Rotate(reversedSnake);
- }
- if (uniqueSnakes.Contains(reversedSnake))
- {
- return true;
- }
- uniqueSnakes.Add(snake);
- return false;
- }
- private static string GetVerticalMirrorImage(string snake)
- {
- var newSnake = new StringBuilder();
- var newDirection = ' ';
- for (int i = 0; i < snake.Length; i++)
- {
- if (snake[i] == 'D')
- {
- newDirection = 'U';
- }
- else if (snake[i] == 'U')
- {
- newDirection = 'D';
- }
- else
- {
- newDirection = snake[i];
- }
- newSnake.Append(newDirection);
- }
- return newSnake.ToString();
- }
- private static string Rotate(string snake)
- {
- var newSnake = new StringBuilder();
- newSnake.Append("S");
- char newDirection = ' ';
- for (int i = 1; i < snake.Length; i++)
- {
- switch (snake[i])
- {
- case 'R': newDirection = 'D'; break;
- case 'D': newDirection = 'L'; break;
- case 'L': newDirection = 'U'; break;
- case 'U': newDirection = 'R'; break;
- default: break;
- }
- newSnake.Append(newDirection);
- }
- return newSnake.ToString();
- }
- private static string ReverseFlow(string snake)
- {
- var newSnake = new StringBuilder();
- newSnake.Append("S");
- char newDirection = ' ';
- for (int i = snake.Length - 1; i > 1; i--)
- {
- switch (snake[i])
- {
- case 'R': newDirection = 'L'; break;
- case 'D': newDirection = 'U'; break;
- case 'L': newDirection = 'R'; break;
- case 'U': newDirection = 'D'; break;
- default: break;
- }
- newSnake.Append(newDirection);
- }
- newSnake.Append("L");
- return newSnake.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement