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