Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Collections;
  8.  
  9. namespace EightQueens
  10. {
  11.     public class NoMoreTokensException : Exception
  12.     {
  13.     }
  14.  
  15.     public class Tokenizer
  16.     {
  17.         string[] tokens = new string[0];
  18.         private int pos;
  19.         StreamReader reader;
  20.  
  21.         public Tokenizer(Stream inStream)
  22.         {
  23.             var bs = new BufferedStream(inStream);
  24.             reader = new StreamReader(bs);
  25.         }
  26.  
  27.         public Tokenizer()
  28.             : this(Console.OpenStandardInput())
  29.         {
  30.             // Nothing more to do
  31.         }
  32.  
  33.         private string PeekNext()
  34.         {
  35.             if (pos < 0)
  36.                 // pos < 0 indicates that there are no more tokens
  37.                 return null;
  38.             if (pos < tokens.Length)
  39.             {
  40.                 if (tokens[pos].Length == 0)
  41.                 {
  42.                     ++pos;
  43.                     return PeekNext();
  44.                 }
  45.                 return tokens[pos];
  46.             }
  47.             string line = reader.ReadLine();
  48.             if (line == null)
  49.             {
  50.                 // There is no more data to read
  51.                 pos = -1;
  52.                 return null;
  53.             }
  54.             // Split the line that was read on white space characters
  55.             tokens = line.Split(null);
  56.             pos = 0;
  57.             return PeekNext();
  58.         }
  59.  
  60.         public bool HasNext()
  61.         {
  62.             return (PeekNext() != null);
  63.         }
  64.  
  65.         public string Next()
  66.         {
  67.             string next = PeekNext();
  68.             if (next == null)
  69.                 throw new NoMoreTokensException();
  70.             ++pos;
  71.             return next;
  72.         }
  73.     }
  74.  
  75.  
  76.     public class Scanner : Tokenizer
  77.     {
  78.  
  79.         public int NextInt()
  80.         {
  81.             return int.Parse(Next());
  82.         }
  83.  
  84.         public long NextLong()
  85.         {
  86.             return long.Parse(Next());
  87.         }
  88.  
  89.         public float NextFloat()
  90.         {
  91.             return float.Parse(Next());
  92.         }
  93.  
  94.         public double NextDouble()
  95.         {
  96.             return double.Parse(Next());
  97.         }
  98.     }
  99.  
  100.  
  101.     public class BufferedStdoutWriter : StreamWriter
  102.     {
  103.         public BufferedStdoutWriter()
  104.             : base(new BufferedStream(Console.OpenStandardOutput()))
  105.         {
  106.         }
  107.     }
  108.  
  109.     class Program
  110.     {
  111.         static void Main(string[] args)
  112.         {
  113.             try
  114.             {
  115.                
  116.                 BufferedStdoutWriter writer = new BufferedStdoutWriter();
  117.                 int[,] boardInput = new int[8, 8];
  118.                 string[] lines = new string[8];
  119.                 Tokenizer tokens = new Tokenizer();
  120.                 for (int y = 0; y < 8; y++)
  121.                 {
  122.                     lines[y] = tokens.Next();
  123.  
  124.                     char[] line = lines[y].ToCharArray();
  125.  
  126.                     if (y > boardInput.GetLength(0))
  127.                         break;
  128.  
  129.                     for (int x = 0; x < boardInput.GetLength(1); x++)
  130.                     {
  131.                         if (line[x].Equals('*'))
  132.                             boardInput[y, x] = 1;
  133.                         else
  134.                             boardInput[y, x] = 0;
  135.                     }
  136.                 }
  137.  
  138.                 if (IsBoardValid(boardInput))
  139.                     writer.Write("valid");
  140.                 else
  141.                     writer.Write("invalid");
  142.  
  143.                 writer.Flush();
  144.                
  145.             }
  146.             catch
  147.             {
  148.                 //writer.WriteLine("invalid");
  149.             }
  150.             // Console.ReadLine();
  151.         }
  152.  
  153.         static bool IsBoardValid(int[,] boardInput)
  154.         {
  155.             int[,] attackVectors = new int[boardInput.GetLength(0), boardInput.GetLength(1)];
  156.  
  157.             for (int y = 0; y < attackVectors.GetLength(0); y++)
  158.             {
  159.                 for (int x = 0; x < attackVectors.GetLength(1); x++)
  160.                 {
  161.                    
  162.                     if (boardInput[y,x] == 1)
  163.                     {
  164.                        
  165.                         for (int u = 0; u < attackVectors.GetLength(1); u++)
  166.                         {
  167.                             // left to right
  168.                             if (boardInput[y, u] == 1 && u != x)
  169.                             {
  170.                                 return false;
  171.                             }
  172.                    
  173.                             // up and down
  174.                             if (boardInput[u, x] == 1 && u != y)
  175.                             {
  176.                                 return false;
  177.                             }
  178.                      
  179.  
  180.                             // y + u, x + u
  181.                             if (y + u != y && x + u != x
  182.                                 && y + u >= 0 && x + u >= 0
  183.                                 && y + u < attackVectors.GetLength(0)
  184.                                 && x + u < attackVectors.GetLength(1)
  185.                                 && boardInput[y + u, x + u] == 1)
  186.                             {
  187.                                 return false;
  188.                             }
  189.                  
  190.                             // y - u, x - u
  191.                             if (y - u != y && x - u != x
  192.                                 && y - u >= 0 && x - u >= 0
  193.                                 && y - u < attackVectors.GetLength(0)
  194.                                 && x - u < attackVectors.GetLength(1)
  195.                                 && boardInput[y - u, x - u] == 1)
  196.                             {
  197.                                 return false;
  198.                             }
  199.  
  200.                             // y + u, x - u
  201.                             if (y + u != y && x - u != x
  202.                                 && y + u >= 0 && x - u >= 0
  203.                                 && y + u < attackVectors.GetLength(0)
  204.                                 && x - u < attackVectors.GetLength(1)
  205.                                 && boardInput[y + u, x - u] == 1)
  206.                             {
  207.                                 return false;
  208.                             }
  209.  
  210.                             // y - u, x + u
  211.                             if (y - u != y && x + u != x
  212.                                 && y - u >= 0 && x + u >= 0
  213.                                 && y - u < attackVectors.GetLength(0)
  214.                                 && x + u < attackVectors.GetLength(1)
  215.                                 && boardInput[y - u, x + u] == 1)
  216.                             {
  217.                                 return false;
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.             }        
  223.  
  224.             return true;
  225.         }
  226.  
  227.         static System.Collections.Generic.IEnumerable<char[]> ReadInput()
  228.         {
  229.             FileStream inputStream = null;
  230.             StreamReader inputReader = null;
  231.  
  232.             Console.Write("Please enter the input file name: ");
  233.             string fileName = Console.ReadLine();
  234.             Console.WriteLine("Attempting to open '" + fileName + "' for reading.");
  235.            
  236.             int bytesRead = 0;
  237.  
  238.             try
  239.             {
  240.                 inputStream = new FileStream(fileName, FileMode.Open);
  241.                 inputReader = new StreamReader(inputStream);
  242.             }
  243.             catch (Exception e)
  244.             {
  245.                 Console.WriteLine("Exception Thrown: " + e.GetType().ToString());
  246.                 bytesRead = -1;
  247.             }
  248.  
  249.             if (inputStream == null || inputReader == null || bytesRead == -1)
  250.                 yield break;
  251.  
  252.             do
  253.             {
  254.                 char[] buffer = new char[9];
  255.                 bytesRead = inputReader.Read(buffer, 0, 9);
  256.                 if (bytesRead > 0)
  257.                     yield return buffer;
  258.             } while (bytesRead > 0);
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement