[프로그래머스] 삼각 달팽이 - c++

규칙찾기

    vector<int> solution(int n) {
    vector<int> answer;
    vector<vector<int>> tmp(n, vector<int>(n));
    int value=1;
    int type=0, x=0, y=0;
    
    for(int i=0;i<n;i++){
        switch(type)
        {
        case 0:
            for(int j=i;j<n;j++){
                tmp[x++][y]=value++;
            }
            x--;
            y++;
            type=1;
            break;
                
        case 1:
            for(int j=i;j<n;j++){
                tmp[x][y++]=value++;
                
            }
            x--;
            y-=2;
            type=2;
            break;
        
        case 2:
            for(int j=i;j<n;j++){
                tmp[x--][y--]=value++;
            }
            y++;
            x+=2;
            type=0;
            break;
        }
    }
    for (int i = 0; i < n; i++)
	{
    	for (int j = 0; j < i + 1; j++)
	    {
		    	answer.push_back(tmp[i][j]);
	    }   
	}
    return answer;
    }

좋은 웹페이지 즐겨찾기