Advertisement
Guest User

Eight Queens

a guest
Oct 1st, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.67 KB | None | 0 0
  1.    
  2.  
  3.     import java.io.BufferedReader;
  4.     import java.io.File;
  5.     import java.io.FileReader;
  6.     import java.io.IOException;
  7.     import java.util.ArrayList;
  8.     import java.util.List;
  9.      
  10.      
  11.     public class Main
  12.     {
  13.             //Directory with sample input data and answers
  14.             private static final String dir = "8queens/data/secret/";
  15.      
  16.             //chess board
  17.             static int board[][] = new int[8][8];
  18.            
  19.             //Array lists for input and answer files
  20.             static List<File> inFiles = new ArrayList<File>();
  21.             static List<File> ansFiles = new ArrayList<File>();
  22.      
  23.             //Initiliazes array lists of files
  24.             public static void initFiles()
  25.             {
  26.                     File folder = new File(dir);
  27.      
  28.                     for(File f : folder.listFiles())
  29.                     {
  30.                             String ext = f.getName().substring(f.getName().lastIndexOf('.'));
  31.                             //If it's an input file containing board layout
  32.                             if(ext.contains("in"))
  33.                                     inFiles.add(f);
  34.                             //It's an answer file containing the validity of its input
  35.                             else if(ext.contains("ans"))
  36.                                     ansFiles.add(f);
  37.                     }
  38.             }
  39.      
  40.             public static void main(String[] args) throws IOException
  41.             {
  42.                     initFiles();
  43.                     for(int x = 0; x < inFiles.size(); x++)
  44.                     {
  45.                             //next file
  46.                             File input = inFiles.get(x);
  47.                            
  48.                             FileReader fr = new FileReader(input);
  49.                             BufferedReader br = new BufferedReader(fr);
  50.      
  51.                             //Iterate over matrix, input file data
  52.                             for(int i = 0; i < 8; i++)
  53.                             {
  54.                                     //Read first line
  55.                                     String line = br.readLine();
  56.                                     for(int j = 0; j < 8; j++)
  57.                                     {
  58.                                             //next char in line
  59.                                             char c = line.charAt(j);
  60.                                             //asterisks are queens, therefore 1's
  61.                                             if(c == '*')
  62.                                                     board[i][j] = 1;
  63.                                             //no queen means a 0
  64.                                             else
  65.                                                     board[i][j] = 0;
  66.                                     }
  67.                             }
  68.      
  69.                             fr.close();
  70.                             br.close();
  71.      
  72.                             //to assist breaking out of nested loop
  73.                             boolean keepGoing = true;
  74.      
  75.                             for(int i = 0; i < 8; i++)
  76.                             {
  77.                                     for(int j = 0; j < 8; j++)
  78.                                     {
  79.                                             //if we find a queen
  80.                                             if(board[i][j] == 1)
  81.                                             {
  82.                                                     //check if it is placed well
  83.                                                     if(!validChoice(i, j))
  84.                                                     {
  85.                                                             //if not, algorithm can stop
  86.                                                             keepGoing = false;
  87.                                                             break;
  88.                                                     }
  89.                                             }
  90.                                     }
  91.                                     if(!keepGoing)
  92.                                             break;
  93.                             }
  94.                            
  95.                             String myAnswer = null;
  96.      
  97.                             if(keepGoing)
  98.                                     myAnswer = "valid";
  99.                             else
  100.                                     myAnswer = "invalid";
  101.      
  102.                             File ans = ansFiles.get(x);
  103.                            
  104.                             fr = new FileReader(ans);
  105.                             br = new BufferedReader(fr);
  106.                            
  107.                             //get judges answer
  108.                             String answer = br.readLine();
  109.                            
  110.                             fr.close();
  111.                             br.close();
  112.      
  113.                             //if our answers dont match, let me know which file
  114.                             if(!answer.contains(myAnswer))
  115.                                     System.out.println(ans.getName());
  116.                             else
  117.                                     System.out.println(myAnswer);
  118.                     }
  119.      
  120.             }
  121.      
  122.             private static boolean validChoice(int row, int column)
  123.             {
  124.                     if(row > 7 || column > 7)
  125.                             return false;
  126.      
  127.                     int rowSum = 0;
  128.                     int columnSum = 0;
  129.                     int diagnolSum1 = 0;
  130.                     int diagnolSum2 = 0;
  131.      
  132.                     for(int i = 0; i < 8; i++)
  133.                     {
  134.                             //how many queens in the row?
  135.                             //should be 1 to be valid
  136.                             rowSum += board[row][i];
  137.                             //how many queens in column?
  138.                             //should b 1 to be valid;
  139.                             columnSum += board[i][column];
  140.                     }
  141.      
  142.                     //down and right diagnol
  143.                     int currCol = column;
  144.                     for(int i = row + 1; i < 8; i++)
  145.                     {
  146.                             currCol++;
  147.                             if(currCol > 7)
  148.                                     break;
  149.                             diagnolSum2 += board[i][currCol];
  150.                     }
  151.      
  152.                     //up and left diagnol
  153.                     currCol = column;
  154.                     for(int i = row - 1; i >= 0; i--)
  155.                     {
  156.                             currCol--;
  157.                             if(currCol < 0)
  158.                                     break;
  159.                             diagnolSum2 += board[i][currCol];
  160.                     }
  161.      
  162.                     //down and left diagnol
  163.                     currCol = column;
  164.                     for(int i = row + 1; i < 8; i++)
  165.                     {
  166.                             currCol--;
  167.                             if(currCol < 0)
  168.                                     break;
  169.                             diagnolSum1 += board[i][currCol];
  170.                     }
  171.      
  172.                     //up and right diagnol
  173.                     currCol = column;
  174.                     for(int i = row - 1; i >= 0; i--)
  175.                     {
  176.                             currCol++;
  177.                             if(currCol > 7)
  178.                                     break;
  179.                             diagnolSum1 += board[i][currCol];
  180.                     }
  181.                    
  182.                     if(rowSum <= 1 && columnSum <= 1 && diagnolSum1 < 1 && diagnolSum2 < 1)
  183.                             return true;
  184.      
  185.                     return false;
  186.             }
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement