본문 바로가기
CODING/SW Expert Academy

[SWEA] 1954. 달팽이 숫자

by snow_white 2022. 5. 29.


import java.util.Scanner;

public class Solution {
	static int n;
	static int arr[][];
	static int dx[] = {0,1,0,-1};
	static int dy[] = {1,0,-1,0};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int TC = sc.nextInt();
		for(int tc=0 ;tc<TC; tc++) {
			n = sc.nextInt();
			arr = new int[n][n];
			int direction = 0, num=1;
			int x=0,y=0;
			arr[x][y] = num++;
			while(num<=n*n) {
				int next_x = x+dx[direction];
				int next_y = y+dy[direction];
				if(next_x<n && next_y<n && next_x>=0 && next_y>=0 && arr[next_x][next_y]==0) {
					x = next_x;
					y = next_y;
					arr[x][y] = num++;
				}
				else
					direction = (direction+1 >= 4)?0:direction+1;
			}
			System.out.println("#"+(tc+1));
			for(int i=0; i<n; i++) {
				for(int j=0; j<n; j++) {
					System.out.print(arr[i][j]+" ");
				}System.out.println();
			}
		}
	}
}

[출처]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PobmqAPoDFAUq&categoryId=AV5PobmqAPoDFAUq&categoryType=CODE&problemTitle=%EB%8B%AC%ED%8C%BD%EC%9D%B4&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

'CODING > SW Expert Academy' 카테고리의 다른 글

[SW Expert Academy] 2072. 홀수만 더하기  (0) 2022.01.20

댓글