[프로그래머스 문제풀이69]행렬 테두리 회전하기 js

이ᄏᄋ·2021년 10월 3일
0
const rowsXcolumns=[];
function solution(rows, columns, queries) {
    const answer=[]
    
    for(let i=0;i<rows;i++){
        rowsXcolumns[i]=[];
        for(let j=0;j<columns;j++){
            rowsXcolumns[i][j]=i*columns+j+1;
        }
    }
    for(const query of queries){
        answer.push(rotate(query,rowsXcolumns)[1]);
        
    }
    
    return answer;
}
function rotate(query,rowsXcolumns){
    const checkList=[];
    for(let i=0;i<rowsXcolumns.length;i++){
        checkList[i]=[];
        for(let j=0;j<rowsXcolumns[0].length;j++){
            checkList[i][j]=i*rowsXcolumns[0].length+j+1;
        }
    }
    const result=rowsXcolumns;
    let nowX=query[1]-1;
    let nowY=query[0]-1;
    const startX=query[1]-1;
    const startY=query[0]-1;
    const endX=query[3]-1;
    const endY=query[2]-1;
    let nextNum=result[nowY][nowX];//다음으로 이동할 숫자
    let nowNum=0;// 숫자 
    let min=nextNum;
  
    while(true){
        if(checkList[nowY][nowX]===true){
            break;
        }
      
        checkList[nowY][nowX]=true;
        nowNum=nextNum;
        min=Math.min(nowNum,min);
   
        if(nowY===startY){
           if(nowX===endX){
                nextNum=result[nowY+1][nowX];
                result[nowY+1][nowX]=nowNum;   
                nowY+=1;
                continue;
           }else{
                nextNum=result[nowY][nowX+1];
                result[nowY][nowX+1]=nowNum;
                nowX+=1;     
                continue;
           }
        } 
        if(nowY===endY){
             if(nowX===startX){
                nextNum=result[nowY-1][nowX]
                result[nowY-1][nowX]=nowNum;
                nowY-=1;
                continue;
           }else{
                nextNum=result[nowY][nowX-1]
                result[nowY][nowX-1]=nowNum;
                nowX-=1;
                continue;
           }
        }
        if(nowX===endX){
                nextNum=result[nowY+1][nowX]
                result[nowY+1][nowX]=nowNum;
                nowY+=1;       
                continue;
        }
        if(nowX===startX){
            nextNum=result[nowY-1][nowX]
            result[nowY-1][nowX]=nowNum;
            nowY-=1;
            continue;
    }
       
    }
    return [result,min];
}

js의 배열 복사에 대해서 다차원배열과 1차원 배열이 다르다는 것을 알게되었다.

profile
미쳤다.

0개의 댓글