|
| 1 | +import java.io.*; |
| 2 | +//import java.util.*; |
| 3 | +import modules.PrettyPrinter; |
| 4 | +public class cowtiper { |
| 5 | + private static int[][] field; |
| 6 | + private static int N; |
| 7 | + private static boolean patch; |
| 8 | + public static int c(char bit) { |
| 9 | + switch(bit) { |
| 10 | + case '1': |
| 11 | + patch=false; |
| 12 | + return 1; |
| 13 | + case '0': |
| 14 | + return 0; |
| 15 | + default: |
| 16 | + return -1; |
| 17 | + } |
| 18 | + } |
| 19 | + public static int sum(int[][] a) { |
| 20 | + /* |
| 21 | + * Finds total amount of cows not tipped over |
| 22 | + */ |
| 23 | + int out=0; |
| 24 | + for(int i=0;i<N;i++) { |
| 25 | + for(int j=0;j<N;j++) { |
| 26 | + if(a[i][j]==0) { |
| 27 | + out++; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + return out; |
| 32 | + } |
| 33 | + public static int[][] invert(int[][] a){ |
| 34 | + for(int i=0;i<N;i++) { |
| 35 | + for(int j=0;j<N;j++) { |
| 36 | + |
| 37 | + if(a[i][j]==1) { |
| 38 | + a[i][j]=0; |
| 39 | + }else if(a[i][j]==0) { |
| 40 | + a[i][j]=1; |
| 41 | + } |
| 42 | + |
| 43 | + } |
| 44 | + } |
| 45 | + return a; |
| 46 | + } |
| 47 | + public static int[][] invert(int[][] a,int x,int y){ |
| 48 | + for(int i=0;i<x;i++) { |
| 49 | + for(int j=0;j<y;j++) { |
| 50 | + System.out.println("Invert X:"+i+" Y:"+j+""); |
| 51 | + |
| 52 | + if(a[i][j]==1) { |
| 53 | + a[i][j]=0; |
| 54 | + }else if(a[i][j]==0) { |
| 55 | + a[i][j]=1; |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + return a; |
| 61 | + } |
| 62 | + public static int[][] best(int[][] a){ |
| 63 | + int temp=sum(a); |
| 64 | + if(temp<((N*N)-temp)) { |
| 65 | + a=invert(a); |
| 66 | + } |
| 67 | + return a; |
| 68 | + } |
| 69 | + public static int[][] best_NS(int[][] a){ |
| 70 | + int temp=sum(a); |
| 71 | + if(temp<((N*N)-temp)) { |
| 72 | + a=invert(a); |
| 73 | + } |
| 74 | + return a; |
| 75 | + } |
| 76 | + public static int[][] best(int[][] a,int x,int y){ |
| 77 | + int temp=sum(a); |
| 78 | + if(temp<((N*N)-temp)) { |
| 79 | + a=invert(a,x,y); |
| 80 | + } |
| 81 | + return a; |
| 82 | + } |
| 83 | + public static int[][] best_NS(int[][] a,int x,int y){ |
| 84 | + int temp=sum(a); |
| 85 | + if(temp<((N*N)-temp)) { |
| 86 | + a=invert(a,x,y); |
| 87 | + } |
| 88 | + return a; |
| 89 | + } |
| 90 | + public static void showfield() { |
| 91 | + String[][] ppa=new String[N][N]; |
| 92 | + //field=best(field); |
| 93 | + PrettyPrinter pp=new PrettyPrinter(System.out); |
| 94 | + for(int i = 0; i < N; i++) |
| 95 | + { |
| 96 | + for(int j = 0; j < N; j++) |
| 97 | + ppa[i][j] = Integer.toString(field[i][j]); |
| 98 | + } |
| 99 | + pp.print(ppa); |
| 100 | + } |
| 101 | + |
| 102 | + public static int hx(int[][] a) { |
| 103 | + int l=0; |
| 104 | + for(int i=0;i<N;i++) { |
| 105 | + for(int j=0;j<N;j++) { |
| 106 | + if(a[i][j]==1) { |
| 107 | + if(i>l) { |
| 108 | + l=i; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return l; |
| 114 | + } |
| 115 | + public static int hy(int[][] a) { |
| 116 | + int l=-1; |
| 117 | + for(int i=0;i<N;i++) { |
| 118 | + for(int j=0;j<N;j++) { |
| 119 | + if(a[i][j]==1) { |
| 120 | + if(j>l) { |
| 121 | + l=j; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return l; |
| 127 | + } |
| 128 | + public static void main(String[] args) throws Exception{ |
| 129 | + // TODO Auto-generated method stub |
| 130 | + BufferedReader f=new BufferedReader(new FileReader("5(1).in")); |
| 131 | + N = Integer.parseInt(f.readLine()); |
| 132 | + field = new int[N][N]; |
| 133 | + |
| 134 | + String temp; |
| 135 | + patch = true; |
| 136 | + for(int i=0;i<N;i++) { |
| 137 | + temp=f.readLine(); |
| 138 | + for(int j=0;j<N;j++) { |
| 139 | + field[i][j]=c(temp.charAt(j)); |
| 140 | + //Integer.parseInt(Character.toString(temp.charAt(j))); |
| 141 | + } |
| 142 | + } |
| 143 | + f.close(); |
| 144 | + //Begin debug zone |
| 145 | + showfield(); |
| 146 | + String[][] ppa=new String[N][N]; |
| 147 | + |
| 148 | + field=best(field); // DO NOT REMOVE |
| 149 | + |
| 150 | + PrettyPrinter pp=new PrettyPrinter(System.out); |
| 151 | + for(int i = 0; i < N; i++) |
| 152 | + { |
| 153 | + for(int j = 0; j < N; j++) |
| 154 | + ppa[i][j] = Integer.toString(field[i][j]); |
| 155 | + } |
| 156 | + pp.print(ppa); |
| 157 | + |
| 158 | + //End debug zone |
| 159 | + int test=0; |
| 160 | + //if(sum(field)==(N*N)) { |
| 161 | + test=0; |
| 162 | + //}else { |
| 163 | + test=1; |
| 164 | + |
| 165 | + while(sum(field)!=(N*N)) { |
| 166 | + //if() |
| 167 | + showfield(); |
| 168 | + System.out.println(hx(field)); |
| 169 | + System.out.println(hy(field)); |
| 170 | + field=invert(field,hx(field)+1,hy(field)+1); |
| 171 | + showfield();//Debug |
| 172 | + //Thread.sleep(500); |
| 173 | + test++; |
| 174 | + if(test>10000) { |
| 175 | + throw new Exception("Auto terminate"); |
| 176 | + } |
| 177 | + } |
| 178 | + //} |
| 179 | + |
| 180 | + PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("cowtip.out"))); |
| 181 | + if(patch) {test=0;} |
| 182 | + pw.println(test); |
| 183 | + pw.flush(); |
| 184 | + pw.close(); |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments