algorithm

[algorithm] 1952 달팽이 java

tonirr 2021. 4. 14. 22:05

www.acmicpc.net/problem/1952

 

1952번: 달팽이2

M줄 N칸으로 되어 있는 표 위에, 달팽이 모양으로 선을 그리려고 한다. 위의 그림은 M=5, N=3의 예이다. 이제 표의 왼쪽 위 칸(ㅇ)에서 시작하여, 오른쪽으로 선을 그려 나간다. 표의 바깥 또는 이미

www.acmicpc.net

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		int [][]arr = new int[m][n];
		int max = m * n;
		int x = m;
		int y = n;
		int answer = 0;
		
		int i = 0;
		int j = -1;
		int count = 1;
		while(true) {
			for(int t = 0; t < n; t++) {
				j += count;
				arr[i][j] = max;
				max--;
			}
			
			m--;
			
			if(m <= 0) break;
			answer++;
			
			for(int t = 0; t < m; t++) {
				i += count;
				arr[i][j] = max;
				max--;
			}
			
			n--;
			
			if(n <= 0) break;
			count *= -1;
			answer++;
			
		}
		System.out.println(answer);
	}
}