Advertisement
dimipan80

Exams - Labyrinth Escape

Nov 23rd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a rectangular field of size NxM, filled with numbers and directions.
  2.  On each cell on the field there will be a direction marked with the letters l, r, u, d.
  3.  On the first row the values are from 1 to M, on the second row – from M+1 to 2*M, on the third row –
  4.  from 2*M +1 to 3*M, on the Nth row – from (N-1)*M to N*M. By given start position, find the path outside
  5.  of the field, or print if you are lost. The method Solve accepts a zero-based array of strings.
  6.  The arguments are as follows: args[0] contains M and N separated by a single space (" "); args[1] contains R and C –
  7.  the start position (the star position is zero-based); args[2] to args[N+2] contain exactly M characters
  8.  (only the letters 'l', 'r', 'u' or 'd'). The output should contain a single string. */
  9.  
  10. "use strict";
  11.  
  12. function solve(args) {
  13.     var dimensions = args[0].split(/\s/).filter(Boolean);
  14.     var rows = parseInt(dimensions[0]), cols = parseInt(dimensions[1]);
  15.     var startPosition = args[1].split(/\s/).filter(Boolean);
  16.     var row = parseInt(startPosition[0]), col = parseInt(startPosition[1]);
  17.     var matrix = new Array(rows);
  18.     for (var i = 2; i < args.length; i += 1) {
  19.         matrix[i - 2] = args[i].split('').filter(Boolean);
  20.     }
  21.  
  22.     function movingToTheNextCell() {
  23.         var cellValue = matrix[row][col];
  24.         matrix[row][col] = undefined;
  25.         switch (cellValue) {
  26.             case 'l':
  27.                 row += 0;
  28.                 col += -1;
  29.                 break;
  30.             case 'r':
  31.                 row += 0;
  32.                 col += 1;
  33.                 break;
  34.             case 'u':
  35.                 row += -1;
  36.                 col += 0;
  37.                 break;
  38.             case 'd':
  39.                 row += 1;
  40.                 col += 0;
  41.                 break;
  42.         }
  43.  
  44.         return cellValue;
  45.     }
  46.  
  47.     var sum = 0, countCells = 0;
  48.     do {
  49.         sum += (row * cols) + col + 1;
  50.         if (matrix[row][col]) {
  51.             movingToTheNextCell();
  52.         } else {
  53.             return console.log("lost %d", countCells);
  54.         }
  55.         countCells += 1;
  56.     } while (row >= 0 && row < rows && col >= 0 && col < cols);
  57.  
  58.     console.log("out %d", sum);
  59. }
  60.  
  61. solve([
  62.     '3 4',
  63.     '1 3',
  64.     'lrrd',
  65.     'dlll',
  66.     'rddd'
  67. ]);
  68.  
  69. solve([
  70.     '5 8',
  71.     '0 0',
  72.     'rrrrrrrd',
  73.     'rludulrd',
  74.     'durlddud',
  75.     'urrrldud',
  76.     'ulllllll'
  77. ]);
  78.  
  79. solve([
  80.     '5 8',
  81.     '0 0',
  82.     'rrrrrrrd',
  83.     'rludulrd',
  84.     'lurlddud',
  85.     'urrrldud',
  86.     'ulllllll'
  87. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement