Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labs.course15_16.lab5dynamic.roadsInCity;
- /**
- * The complexity of the algorithm is quadratic because we have to iterate
- * through all the rows and columns of the map in order to fill it.
- * @author UO244826
- *
- */
- public class RoadsInCity {
- //First i initialize the "map" and variables for the rows and columns
- long road [][];
- int x;
- int y;
- //just the constructor
- public RoadsInCity(int x, int y) {
- this.road = new long [x][y];
- this.x = x;
- this.y = y;
- }
- //we add the barrier wherever we want as a -1
- public void addBarrier(int x, int y) {
- road[x][y] = -1;
- }
- public void interchange(int beginX,int beginY,int endX,int endY){
- int auxbeginX= beginX;
- int auxendX = endX;
- int auxbeginY = beginY;
- int auxendY = endY;
- if (beginX < endX || beginY > endY )
- beginX = auxbeginY;
- beginY = auxbeginX;
- endX = auxendY;
- endY = auxendX;
- }
- public long calculate(int beginX, int beginY, int endX, int endY) {
- interchange(beginX,beginY,endX,endY);
- //first, we check some logical conditions that must be fulfilled
- //in the map in order to make sense. If one of them it's true
- //it means something is wrong and we have to return -1
- //a) if we begin and end in the same place
- if (beginX == endX && beginY == endY ||
- //b) if they have incorrect positions
- beginX < endX || beginY > endY||
- //c) more incorrect positions, it cannot be outside the limits
- endX >= x || beginY >= y ||
- beginX >= x || endY>= y) {
- return -1;
- } else {
- //we call to other to methods which i will explain later in order to
- //simplify the code
- initialize(beginX,beginY,endX,endY);
- fill(beginX,beginY,endX,endY);
- // i return the beginning because i decided to do it backwards
- // because it was easier for me to understand the axis
- showRoads(beginX, beginY, endX, endY);
- return road[beginX][beginY];
- }
- }
- //method for filling the road
- //if we find a barrier we have to make it a 0 in order not to sum the -1
- // if not, we just sum the diagonals
- private void fill(int beginX, int beginY, int endX, int endY) {
- interchange(beginX,beginY,endX,endY);
- for (int row = endX+1; row <= beginX; row++)
- for (int col = endY-1; col >= beginY; col--){
- if (road[row][col]==-1){
- road[row][col]=0;
- }
- else {
- road [row][col] = road[row][col+1] + road[row-1][col];
- }
- }
- }
- private void initialize(int beginX, int beginY, int endX, int endY) {
- interchange(beginX,beginY,endX,endY);
- //initialize: we first fill with ones the column which
- // has the same Y coordinate as the end position
- // (always if there is no barrier. If there is a barrier
- // we should put zeros in all the positions next to the barrier)
- for (int i = endX; i<=beginX; i++){
- if (road[i][endY]!=-1){
- road[i][endY]=1;
- }
- else{
- for (int k = i; k<beginX; k++){
- road[k][endY]=0;
- }
- break;
- }
- }
- //and then the row which has the same X coordinate as the end position
- //(same thing as before with the barriers)
- for (int j = endY-1;j>=beginY; j--){
- if (road[endX][j]!=-1){
- road[endX][j]=1;
- }
- else{
- for (int k = j; k>=beginY; k--){
- road[endX][k]=0;
- }
- break;
- }
- }
- }
- //method for printing the roads, i adjunt the screencaptures
- // i didn't print the ones who gave me errors (last three)
- public void showRoads(int beginX, int beginY, int endX, int endY){
- System.out.println();
- for(int i = 0; i < road.length; i++){
- for(int j = 0; j < road[0].length; j++){
- if (i == beginX && j == beginY)
- System.out.print("START\t");
- else if (i == endX && j == endY)
- System.out.print("END\t\t");
- else
- System.out.print(road[i][j] + "\t\t");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement