Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.LinkedList;
- class test4 {
- public static void main(String args[]) {
- //Start Time
- long start = System.currentTimeMillis();
- //Memory
- LinkedList<Integer> temp = new LinkedList<Integer>();
- LinkedList<Integer> temper = new LinkedList<Integer>();
- LinkedList<Integer> list = new LinkedList<Integer>();
- Random generator = new Random();
- int matrix[][] = new int[4][4];
- int picked[][] = new int[4][4];
- int repicked[][] = new int[4][4];
- int total=0;
- int curtotal=0;
- int colrow=0;
- int picker=0;
- int max=0;
- //Build Temporary Default Array.
- temper = new LinkedList<Integer>();
- temper.add(0);
- temper.add(1);
- temper.add(2);
- temper.add(3);
- temper.add(4);
- temper.add(5);
- temper.add(6);
- temper.add(7);
- //Number of tests to run
- for(int runs=0;runs<1000;runs++){
- //Sets per-run variables
- curtotal=0;
- //Builds the matrix and the picked list
- for(int i=0;i<4;i++)for(int j=0;j<4;j++){
- matrix[i][j]=generator.nextInt(16);
- picked[i][j]=0;
- repicked[i][j]=0;
- }
- //Solve the Matrix
- for(int a=0;a<8;a++)
- for(int b=0;b<7;b++)
- for(int c=0;c<6;c++)
- for(int d=0;d<5;d++)
- for(int e=0;e<4;e++)
- for(int f=0;f<3;f++)
- for(int g=0;g<2;g++){
- //Create a temp list...
- temp = new LinkedList<Integer>(temper);
- //Create an ordered list...
- list = new LinkedList<Integer>();
- list.add(temp.get(a));
- temp.remove(a);
- list.add(temp.get(b));
- temp.remove(b);
- list.add(temp.get(c));
- temp.remove(c);
- list.add(temp.get(d));
- temp.remove(d);
- list.add(temp.get(e));
- temp.remove(e);
- list.add(temp.get(f));
- temp.remove(f);
- list.add(temp.get(g));
- temp.remove(g);
- list.add(temp.get(0));
- //temp.remove(0);
- //Go through operations in order...
- while(list.size()>0){
- colrow = list.get(0);
- list.remove(0);
- picker=0;
- max=0;
- //Switch, if it's a column do this, else do that...
- switch (colrow){
- //Columns
- case 0:
- case 1:
- case 2:
- case 3:
- max=0;
- picker=5;
- for(int i=0;i<4;i++){
- if(max < matrix[i][colrow] && picked[i][colrow]!=1){
- max = matrix[i][colrow];
- picker = i;
- }
- }
- if(picker!=5){
- picked[picker][colrow]=1;
- total=total+matrix[picker][colrow];
- }
- break;
- //Rows
- case 4:
- case 5:
- case 6:
- case 7:
- max=0;
- picker=5;
- for(int i=0;i<4;i++){
- if(max < matrix[colrow-4][i] && picked[colrow-4][i]!=1){
- max = matrix[colrow-4][i];
- picker = i;
- }
- }
- if(picker!=5){
- picked[colrow-4][picker]=1;
- total=total+matrix[colrow-4][picker];
- }
- break;
- default:
- //Shouldn't happen...
- break;
- }
- }
- //Check Total and reset
- if(total > curtotal) curtotal=total;
- total=0;
- //Reset Picked
- for(int i=0;i<4;i++)for(int j=0;j<4;j++){
- picked[i][j]=0;
- }
- }
- System.out.println();
- System.out.println(curtotal);
- System.out.println();
- }
- //End Time
- long stop = System.currentTimeMillis();
- System.out.println("Finished at: "+(stop-start));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement