Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let data = gets().split(' ').map(Number);
- let N = data[0];
- let M = data[1];
- let jumps = data[2];
- let matrix = [];
- let counter = 1
- let jumpCount = 0
- //създава полето
- for( let i = 0; i<=N-1;i++){
- matrix.push([]);
- for(let j = 0; j <= M-1; j++){
- matrix[i][j]=counter
- counter++
- }
- }
- let currentPosition = gets().split(' ').map(Number);
- let sumOfnumbers = matrix[currentPosition[0]][currentPosition[1]];
- matrix[currentPosition[0]][currentPosition[1]] = 0
- let moves = []
- for (let i = 1; i<=jumps; i++){
- moves.push(gets().split(' ').map(Number))
- }
- let newPos = (move,pos) => {
- let result = []
- let row = move[0];
- let col = move[1];
- result.push(row + pos[0]);
- result.push(col + pos[1]);
- return result
- }
- hasEscaped = true
- outer:
- while(true){
- for (let i = 0 ; i <= jumps-1; i++){
- currentPosition = newPos(moves[i],currentPosition)
- if(matrix[currentPosition[0]][currentPosition[1]] === 0){
- hasEscaped = false
- break outer;
- } else if (currentPosition[0] < 0 || currentPosition[0] > M-1 || currentPosition[1] < 0 || currentPosition[1] > N-1){
- hasEscaped = true
- break outer;
- }
- sumOfnumbers = sumOfnumbers + matrix [currentPosition[0]][currentPosition[1]];
- matrix[currentPosition[0]][currentPosition[1]]=0
- jumpCount++
- }
- }
- if(hasEscaped){
- console.log(`escaped ${sumOfnumbers}`);
- } else {
- console.log(`caught ${jumpCount}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment