Advertisement
lnikod4s

Joro the Naughty

May 27th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     var initialData = args[0].split(' '),
  3.         N = Number(initialData[0]),
  4.         M = Number(initialData[1]),
  5.         J = Number(initialData[2]),
  6.         startPos = args[1].split(' '),
  7.         currRow = Number(startPos[0]),
  8.         currCol = Number(startPos[1]),
  9.         matrix = [],
  10.         i,
  11.         j,
  12.         sum = 0,
  13.         jump = 0,
  14.         currJump,
  15.         jumps = args.slice(2).map(function (line) {
  16.             return line.split(' ').map(Number)
  17.         }),
  18.         jumpsCount = 0;
  19.  
  20.     for (i = 0; i < N; i++) {
  21.         matrix[i] = [];
  22.         for (j = 0; j < M; j++) {
  23.             matrix[i][j] = M * i + j + 1;
  24.         }
  25.     }
  26.  
  27.     function isCellTraversal(row, col) {
  28.         return row >= 0 && row < N && col >= 0 && col < M;
  29.     }
  30.  
  31.     function isCellVisited(row, col) {
  32.         return matrix[row][col] == 'x';
  33.     }
  34.  
  35.     function getCellValue(row, col) {
  36.         return matrix[row][col];
  37.     }
  38.  
  39.     function markCellAsVisited(row, col) {
  40.         matrix[row][col] = 'x';
  41.     }
  42.  
  43.     while (true) {
  44.         if (!isCellTraversal(currRow, currCol)) {
  45.             return 'escaped ' + sum;
  46.         }
  47.         if (isCellVisited(currRow, currCol)) {
  48.             return 'caught ' + jumpsCount;
  49.         }
  50.  
  51.         sum += getCellValue(currRow, currCol);
  52.         jump %= jumps.length;
  53.         currJump = jumps[jump];
  54.         markCellAsVisited(currRow, currCol);
  55.  
  56.         currRow += currJump[0];
  57.         currCol += currJump[1];
  58.         jumpsCount++;
  59.         jump++;
  60.     }
  61. }
  62.  
  63. console.log(solve([
  64.     '6 7 3',
  65.     '0 0',
  66.     '2 2',
  67.     '-2 2',
  68.     '3 -1'
  69. ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement