Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class Main
- {
- //Directory with sample input data and answers
- private static final String dir = "8queens/data/secret/";
- //chess board
- static int board[][] = new int[8][8];
- //Array lists for input and answer files
- static List<File> inFiles = new ArrayList<File>();
- static List<File> ansFiles = new ArrayList<File>();
- //Initiliazes array lists of files
- public static void initFiles()
- {
- File folder = new File(dir);
- for(File f : folder.listFiles())
- {
- String ext = f.getName().substring(f.getName().lastIndexOf('.'));
- //If it's an input file containing board layout
- if(ext.contains("in"))
- inFiles.add(f);
- //It's an answer file containing the validity of its input
- else if(ext.contains("ans"))
- ansFiles.add(f);
- }
- }
- public static void main(String[] args) throws IOException
- {
- initFiles();
- for(int x = 0; x < inFiles.size(); x++)
- {
- //next file
- File input = inFiles.get(x);
- FileReader fr = new FileReader(input);
- BufferedReader br = new BufferedReader(fr);
- //Iterate over matrix, input file data
- for(int i = 0; i < 8; i++)
- {
- //Read first line
- String line = br.readLine();
- for(int j = 0; j < 8; j++)
- {
- //next char in line
- char c = line.charAt(j);
- //asterisks are queens, therefore 1's
- if(c == '*')
- board[i][j] = 1;
- //no queen means a 0
- else
- board[i][j] = 0;
- }
- }
- fr.close();
- br.close();
- //to assist breaking out of nested loop
- boolean keepGoing = true;
- for(int i = 0; i < 8; i++)
- {
- for(int j = 0; j < 8; j++)
- {
- //if we find a queen
- if(board[i][j] == 1)
- {
- //check if it is placed well
- if(!validChoice(i, j))
- {
- //if not, algorithm can stop
- keepGoing = false;
- break;
- }
- }
- }
- if(!keepGoing)
- break;
- }
- String myAnswer = null;
- if(keepGoing)
- myAnswer = "valid";
- else
- myAnswer = "invalid";
- File ans = ansFiles.get(x);
- fr = new FileReader(ans);
- br = new BufferedReader(fr);
- //get judges answer
- String answer = br.readLine();
- fr.close();
- br.close();
- //if our answers dont match, let me know which file
- if(!answer.contains(myAnswer))
- System.out.println(ans.getName());
- else
- System.out.println(myAnswer);
- }
- }
- private static boolean validChoice(int row, int column)
- {
- if(row > 7 || column > 7)
- return false;
- int rowSum = 0;
- int columnSum = 0;
- int diagnolSum1 = 0;
- int diagnolSum2 = 0;
- for(int i = 0; i < 8; i++)
- {
- //how many queens in the row?
- //should be 1 to be valid
- rowSum += board[row][i];
- //how many queens in column?
- //should b 1 to be valid;
- columnSum += board[i][column];
- }
- //down and right diagnol
- int currCol = column;
- for(int i = row + 1; i < 8; i++)
- {
- currCol++;
- if(currCol > 7)
- break;
- diagnolSum2 += board[i][currCol];
- }
- //up and left diagnol
- currCol = column;
- for(int i = row - 1; i >= 0; i--)
- {
- currCol--;
- if(currCol < 0)
- break;
- diagnolSum2 += board[i][currCol];
- }
- //down and left diagnol
- currCol = column;
- for(int i = row + 1; i < 8; i++)
- {
- currCol--;
- if(currCol < 0)
- break;
- diagnolSum1 += board[i][currCol];
- }
- //up and right diagnol
- currCol = column;
- for(int i = row - 1; i >= 0; i--)
- {
- currCol++;
- if(currCol > 7)
- break;
- diagnolSum1 += board[i][currCol];
- }
- if(rowSum <= 1 && columnSum <= 1 && diagnolSum1 < 1 && diagnolSum2 < 1)
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement