Advertisement
Guest User

Untitled

a guest
Oct 27th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. package dpstandalone;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class DP237H {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. Scanner sc = null;
  13. try {
  14. sc = new Scanner(new File("src/dpstandalone/Text Inputs/237H"));
  15. } catch (FileNotFoundException e) {
  16. System.out.println("Input file not found!");
  17. System.exit(0);
  18. }
  19.  
  20. ArrayList<String> lines = new ArrayList<String>();
  21. while(sc.hasNextLine()){
  22. lines.add(sc.nextLine());
  23. }
  24.  
  25. String[][] grid = new String[lines.size()][lines.size()];
  26.  
  27. for(int i=0; i<lines.size(); i++){
  28. for(int j=0; j<lines.get(i).length(); j++){
  29. grid[i][j] = lines.get(i).charAt(j)+"";
  30. if(grid[i][j].equals("0"))
  31. grid[i][j] = "a";
  32. if(grid[i][j].equals("1"))
  33. grid[i][j] = "b";
  34. if(grid[i][j].equals("."))
  35. grid[i][j] = "0";
  36.  
  37. System.out.print(grid[i][j]);
  38. }
  39. System.out.println();
  40. }
  41.  
  42. String[] single = toSingleDimArray(grid);
  43. long nonfixedcount = 0;
  44. System.out.println();
  45. for(int i=0; i<single.length; i++){
  46. System.out.print(single[i]);
  47. if(single[i].equals("0") | single[i].equals("1")){
  48. nonfixedcount++;
  49. }
  50. }
  51.  
  52. //nonfixedcount = (long) Math.pow(2, nonfixedcount);
  53.  
  54. System.out.println();
  55.  
  56. long start = System.currentTimeMillis();
  57. int opcount = 0;
  58. String[] inc = binaryInc(single);
  59. while(!allOnes(inc) && !validArray(inc)){
  60. //while(!allOnes(inc)){
  61. inc = binaryInc(inc);
  62. //System.out.println();
  63. if(opcount%100000 == 0)
  64. //System.out.println(" ("+opcount+"/"+nonfixedcount+")");
  65. for(int j=0; j<inc.length; j++){
  66. //System.out.print(inc[j]);
  67. }
  68. //System.out.print(" ("+opcount+"/"+nonfixedcount+")");
  69. opcount++;
  70. }
  71. long end = System.currentTimeMillis();
  72.  
  73. System.out.println();
  74. System.out.println();
  75. System.out.println();
  76. System.out.println("Found solution after " + opcount + " operations: ");
  77. System.out.println("Total time: " + (end-start)+"ms");
  78. System.out.println("Time per operation: " + (double)1/((opcount)/(end-start)) +"ms");
  79. System.out.println();
  80. //0.002183406113537118
  81.  
  82.  
  83. String[][] multi = toMultiDimArray(convertfromfixed(single));
  84. for(int i=0; i<multi.length; i++){
  85. for(int j=0; j<multi.length; j++){
  86. System.out.print(multi[i][j]);
  87. }
  88. System.out.println();
  89. }
  90.  
  91. }
  92.  
  93. public static String[] toSingleDimArray(String[][] ar){
  94. String[] newarray = new String[ar.length*ar.length];
  95. for(int i=0; i<ar.length; i++){
  96. for(int j=0; j<ar.length; j++){
  97. newarray[ar.length*i+j] = ar[i][j];
  98. }
  99. }
  100.  
  101. return newarray;
  102. }
  103.  
  104. public static String[][] toMultiDimArray(String[] ar){
  105. String[][] newarray = new String[(int) Math.sqrt(ar.length)][(int) Math.sqrt(ar.length)];
  106. for(int i=0; i<newarray.length; i++){
  107. for(int j=0; j<newarray.length; j++){
  108. newarray[i][j] = ar[newarray.length*i+j];
  109. }
  110. }
  111.  
  112. return newarray;
  113. }
  114.  
  115. public static String[] binaryInc(String[] ar){
  116. //Increment as a reverse binary counter, ignoring a,b
  117. boolean zeroswitched = false;
  118. int pos = 0;
  119. while(!zeroswitched && pos < ar.length){
  120. if(ar[pos].equals("0")){
  121. ar[pos] = "1";
  122. zeroswitched = true;
  123. }
  124. else if(ar[pos].equals("1")){
  125. ar[pos] = "0";
  126. }
  127. pos++;
  128. }
  129. return ar;
  130. }
  131.  
  132. public static boolean allOnes(String[] ar){
  133. for(int i=0; i<ar.length; i++){
  134. if(ar[i].equals("0"))
  135. return false;
  136. }
  137. return true;
  138. }
  139.  
  140. public static boolean checkRows(String[][] ar){
  141. //False if:
  142. //- Two rows are identical
  143. //- There are more than 2 adjacent 1s or 0s in a row
  144.  
  145. ArrayList<String[]> previousrows = new ArrayList<String[]>();
  146. for(int i=0; i<ar.length; i++){
  147. String[] thisrow = ar[i];
  148. for(int j=0; j<thisrow.length-2; j++){
  149. if(thisrow[j].equals(thisrow[j+1]) && thisrow[j+1].equals(thisrow[j+2])){
  150. //System.out.println(" Failed adjacency row check on row " + i);
  151. return false;
  152. }
  153. }
  154. if(containsSameArray(previousrows, thisrow)){
  155. //System.out.println(" Duplicate row found for row " + i);
  156. return false;
  157. }
  158. previousrows.add(thisrow);
  159. }
  160.  
  161. return true;
  162. }
  163.  
  164. public static boolean checkCols(String[][] ar){
  165. //False if:
  166. //- Two cols are identical
  167. //- There are more than 2 adjacent 1s or 0s in a col
  168.  
  169. ArrayList<String[]> previouscols = new ArrayList<String[]>();
  170. for(int i=0; i<ar.length; i++){
  171. String[] thiscol = new String[ar.length];
  172. for(int k=0; k<ar.length; k++){
  173. thiscol[k]=ar[k][i];
  174. }
  175.  
  176. //{ {0123} {0123} {0123} {0123} } ar[0][0], ar[1][0], ...
  177. for(int j=0; j<thiscol.length-2; j++){
  178. //System.out.println(j + " " + (j+1) + " " + (j+2) + " in " + thiscol.length);
  179. if(thiscol[j].equals(thiscol[j+1]) && thiscol[j+1].equals(thiscol[j+2])){
  180. //System.out.println(" Failed adjacency col check on col " + i);
  181. return false;
  182. }
  183. }
  184. if(containsSameArray(previouscols, thiscol)){
  185. //System.out.println(" Duplicate col found for col " + i);
  186. return false;
  187. }
  188. previouscols.add(thiscol);
  189. }
  190.  
  191. return true;
  192. }
  193.  
  194. public static boolean containsSameArray(ArrayList<String[]> checkthis, String[] ar){
  195. if(checkthis.size() == 0){
  196. return false;
  197. }
  198.  
  199. for(int i=0; i<checkthis.size(); i++){
  200. boolean thissame = true;
  201. for(int j=0; j<checkthis.get(i).length; j++){
  202. if(!(checkthis.get(i)[j]).equals(ar[j])){
  203. thissame = false;
  204. }
  205. }
  206. if(thissame)
  207. return true;
  208. }
  209.  
  210. return false;
  211. }
  212.  
  213. public static boolean validArray(String[] ar_in){
  214. //Convert a and b to 0 and 1 respectives
  215. String[] ar = ar_in.clone();
  216. for(int i=0; i<ar.length; i++){
  217. if(ar[i].equals("a"))
  218. ar[i] = "0";
  219. if(ar[i].equals("b"))
  220. ar[i] = "1";
  221. }
  222. String[][] check = toMultiDimArray(ar);
  223. int[] rowcount = checkRowsNum(check);
  224. int[] colcount = checkColsNum(check);
  225. if(rowcount[0] != colcount[0]){
  226. return false;
  227. }
  228. else{
  229. if(rowcount[1] == 0)
  230. return false;
  231. if(colcount[1] == 0)
  232. return false;
  233. //Otherwise all cols = all rows = good, proceed!
  234. }
  235. if(checkCols(check) && checkRows(check)){
  236. return true;
  237. }
  238. String debugcheck = "";
  239.  
  240. return false;
  241. }
  242.  
  243. public static String[] convertfromfixed(String[] ar_in){
  244. String[] ar = ar_in.clone();
  245. for(int i=0; i<ar.length; i++){
  246. if(ar[i].equals("a"))
  247. ar[i] = "0";
  248. if(ar[i].equals("b"))
  249. ar[i] = "1";
  250. }
  251.  
  252. return ar;
  253. }
  254.  
  255.  
  256. public static int[] checkRowsNum(String[][] ar){
  257. int[] ret = {-1,-1};
  258. for(int i=0; i<ar.length; i++){
  259. String[] thisrow = ar[i];
  260. int[] counts = {0,0};
  261. for(int j=0; j<thisrow.length; j++){
  262. counts[Integer.parseInt(thisrow[j])]++;
  263. }
  264. boolean zosame;
  265. if(counts[0] == counts[1]){
  266. zosame = true;
  267. }
  268. else{
  269. zosame = false;
  270. }
  271.  
  272. if(i == 0){
  273. if(!zosame){
  274. ret[0] = counts[0];
  275. ret[1] = 0;
  276. return ret;
  277. }
  278. else{
  279. ret[0] = counts[0];
  280. ret[1] = 1;
  281. }
  282. }
  283. else{
  284. if(!zosame){
  285. ret[0] = counts[0];
  286. ret[1] = 0;
  287. return ret;
  288. }
  289. else{
  290. if(ret[0] == counts [0]){
  291. //Do nothing, value remains the same
  292. }
  293. else{
  294. ret[0] = counts[0];
  295. ret[1] = 0;
  296. return ret;
  297. }
  298. }
  299. }
  300. }
  301.  
  302. return ret; //Will contains the number of 0s and 1s in each row, and a 1 to confirm same number
  303. }
  304.  
  305. public static int[] checkColsNum(String[][] ar){
  306. int[] ret = {-1,-1};
  307. for(int i=0; i<ar.length; i++){
  308. String[] thiscol = new String[ar.length];
  309. for(int k=0; k<ar.length; k++){
  310. thiscol[k]=ar[k][i];
  311. }
  312. int[] counts = {0,0};
  313. for(int j=0; j<thiscol.length; j++){
  314. counts[Integer.parseInt(thiscol[j])]++;
  315. }
  316. boolean zosame;
  317. if(counts[0] == counts[1]){
  318. zosame = true;
  319. }
  320. else{
  321. zosame = false;
  322. }
  323.  
  324. if(i == 0){
  325. if(!zosame){
  326. ret[0] = counts[0];
  327. ret[1] = 0;
  328. return ret;
  329. }
  330. else{
  331. ret[0] = counts[0];
  332. ret[1] = 1;
  333. }
  334. }
  335. else{
  336. if(!zosame){
  337. ret[0] = counts[0];
  338. ret[1] = 0;
  339. return ret;
  340. }
  341. else{
  342. if(ret[0] == counts [0]){
  343. //Do nothing, value remains the same
  344. }
  345. else{
  346. ret[0] = counts[0];
  347. ret[1] = 0;
  348. return ret;
  349. }
  350. }
  351. }
  352. }
  353.  
  354. return ret; //Will contains the number of 0s and 1s in each row, and a 1 to confirm same number
  355. }
  356.  
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement