Created
April 7, 2012 00:45
-
-
Save lgo/2324315 to your computer and use it in GitHub Desktop.
In an attempt to see if the Matrix is solvable (http://www.drunkmenworkhere.org/129) and to learn recursion further - Saw the opportunity and shot for it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| public class Matrix { | |
| static int[][] matrix = new int[5][5]; | |
| static int max = 0; | |
| public static void main(String[] args) throws IOException { | |
| BufferedReader bfr = new BufferedReader(new FileReader("m.txt")); | |
| for (int i = 0; i < 5; i++) { | |
| String[] s = bfr.readLine().split(" "); | |
| for (int j = 0; j < 5; j++) { | |
| matrix[i][j] = Integer.parseInt(s[j]); | |
| } | |
| } | |
| for (int x = 0; x < 5; x++) { | |
| for (int y = 0; y < 5; y++) { | |
| recursion(x, y); | |
| } | |
| } | |
| System.out.println(max); | |
| } | |
| private static void recursion(int x, int y) { | |
| recursion(x, y, matrix[x][y], startTable(x, y), new int[]{ matrix[x][y], 0 , 0, 0, 0 }); | |
| } | |
| private static void recursion(int x, int y, int value, boolean[][] table, int[] steps) { | |
| if (value > max) max = value; | |
| if (value >= 100) { | |
| System.out.println(); | |
| for (int i = 0; i < 5; i++) { | |
| System.out.print(steps[i] + " "); | |
| } | |
| } | |
| for (int _x = 0; _x < 5; _x++) { | |
| for (int _y = 0; _y < 5; _y++) { | |
| if (table[_x][_y]) continue; | |
| else { | |
| for (int i = 0; i < 5; i++) { | |
| if (steps[i] == 0) { | |
| steps[i] = matrix[_x][_y]; | |
| break; | |
| } | |
| } | |
| recursion(_x, _y, value + matrix[_x][_y], fillTable(table, _x, _y), steps); | |
| } | |
| } | |
| } | |
| } | |
| private static boolean[][] startTable(int x, int y) { | |
| boolean[][] blarg = new boolean[5][5]; | |
| for (int i = 0; i < 5; i++) { | |
| for (int j = 0; j < 5; j++) { | |
| blarg[i][j] = false; | |
| } | |
| } | |
| for (int i = 0; i < 5; i++) { | |
| blarg[x][i] = true; | |
| } | |
| for (int i = 0; i < 5; i++) { | |
| blarg[i][y] = true; | |
| } | |
| return blarg; | |
| } | |
| private static boolean[][] fillTable(boolean[][] table, int x, int y) { | |
| for (int i = 0; i < 5; i++) { | |
| table[x][i] = true; | |
| } | |
| for (int i = 0; i < 5; i++) { | |
| table[i][y] = true; | |
| } | |
| return table; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment