Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public static int answer(int[] entrances, int[] exits, int[][] path) {
  2. if(entrances.length == 0 || exits.length == 0)
  3. return 0;
  4.  
  5. int[] max = new int[path.length]; // array for bunnies in each room
  6.  
  7. // set max bunnies of entrances to how much they can send through their corridors
  8. for(int i = 0; i < entrances.length; i++) {
  9. for(int j = 0; j < path[entrances[i]].length; j++) {
  10. if(path[entrances[i]][j] != 0)
  11. max[entrances[i]] += path[entrances[i]][j];
  12. }
  13. }
  14.  
  15. // calls recursive function to do the work
  16. for(int i = 0; i < entrances.length; i++)
  17. helper(entrances, exits, max, entrances[i], path);
  18.  
  19.  
  20. // takes all the bunnies in the exits and totals them
  21. int sum = 0;
  22. for(int i = 0; i < exits.length; i++)
  23. sum += max[exits[i]];
  24.  
  25. return sum;
  26. }
  27.  
  28. public static void helper(int[] ent, int[] exit, int[] max, int current, int[][] path) {
  29. if(checkIfExit(current, exit)) // if we reach an exit, return
  30. return;
  31.  
  32. for(int i = 0; i < path.length; i++) {
  33. if(checkIfEnt(i, ent)) // Skip if hallway goes back to entrance
  34. continue;
  35.  
  36. // send the bunnies through the corridors
  37. if(path[current][i] != 0 && max[current] != 0) { // If you have bunnies to send/there's a hallway
  38. if(max[current] >= path[current][i]) { // if more than enough bunnies in rooms, send how many can fit
  39. max[current] -= path[current][i];
  40. max[i] += path[current][i];
  41. path[current][i] = 0;
  42. helper(ent, exit, max, i, path);
  43. }
  44.  
  45. else { // if not enough, send whatever you have
  46. max[i] += max[current];
  47. path[current][i] -= max[current];
  48. max[current] = 0;
  49. helper(ent, exit, max, i, path);
  50. }
  51. }
  52. }
  53. }
  54.  
  55. public static boolean checkIfEnt(int floor, int[] ent) {
  56. for(int i = 0; i < ent.length; i++) {
  57. if(floor == ent[i])
  58. return true;
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. public static boolean checkIfExit(int floor, int[] exit) {
  65. for(int i = 0; i < exit.length; i++) {
  66. if(floor == exit[i])
  67. return true;
  68. }
  69.  
  70. return false;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement