Created
June 29, 2016 13:02
-
-
Save developer-sdk/102ef52a3cee603eaef11de0b209719d to your computer and use it in GitHub Desktop.
더블릿, 자리배정 문제 알고리즘(http://59.23.113.171/pool/koi_seat/koi_seat.php?pname=koi_seat)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Koi_Seat { | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| int y = in.nextInt(); | |
| int x = in.nextInt(); | |
| int targetNumber = in.nextInt(); | |
| in.close(); | |
| // int y = 5; | |
| // int x = 180; | |
| // int targetNumber = 899; | |
| int[][] array = new int[x][y]; | |
| int round = 0; | |
| int prevNumber = 1; | |
| while (true) { | |
| int number = (x * 2 + (y - 2) * 2) + prevNumber; | |
| if (prevNumber <= targetNumber && targetNumber < number) { | |
| check(array, round, targetNumber, prevNumber); | |
| break; | |
| } else if (prevNumber == targetNumber && targetNumber == number) { | |
| check(array, round, targetNumber, prevNumber); | |
| break; | |
| } | |
| prevNumber = number; | |
| round++; | |
| x -= 2; | |
| y -= 2; | |
| if (x <= 0 || y <= 0) { | |
| round = -1; | |
| break; | |
| } | |
| } | |
| if(round == -1) | |
| System.out.println("0"); | |
| // check(array, 3, x, y, 11, 43); | |
| // for (int[] ys : array) { | |
| // for (int yt : ys) | |
| // System.out.printf("%3d ", yt); | |
| // System.out.println(); | |
| // } | |
| } | |
| public static void check(int[][] array, int round, int number, int countNumber) { | |
| int x = array.length; | |
| int y = array[0].length; | |
| int count = countNumber; | |
| int startX = round; | |
| int startY = round; | |
| // x 증가 | |
| for (; startX < x-round; startX++) { | |
| array[startX][startY] = count++; | |
| if (array[startX][startY] == number) { | |
| print(startX, startY); | |
| return; | |
| } | |
| } | |
| startX--; | |
| startY++; | |
| // y 증가 | |
| for (; startY < y-round; startY++) { | |
| array[startX][startY] = count++; | |
| if (array[startX][startY] == number) { | |
| print(startX, startY); | |
| return; | |
| } | |
| } | |
| startX--; | |
| startY--; | |
| // x 감소 | |
| for (; startX > round; startX--) { | |
| array[startX][startY] = count++; | |
| if (array[startX][startY] == number) { | |
| print(startX, startY); | |
| return; | |
| } | |
| } | |
| // y 감소 | |
| for (; startY > round; startY--) { | |
| array[startX][startY] = count++; | |
| if (array[startX][startY] == number) { | |
| print(startX, startY); | |
| return; | |
| } | |
| } | |
| } | |
| public static void print(int x, int y) { | |
| System.out.printf("%d %d\n", y+1, x+1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment