Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a matrix of directions. The rows are zero-based. The possible directions are eight and
- follow the horse moves from the game chess. You should generate another matrix with the same size.
- The numbers in each row are consecutive and decreasing. The leftmost (first) number in each row is
- a power of 2, calculated with the formula 2row where row is the number of this row and
- each cell to the right on the same row is lesser with 1. Your task is to find the sum that can be found,
- using the given directions and summing the numbers in each cell you step into.
- If with the current direction, you step out of the matrix – print "Go go Horsy! Collected SUM weeds'",
- where SUM is the collected sum. If you step on a cell that you have previously stepped in –
- print "Sadly the horse is doomed in JUMPS jumps" where JUMPS is the number of cells passed.
- You start always at bottom-right cell (row-1, col-1).
- The first value of the input arguments you will be the numbers R and C, separated by a space
- • R is the number of rows in the matrix
- • C is the number of columns in the matrix
- The next R values in the input arguments will be exactly C directions
- Your function should return a single string:
- • If success, return "Go go Horsy! Collected SUM weeds", where sum is the sum of the visited cells
- • If fail, return "Sadly the horse is doomed in JUMPS jumps", where JUMPS are the number of cells from the path
- of the horse.
- R will be between 3 and 20, inclusive. C will be between 3 and 1024, inclusive. */
- "use strict";
- function solve(args) {
- args[0] = args[0].split(/\s/).filter(Boolean);
- var rows = parseInt(args[0][0]);
- var cols = parseInt(args[0][1]);
- var matrix = new Array(rows);
- for (var i = 1; i < args.length; i += 1) {
- matrix[i - 1] = args[i].split('').filter(Boolean);
- }
- function movingToTheNextCell() {
- var cellValue = matrix[row][col];
- matrix[row][col] = undefined;
- switch (cellValue) {
- case '1':
- row += -2;
- col += 1;
- break;
- case '2':
- row += -1;
- col += 2;
- break;
- case '3':
- row += 1;
- col += 2;
- break;
- case '4':
- row += 2;
- col += 1;
- break;
- case '5':
- row += 2;
- col += -1;
- break;
- case '6':
- row += 1;
- col += -2;
- break;
- case '7':
- row += -1;
- col += -2;
- break;
- case '8':
- row += -2;
- col += -1;
- break;
- }
- return cellValue;
- }
- var sum = 0;
- var jumps = 0;
- var row = rows - 1;
- var col = cols - 1;
- do {
- sum += Math.pow(2, row) - col;
- if (matrix[row][col]) {
- movingToTheNextCell();
- } else {
- return console.log("Sadly the horse is doomed in %d jumps", jumps);
- }
- jumps += 1;
- } while (row >= 0 && row < rows && col >= 0 && col < cols);
- console.log("Go go Horsy! Collected %d weeds", sum);
- }
- solve([
- '3 5',
- '54561',
- '43328',
- '52388'
- ]);
- solve([
- '3 5',
- '54361',
- '43326',
- '52188'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement