Categories
JavaScript Телерик

Решение на задача – Joro the Naughty

Решение на задача: Joro The Naughty

Изпит: JavaScript основи

Точки в BGCoder: 100 от 100

function Solve(params) {
    var line = params[0].split(" ");
    var N = parseInt(line[0]);
    var M = parseInt(line[1]);

    var numbers = new Array(N);
    var visited = new Array(N);

    var number = 1;
    for (var i = 0; i < N; i++) {
        numbers[i] = new Array(M);
        visited[i] = new Array(M);
        for (var j = 0; j < M; j++) {
            numbers[i][j] = number;
            visited[i][j] = 0;
            number++;
        }
    }
    var J = parseInt(line[2]);
    line = params[1].split(" ");
    var rowPosition = parseInt(line[0]);
    var columnPosition = parseInt(line[1]);
    visited[rowPosition][columnPosition] = 1;
    var numbersSum = 0;
    numbersSum += numbers[rowPosition][columnPosition];
    for (var jump = 2; jump < J + 2; jump++) {
        line = params[jump].split(" ");
        var rowStep = parseInt(line[0]);
        var columnStep = parseInt(line[1]);
        rowPosition += rowStep;
        columnPosition += columnStep;
        if (rowPosition < 0 || rowPosition >= N || columnPosition < 0 || columnPosition >= M) {
            return 'escaped ' + numbersSum;
        }

        if (visited[rowPosition][columnPosition] === 1) {
            return 'caugth' + jump - 1;
        }

        visited[rowPosition][columnPosition] = 1;

        numbersSum += numbers[rowPosition][columnPosition];

        if (jump === J + 1) {
            jump = 1;
        }
    }
}