Advertisement
dimipan80

Exams - Paths

Dec 26th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a matrix of directions. The rows are zero-based. The possible directions are four:
  2.  •    "dr" stands for "down-right" direction
  3.  •    "ur" stands for "up-right" direction
  4.  •    "ul" stands for "up-left" direction
  5.  •    "dl" stands for "down-left" direction
  6.  You should generate another matrix with the same size. The numbers in each row are consecutive.
  7.  The leftmost (first) number in each row is a power of 2, calculated with the formula 2row where row is the number
  8.  of this row. Your task is to find the sum that can be found, using the given directions and summing the numbers
  9.  in each cell you step into. If with the current direction, you step out of the matrix – print "successed with SUM",
  10.  where sum is the calculated sum. If you step on a cell, that you have previously stepped in – print
  11.  "failed at POSITION", where POSITION is the position of the previously visited cell.
  12.  You start always at position (0, 0) – row = 0, column = 0. Create a function in JavaScript that solves this problem.
  13.  The first value of the input arguments you will be the numbers R and C, separated by a space
  14.  •    R is the number of rows in the matrix
  15.  •    C is the number of columns in the matrix
  16.  The next R values in the input arguments will be exactly C directions, separated by a space.
  17.  Your function should return a single string:
  18.  •    If success, return "successed with SUM", where sum is the sum of the visited cells
  19.  •    If fail, return "failed at POSITION", where POSITION is the position of the already visited cell */
  20.  
  21. "use strict";
  22.  
  23. function solve(args) {
  24.     args[0] = args[0].split(/\s+/).filter(Boolean);
  25.     var rows = parseInt(args[0][0]);
  26.     var cols = parseInt(args[0][1]);
  27.     var matrix = new Array(rows);
  28.     for (var i = 1; i < args.length; i += 1) {
  29.         matrix[i - 1] = args[i].split(/\s+/).filter(Boolean);
  30.     }
  31.  
  32.     var sum = 0;
  33.     var row = 0;
  34.     var col = 0;
  35.  
  36.     function movingToTheNextCell() {
  37.         var cellValue = matrix[row][col];
  38.         matrix[row][col] = undefined;
  39.         switch (cellValue) {
  40.             case 'dr':
  41.                 row += 1;
  42.                 col += 1;
  43.                 break;
  44.             case 'dl':
  45.                 row += 1;
  46.                 col += -1;
  47.                 break;
  48.             case 'ur':
  49.                 row += -1;
  50.                 col += 1;
  51.                 break;
  52.             case 'ul':
  53.                 row += -1;
  54.                 col += -1;
  55.                 break;
  56.         }
  57.         return cellValue;
  58.     }
  59.  
  60.     do {
  61.         sum += (Math.pow(2, row) + col);
  62.         if (matrix[row][col]) {
  63.             movingToTheNextCell();
  64.         } else {
  65.             return console.log("failed at (%d, %d)", row, col);
  66.         }
  67.     } while (row >= 0 && row < rows && col >= 0 && col < cols);
  68.  
  69.     console.log("successed with %d", sum);
  70. }
  71.  
  72. solve([
  73.     '3 5',
  74.     'dr ur dr ul dr',
  75.     'dr ur dr dl dr',
  76.     'dr ur dr ul dr'
  77. ]);
  78.  
  79. solve([
  80.     '3 5',
  81.     'dr ur dr ul dr',
  82.     'dr ur dr dl dr',
  83.     'dr ur ur ul dr'
  84. ]);
  85.  
  86. solve([
  87.     '3 5',
  88.     'dr dl dr ur ul',
  89.     'dr dr ul ur ur',
  90.     'dl dr ur dl ur'
  91. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement