diff --git a/sitou/src/B106_ChangeSeat.java b/sitou/src/B106_ChangeSeat.java new file mode 100644 index 0000000000000000000000000000000000000000..68faaba25e57f4fce7011bddee708ca2df3bf0a3 --- /dev/null +++ b/sitou/src/B106_ChangeSeat.java @@ -0,0 +1,139 @@ +package src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B106_ChangeSeat { + public static void main(String[] args) { + B106_ChangeSeat b106 = new B106_ChangeSeat(); + } + + private final InputData inData; + + public B106_ChangeSeat() { + this(input()); + final List> result = execute(); + output(result); + + } + + public B106_ChangeSeat(final InputData data) { + this.inData = data; + } + + private List> execute() { + List> result = new ArrayList>(); + Seats seats = new Seats(inData.height, inData.width); + + result = seats.changeSeat(inData, seats.seatsList); + + return result; + } + + private static InputData input() { + Scanner sc = new Scanner(System.in); + final int height = sc.nextInt(); + final int width = sc.nextInt(); + final int students = sc.nextInt(); + + final List seatNoList = new ArrayList(); + for (int i = 0; i < students; i++) { + seatNoList.add(sc.nextInt()); + } + + sc.close(); + return new InputData(height, width, students, seatNoList); + } + + private void output(final List> result) { + for (int i = 0; i < inData.height; i++) { + for (int j = 0; j < inData.width; j++) { + System.out.print(result.get(i).get(j)); + if (j != inData.width - 1) + System.out.print(" "); + } + System.out.println(); + } + } + + private static class InputData { + final int height; + final int width; + final int students; + final List seatNoList; + + public InputData(final int height, final int width, final int students, + final List seatNoList) { + this.height = height; + this.width = width; + this.students = students; + this.seatNoList = seatNoList; + } + } + + private static class Seats { + final List> seatsList = new ArrayList>(); + + // 全て0で座席表List作成 + public Seats(final int height, final int width) { + // ArrayList list; + for (int i = 0; i < height; i++) { + ArrayList list = new ArrayList(); + for (int j = 0; j < width; j++) { + list.add(0); + } + seatsList.add(list); + } + + } + + public List> changeSeat(final InputData inputData, + final List> seatsList) { + + List> newSeatsList = seatsList; + + for (int studentNo = 1; studentNo <= inputData.students; studentNo++) { + final int no = inputData.seatNoList.get(studentNo - 1) - 1; // studentNo-1 -> Listを初めから行いたい, + // (studentNo-1)-1 -> index取得のため + final int indexHeight = no / inputData.width; + final int indexWidth = no % inputData.width; + newSeatsList.get(indexHeight).set(indexWidth, studentNo); + } + + moveUp(inputData.height, inputData.width, newSeatsList); + + return newSeatsList; + } + + // もう少し分解すべきか + private void moveUp(final int height, final int width, + final List> newSeatsList) { + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + + int nowlookSeat = newSeatsList.get(i).get(j); + if (nowlookSeat != 0) { + int move_i = i; + while (move_i > 0) { + + if (newSeatsList.get(move_i - 1).get(j) == 0) { + newSeatsList.get(move_i - 1).set(j, nowlookSeat); + newSeatsList.get(move_i).set(j, 0); + move_i--; + } else { + break; + } + + } + + } + + } + } + + } + + } +}