Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function orbit(input) {
- let [rowM,colM] = input[0].split(' ');
- let [sR,sC] = input[1].split(' ');
- rowM = Number(rowM);
- colM = Number(colM);
- sR = Number(sR);
- sC = Number(sC);
- let orbit = 1;
- // I - Define MATRIX => matrix[row][col]
- //let matrix = Array(rowM).fill(Array(colM), undefined, rowM);
- // II - Define MATRIX => matrix[row][col]
- let matrix = [];
- for(var i=0; i<rowM; i++) {
- matrix[i] = [];
- for(var j=0; j<colM; j++) {
- matrix[i][j] = undefined;
- }
- }
- // The Star's cell
- matrix[sR][sC]=orbit;
- let startRow = sR - 1, startCol = sC - 1;
- let endRow = sR + 1, endCol = sC + 1;
- while (true) {
- orbit++;
- for (let row = startRow; row <=endRow; row++) {
- for (let col = startCol; col<=endCol; col++){
- if (!((row==sR && col==sC) || row<0 || row>=rowM || col<0 || col>=colM)){
- if (row==startRow || row==endRow) {
- matrix[row][col] = orbit;
- } else {
- if (col==startCol || col==endCol){
- matrix[row][col] = orbit;
- }
- }
- }
- }
- }
- startRow--;
- endRow++;
- startCol--;
- endCol++;
- if (startRow<0 && endRow==rowM && startCol<0 && endCol==colM) {
- break;
- }
- } // End while
- for (let row of matrix){
- console.log(row.join(' '));
- }
- }
Add Comment
Please, Sign In to add comment