algorithm

[algorithm] 달팽이 java

tonirr 2021. 4. 13. 22:38
package baekjoon;

import java.util.Scanner;

public class Dalpaeng {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		int tsize = size;
		
		int n = sc.nextInt();
		int max = size * size;

		int [][] arr = new int[size][size];
		
		int j = 0;
		int i = -1;
		int count = 1;
		
		while(true) {
			for(int t = 1; t <= size; t++) {
				i += count;
				arr[i][j] = max;
				max--;
			}
			
			size--;
			if(size <= 0) break;
			
			for(int t = 1; t <= size; t++) {
				j += count;
				arr[i][j] = max;
				max--;
			}
			
			count *= -1;
		}
		
		int[] ans = new int[2];
		
		for(int x = 0; x < tsize; x++) {
			for(int y = 0; y < tsize; y++) {
				if(n == arr[x][y]) {
					ans[0] = x + 1;
					ans[1] = y + 1;
				}
				System.out.print(arr[x][y] + " ");
			}
			System.out.println();
		}
		System.out.println(ans[0] + " " + ans[1]);
	}
}