티스토리 뷰

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);
	}
}

'algorithm' 카테고리의 다른 글

[algorithm] 그리디 알고리즘  (0) 2021.04.25
[algorithm] 그리디 알고리즘  (0) 2021.04.17
[algorithm] 달팽이 java  (0) 2021.04.13
[algorithm] 동적프로그래밍 알고리즘  (0) 2021.04.13
동적프로그래밍 알고리즘  (0) 2021.04.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함