Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let input = [
- '6 7 3',
- '0 0',
- '2 2',
- '-2 2',
- '3 -1'
- ];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- const line1 = gets().split(' ').map(Number);
- const rows = line1[0];
- const cols = line1[1];
- const countJumps = line1[2];
- const line2 = gets().split(' ').map(Number);
- let curRow = line2[0];
- let curCol = line2[1];
- let arrMoves = []
- for(let i = 0; i < countJumps;i++){
- arrMoves.push(gets().split(' ').map(Number))
- }
- let index = 1;
- let matrix = [];
- let jumps = 0;
- let arrCollected = [];
- let sumOfCollected = 0;
- for(let i = 0; i<rows;i++){
- matrix[i]=[]
- for(let j = 0; j<cols;j++){
- matrix[i][j]=index++
- }
- }
- let counter = 0
- outer:
- while(true){
- if(curRow< 0 || curCol< 0 || curRow >= rows || curCol>= cols){
- jumps++
- print(`escaped ${sumOfCollected}`)
- break outer;
- }else if(arrCollected.includes(matrix[curRow][curCol])){
- jumps++
- print(`caught ${jumps}`)
- break outer;
- }else{
- arrCollected.push(matrix[curRow][curCol]);
- sumOfCollected +=matrix[curRow][curCol]
- jumps++;
- curRow = curRow + arrMoves[counter][0]
- curCol = curCol + arrMoves[counter][1]
- }
- if(counter < arrMoves.length-1){
- counter++
- }else{
- counter = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment