So, in order to practice for future Competitions... I've printed out a few old UIL packets to go through. I noticed a few re-occurring problems that are somewhat difficult, but manageable. I find myself allotting too much time on these couple of problems instead of working on others...
There's quite a bit, so I understand if you're not willing to read through it all.. Any advice is greatly appreciated though!
To get to the point, here's what I'd like some advice on:
- Translating an Array
~ For Example, simulate a tetris game through a matrix of characters.. If the bottom row is filled, return a new array that has deleted the bottom row and set the remaining characters on the bottom row.
My Code:
public static char[][] removeBottom(char[][] matrix){ char[][] newArray = new char[matrix.length][matrix.length]; for(int row = 0; row < matrix.length; row++) for(int col = 0; col < matrix.length; col++) newArray[row][col] = '.'; for(int row = 1; row < matrix.length; row++) for(int col = 0; col < matrix.length; col++) newArray[row][col] = matrix[i-1][j]; return newArray; }
- Secondly, I come across Mazes
~ Here are some questions I come across.. Solve for the shortest path, only path, shortest path length.
My Code:
/* The following method only removes Dead Ends. Only can return shortest path when there is EXACTLY one path I will only show one method, but if this method returns true.. Another will fill in the Dead End. */ public static boolean containsDeadEnds(char[][] maze){ for(int row = 0; row < maze.length; row++){ for(int col = 0; col < maze[row].length; col++){ int deadEnds = 0; if(maze[i][j] == '.'){ if(maze[i-1][j] == '#') deadEnds++; if(maze[i+1][j] == '#') deadEnds++; if(maze[i][j-1] == '#') deadEnds++; if(maze[i][j+1] == '#') deadEnds++; if(deadEnds >= 3) return true; } deadEnds = 0; //Ensure Reset } } return false; }
How would you approach these problems?