Advertisement
claukiller

Untitled

May 4th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. package labs.course15_16.lab5dynamic.roadsInCity;
  2.  
  3. /**
  4. * The complexity of the algorithm is quadratic because we have to iterate
  5. * through all the rows and columns of the map in order to fill it.
  6. * @author UO244826
  7. *
  8. */
  9.  
  10. public class RoadsInCity {
  11.  
  12. //First i initialize the "map" and variables for the rows and columns
  13. long road [][];
  14. int x;
  15. int y;
  16.  
  17. //just the constructor
  18. public RoadsInCity(int x, int y) {
  19. this.road = new long [x][y];
  20. this.x = x;
  21. this.y = y;
  22. }
  23.  
  24. //we add the barrier wherever we want as a -1
  25. public void addBarrier(int x, int y) {
  26. road[x][y] = -1;
  27.  
  28. }
  29.  
  30. public void interchange(int beginX,int beginY,int endX,int endY){
  31.  
  32. int auxbeginX= beginX;
  33. int auxendX = endX;
  34. int auxbeginY = beginY;
  35. int auxendY = endY;
  36. if (beginX < endX || beginY > endY )
  37. beginX = auxbeginY;
  38. beginY = auxbeginX;
  39. endX = auxendY;
  40. endY = auxendX;
  41.  
  42.  
  43. }
  44.  
  45. public long calculate(int beginX, int beginY, int endX, int endY) {
  46.  
  47. interchange(beginX,beginY,endX,endY);
  48. //first, we check some logical conditions that must be fulfilled
  49. //in the map in order to make sense. If one of them it's true
  50. //it means something is wrong and we have to return -1
  51.  
  52. //a) if we begin and end in the same place
  53. if (beginX == endX && beginY == endY ||
  54. //b) if they have incorrect positions
  55. beginX < endX || beginY > endY||
  56. //c) more incorrect positions, it cannot be outside the limits
  57. endX >= x || beginY >= y ||
  58. beginX >= x || endY>= y) {
  59. return -1;
  60. } else {
  61. //we call to other to methods which i will explain later in order to
  62. //simplify the code
  63. initialize(beginX,beginY,endX,endY);
  64. fill(beginX,beginY,endX,endY);
  65. // i return the beginning because i decided to do it backwards
  66. // because it was easier for me to understand the axis
  67. showRoads(beginX, beginY, endX, endY);
  68. return road[beginX][beginY];
  69. }
  70.  
  71. }
  72.  
  73. //method for filling the road
  74. //if we find a barrier we have to make it a 0 in order not to sum the -1
  75. // if not, we just sum the diagonals
  76.  
  77. private void fill(int beginX, int beginY, int endX, int endY) {
  78. interchange(beginX,beginY,endX,endY);
  79. for (int row = endX+1; row <= beginX; row++)
  80. for (int col = endY-1; col >= beginY; col--){
  81. if (road[row][col]==-1){
  82. road[row][col]=0;
  83. }
  84. else {
  85. road [row][col] = road[row][col+1] + road[row-1][col];
  86. }
  87. }
  88. }
  89.  
  90. private void initialize(int beginX, int beginY, int endX, int endY) {
  91. interchange(beginX,beginY,endX,endY);
  92.  
  93. //initialize: we first fill with ones the column which
  94. // has the same Y coordinate as the end position
  95. // (always if there is no barrier. If there is a barrier
  96. // we should put zeros in all the positions next to the barrier)
  97. for (int i = endX; i<=beginX; i++){
  98. if (road[i][endY]!=-1){
  99. road[i][endY]=1;
  100. }
  101. else{
  102. for (int k = i; k<beginX; k++){
  103. road[k][endY]=0;
  104. }
  105. break;
  106. }
  107. }
  108.  
  109. //and then the row which has the same X coordinate as the end position
  110. //(same thing as before with the barriers)
  111. for (int j = endY-1;j>=beginY; j--){
  112. if (road[endX][j]!=-1){
  113. road[endX][j]=1;
  114. }
  115. else{
  116. for (int k = j; k>=beginY; k--){
  117. road[endX][k]=0;
  118. }
  119. break;
  120. }
  121. }
  122. }
  123.  
  124. //method for printing the roads, i adjunt the screencaptures
  125. // i didn't print the ones who gave me errors (last three)
  126. public void showRoads(int beginX, int beginY, int endX, int endY){
  127. System.out.println();
  128. for(int i = 0; i < road.length; i++){
  129. for(int j = 0; j < road[0].length; j++){
  130. if (i == beginX && j == beginY)
  131. System.out.print("START\t");
  132. else if (i == endX && j == endY)
  133. System.out.print("END\t\t");
  134. else
  135. System.out.print(road[i][j] + "\t\t");
  136. }
  137. System.out.println();
  138. }
  139. }
  140.  
  141.  
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement