Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Collections;
- namespace EightQueens
- {
- public class NoMoreTokensException : Exception
- {
- }
- public class Tokenizer
- {
- string[] tokens = new string[0];
- private int pos;
- StreamReader reader;
- public Tokenizer(Stream inStream)
- {
- var bs = new BufferedStream(inStream);
- reader = new StreamReader(bs);
- }
- public Tokenizer()
- : this(Console.OpenStandardInput())
- {
- // Nothing more to do
- }
- private string PeekNext()
- {
- if (pos < 0)
- // pos < 0 indicates that there are no more tokens
- return null;
- if (pos < tokens.Length)
- {
- if (tokens[pos].Length == 0)
- {
- ++pos;
- return PeekNext();
- }
- return tokens[pos];
- }
- string line = reader.ReadLine();
- if (line == null)
- {
- // There is no more data to read
- pos = -1;
- return null;
- }
- // Split the line that was read on white space characters
- tokens = line.Split(null);
- pos = 0;
- return PeekNext();
- }
- public bool HasNext()
- {
- return (PeekNext() != null);
- }
- public string Next()
- {
- string next = PeekNext();
- if (next == null)
- throw new NoMoreTokensException();
- ++pos;
- return next;
- }
- }
- public class Scanner : Tokenizer
- {
- public int NextInt()
- {
- return int.Parse(Next());
- }
- public long NextLong()
- {
- return long.Parse(Next());
- }
- public float NextFloat()
- {
- return float.Parse(Next());
- }
- public double NextDouble()
- {
- return double.Parse(Next());
- }
- }
- public class BufferedStdoutWriter : StreamWriter
- {
- public BufferedStdoutWriter()
- : base(new BufferedStream(Console.OpenStandardOutput()))
- {
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- BufferedStdoutWriter writer = new BufferedStdoutWriter();
- int[,] boardInput = new int[8, 8];
- string[] lines = new string[8];
- Tokenizer tokens = new Tokenizer();
- for (int y = 0; y < 8; y++)
- {
- lines[y] = tokens.Next();
- char[] line = lines[y].ToCharArray();
- if (y > boardInput.GetLength(0))
- break;
- for (int x = 0; x < boardInput.GetLength(1); x++)
- {
- if (line[x].Equals('*'))
- boardInput[y, x] = 1;
- else
- boardInput[y, x] = 0;
- }
- }
- if (IsBoardValid(boardInput))
- writer.Write("valid");
- else
- writer.Write("invalid");
- writer.Flush();
- }
- catch
- {
- //writer.WriteLine("invalid");
- }
- // Console.ReadLine();
- }
- static bool IsBoardValid(int[,] boardInput)
- {
- int[,] attackVectors = new int[boardInput.GetLength(0), boardInput.GetLength(1)];
- for (int y = 0; y < attackVectors.GetLength(0); y++)
- {
- for (int x = 0; x < attackVectors.GetLength(1); x++)
- {
- if (boardInput[y,x] == 1)
- {
- for (int u = 0; u < attackVectors.GetLength(1); u++)
- {
- // left to right
- if (boardInput[y, u] == 1 && u != x)
- {
- return false;
- }
- // up and down
- if (boardInput[u, x] == 1 && u != y)
- {
- return false;
- }
- // y + u, x + u
- if (y + u != y && x + u != x
- && y + u >= 0 && x + u >= 0
- && y + u < attackVectors.GetLength(0)
- && x + u < attackVectors.GetLength(1)
- && boardInput[y + u, x + u] == 1)
- {
- return false;
- }
- // y - u, x - u
- if (y - u != y && x - u != x
- && y - u >= 0 && x - u >= 0
- && y - u < attackVectors.GetLength(0)
- && x - u < attackVectors.GetLength(1)
- && boardInput[y - u, x - u] == 1)
- {
- return false;
- }
- // y + u, x - u
- if (y + u != y && x - u != x
- && y + u >= 0 && x - u >= 0
- && y + u < attackVectors.GetLength(0)
- && x - u < attackVectors.GetLength(1)
- && boardInput[y + u, x - u] == 1)
- {
- return false;
- }
- // y - u, x + u
- if (y - u != y && x + u != x
- && y - u >= 0 && x + u >= 0
- && y - u < attackVectors.GetLength(0)
- && x + u < attackVectors.GetLength(1)
- && boardInput[y - u, x + u] == 1)
- {
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- static System.Collections.Generic.IEnumerable<char[]> ReadInput()
- {
- FileStream inputStream = null;
- StreamReader inputReader = null;
- Console.Write("Please enter the input file name: ");
- string fileName = Console.ReadLine();
- Console.WriteLine("Attempting to open '" + fileName + "' for reading.");
- int bytesRead = 0;
- try
- {
- inputStream = new FileStream(fileName, FileMode.Open);
- inputReader = new StreamReader(inputStream);
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception Thrown: " + e.GetType().ToString());
- bytesRead = -1;
- }
- if (inputStream == null || inputReader == null || bytesRead == -1)
- yield break;
- do
- {
- char[] buffer = new char[9];
- bytesRead = inputReader.Read(buffer, 0, 9);
- if (bytesRead > 0)
- yield return buffer;
- } while (bytesRead > 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement