Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dpstandalone;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class DP237H {
- public static void main(String[] args) {
- Scanner sc = null;
- try {
- sc = new Scanner(new File("src/dpstandalone/Text Inputs/237H"));
- } catch (FileNotFoundException e) {
- System.out.println("Input file not found!");
- System.exit(0);
- }
- ArrayList<String> lines = new ArrayList<String>();
- while(sc.hasNextLine()){
- lines.add(sc.nextLine());
- }
- String[][] grid = new String[lines.size()][lines.size()];
- for(int i=0; i<lines.size(); i++){
- for(int j=0; j<lines.get(i).length(); j++){
- grid[i][j] = lines.get(i).charAt(j)+"";
- if(grid[i][j].equals("0"))
- grid[i][j] = "a";
- if(grid[i][j].equals("1"))
- grid[i][j] = "b";
- if(grid[i][j].equals("."))
- grid[i][j] = "0";
- System.out.print(grid[i][j]);
- }
- System.out.println();
- }
- String[] single = toSingleDimArray(grid);
- long nonfixedcount = 0;
- System.out.println();
- for(int i=0; i<single.length; i++){
- System.out.print(single[i]);
- if(single[i].equals("0") | single[i].equals("1")){
- nonfixedcount++;
- }
- }
- //nonfixedcount = (long) Math.pow(2, nonfixedcount);
- System.out.println();
- long start = System.currentTimeMillis();
- int opcount = 0;
- String[] inc = binaryInc(single);
- while(!allOnes(inc) && !validArray(inc)){
- //while(!allOnes(inc)){
- inc = binaryInc(inc);
- //System.out.println();
- if(opcount%100000 == 0)
- //System.out.println(" ("+opcount+"/"+nonfixedcount+")");
- for(int j=0; j<inc.length; j++){
- //System.out.print(inc[j]);
- }
- //System.out.print(" ("+opcount+"/"+nonfixedcount+")");
- opcount++;
- }
- long end = System.currentTimeMillis();
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("Found solution after " + opcount + " operations: ");
- System.out.println("Total time: " + (end-start)+"ms");
- System.out.println("Time per operation: " + (double)1/((opcount)/(end-start)) +"ms");
- System.out.println();
- //0.002183406113537118
- String[][] multi = toMultiDimArray(convertfromfixed(single));
- for(int i=0; i<multi.length; i++){
- for(int j=0; j<multi.length; j++){
- System.out.print(multi[i][j]);
- }
- System.out.println();
- }
- }
- public static String[] toSingleDimArray(String[][] ar){
- String[] newarray = new String[ar.length*ar.length];
- for(int i=0; i<ar.length; i++){
- for(int j=0; j<ar.length; j++){
- newarray[ar.length*i+j] = ar[i][j];
- }
- }
- return newarray;
- }
- public static String[][] toMultiDimArray(String[] ar){
- String[][] newarray = new String[(int) Math.sqrt(ar.length)][(int) Math.sqrt(ar.length)];
- for(int i=0; i<newarray.length; i++){
- for(int j=0; j<newarray.length; j++){
- newarray[i][j] = ar[newarray.length*i+j];
- }
- }
- return newarray;
- }
- public static String[] binaryInc(String[] ar){
- //Increment as a reverse binary counter, ignoring a,b
- boolean zeroswitched = false;
- int pos = 0;
- while(!zeroswitched && pos < ar.length){
- if(ar[pos].equals("0")){
- ar[pos] = "1";
- zeroswitched = true;
- }
- else if(ar[pos].equals("1")){
- ar[pos] = "0";
- }
- pos++;
- }
- return ar;
- }
- public static boolean allOnes(String[] ar){
- for(int i=0; i<ar.length; i++){
- if(ar[i].equals("0"))
- return false;
- }
- return true;
- }
- public static boolean checkRows(String[][] ar){
- //False if:
- //- Two rows are identical
- //- There are more than 2 adjacent 1s or 0s in a row
- ArrayList<String[]> previousrows = new ArrayList<String[]>();
- for(int i=0; i<ar.length; i++){
- String[] thisrow = ar[i];
- for(int j=0; j<thisrow.length-2; j++){
- if(thisrow[j].equals(thisrow[j+1]) && thisrow[j+1].equals(thisrow[j+2])){
- //System.out.println(" Failed adjacency row check on row " + i);
- return false;
- }
- }
- if(containsSameArray(previousrows, thisrow)){
- //System.out.println(" Duplicate row found for row " + i);
- return false;
- }
- previousrows.add(thisrow);
- }
- return true;
- }
- public static boolean checkCols(String[][] ar){
- //False if:
- //- Two cols are identical
- //- There are more than 2 adjacent 1s or 0s in a col
- ArrayList<String[]> previouscols = new ArrayList<String[]>();
- for(int i=0; i<ar.length; i++){
- String[] thiscol = new String[ar.length];
- for(int k=0; k<ar.length; k++){
- thiscol[k]=ar[k][i];
- }
- //{ {0123} {0123} {0123} {0123} } ar[0][0], ar[1][0], ...
- for(int j=0; j<thiscol.length-2; j++){
- //System.out.println(j + " " + (j+1) + " " + (j+2) + " in " + thiscol.length);
- if(thiscol[j].equals(thiscol[j+1]) && thiscol[j+1].equals(thiscol[j+2])){
- //System.out.println(" Failed adjacency col check on col " + i);
- return false;
- }
- }
- if(containsSameArray(previouscols, thiscol)){
- //System.out.println(" Duplicate col found for col " + i);
- return false;
- }
- previouscols.add(thiscol);
- }
- return true;
- }
- public static boolean containsSameArray(ArrayList<String[]> checkthis, String[] ar){
- if(checkthis.size() == 0){
- return false;
- }
- for(int i=0; i<checkthis.size(); i++){
- boolean thissame = true;
- for(int j=0; j<checkthis.get(i).length; j++){
- if(!(checkthis.get(i)[j]).equals(ar[j])){
- thissame = false;
- }
- }
- if(thissame)
- return true;
- }
- return false;
- }
- public static boolean validArray(String[] ar_in){
- //Convert a and b to 0 and 1 respectives
- String[] ar = ar_in.clone();
- for(int i=0; i<ar.length; i++){
- if(ar[i].equals("a"))
- ar[i] = "0";
- if(ar[i].equals("b"))
- ar[i] = "1";
- }
- String[][] check = toMultiDimArray(ar);
- int[] rowcount = checkRowsNum(check);
- int[] colcount = checkColsNum(check);
- if(rowcount[0] != colcount[0]){
- return false;
- }
- else{
- if(rowcount[1] == 0)
- return false;
- if(colcount[1] == 0)
- return false;
- //Otherwise all cols = all rows = good, proceed!
- }
- if(checkCols(check) && checkRows(check)){
- return true;
- }
- String debugcheck = "";
- return false;
- }
- public static String[] convertfromfixed(String[] ar_in){
- String[] ar = ar_in.clone();
- for(int i=0; i<ar.length; i++){
- if(ar[i].equals("a"))
- ar[i] = "0";
- if(ar[i].equals("b"))
- ar[i] = "1";
- }
- return ar;
- }
- public static int[] checkRowsNum(String[][] ar){
- int[] ret = {-1,-1};
- for(int i=0; i<ar.length; i++){
- String[] thisrow = ar[i];
- int[] counts = {0,0};
- for(int j=0; j<thisrow.length; j++){
- counts[Integer.parseInt(thisrow[j])]++;
- }
- boolean zosame;
- if(counts[0] == counts[1]){
- zosame = true;
- }
- else{
- zosame = false;
- }
- if(i == 0){
- if(!zosame){
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- else{
- ret[0] = counts[0];
- ret[1] = 1;
- }
- }
- else{
- if(!zosame){
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- else{
- if(ret[0] == counts [0]){
- //Do nothing, value remains the same
- }
- else{
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- }
- }
- }
- return ret; //Will contains the number of 0s and 1s in each row, and a 1 to confirm same number
- }
- public static int[] checkColsNum(String[][] ar){
- int[] ret = {-1,-1};
- for(int i=0; i<ar.length; i++){
- String[] thiscol = new String[ar.length];
- for(int k=0; k<ar.length; k++){
- thiscol[k]=ar[k][i];
- }
- int[] counts = {0,0};
- for(int j=0; j<thiscol.length; j++){
- counts[Integer.parseInt(thiscol[j])]++;
- }
- boolean zosame;
- if(counts[0] == counts[1]){
- zosame = true;
- }
- else{
- zosame = false;
- }
- if(i == 0){
- if(!zosame){
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- else{
- ret[0] = counts[0];
- ret[1] = 1;
- }
- }
- else{
- if(!zosame){
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- else{
- if(ret[0] == counts [0]){
- //Do nothing, value remains the same
- }
- else{
- ret[0] = counts[0];
- ret[1] = 0;
- return ret;
- }
- }
- }
- }
- return ret; //Will contains the number of 0s and 1s in each row, and a 1 to confirm same number
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement