diff --git a/tisobe/src/B109.java b/tisobe/src/B109.java new file mode 100644 index 0000000000000000000000000000000000000000..c192c7fd2791cdca7c39c9e41b9c52b0f775229c --- /dev/null +++ b/tisobe/src/B109.java @@ -0,0 +1,144 @@ +package tisobe.src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +enum Seat_situation { + RESERVED(999), + INITIAL_VALUE(10); + + private final int id; + + private Seat_situation(final int id) { + this.id = id; + } + + public int getSeat_situation() { + return id; + } +} + + +public class B109 { + public static void main(String[] args) { + // 自分の得意な言語で + // Let's チャレンジ!! + Scanner sc = new Scanner(System.in); + final int numberOfReserved = sc.nextInt(); + final int seatHeight = sc.nextInt(); + final int seatWidth = sc.nextInt(); + final int goodViewY = sc.nextInt(); + final int goodViewX = sc.nextInt(); + List> seats = new ArrayList<>(); + ReserveSeat.initializeSeats(seatHeight, seatWidth, seats); + + ReserveSeat seat = new ReserveSeat(seatHeight, seatWidth,seats); + MahattanDitance manhattan = new MahattanDitance(seatHeight, seatWidth, goodViewX, goodViewY,seats); + seats = manhattan.calcManhattanDistance(); + + for (int i = 0; i < numberOfReserved; i++) { + int reservedSeatY = sc.nextInt(); + int reservedSeatX = sc.nextInt(); + seat.checkReservedSeat(reservedSeatX, reservedSeatY); + } + seats = seat.searchSeat(manhattan.searchMinManhattanDistance()); + printAnswer(seats); + sc.close(); + } + + static void printAnswer(List> seats) { + for (int i = 0; i < seats.size(); i++) { + if (seats.get(i).size() == 0) + continue; + for (int j = 0; j < seats.get(i).size(); j++) { + System.out.print(seats.get(i).get(j) + " "); + } + System.out.println(); + } + } +} + + +class ReserveSeat { + final private int seatHeight; + final private int seatWidth; + private List> seats; + + public ReserveSeat(final int seatHeight, final int seatWidth, List> seats) { + this.seatHeight = seatHeight; + this.seatWidth = seatWidth; + this.seats = seats; + } + + static void initializeSeats(final int seatHeight, final int seatWidth, List> seats) { + for (int i = 0; i < seatHeight; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < seatWidth; j++) { + row.add(0); + } + seats.add(row); + } + } + + public List> searchSeat(int minManhattanDistance) { + List> mostGoodSeats = new ArrayList<>(); + for (int y = 0; y < seatHeight; y++) { + for (int x = 0; x < seatWidth; x++) { + if (seats.get(y).get(x) != minManhattanDistance) continue; + List row = new ArrayList<>(); + row.add(y); + row.add(x); + mostGoodSeats.add(row); + } + } + return mostGoodSeats; + } + + public void checkReservedSeat(final int reservedSeatX, final int reservedSeatY) { + for (int y = 0; y < seatHeight; y++) { + for (int x = 0; x < seatWidth; x++) { + if (x != reservedSeatX) + continue; + if (y != reservedSeatY) + continue; + seats.get(y).set(x, Seat_situation.RESERVED.getSeat_situation()); + } + } + } +} +class MahattanDitance{ + final private int seatHeight; + final private int seatWidth; + final private int goodViewX; + final private int goodViewY; + private List> seats; + + public MahattanDitance(final int seatHeight, final int seatWidth, final int goodViewX, + final int goodViewY,List> seats) { + this.seatHeight = seatHeight; + this.seatWidth = seatWidth; + this.goodViewX = goodViewX; + this.goodViewY = goodViewY; + this.seats = seats; + } + + List> calcManhattanDistance() { + for (int y = 0; y < seatHeight; y++) { + for (int x = 0; x < seatWidth; x++) { + seats.get(y).set(x, Math.abs(x - goodViewX) + Math.abs(y - goodViewY)); + } + } + return seats; + } + int searchMinManhattanDistance() { + int minManhattanDistance = Seat_situation.INITIAL_VALUE.getSeat_situation(); + for (int y = 0; y < seatHeight; y++) { + for (int x = 0; x < seatWidth; x++) { + if (seats.get(y).get(x) >= minManhattanDistance) continue; + minManhattanDistance = seats.get(y).get(x); + } + } + return minManhattanDistance; + } +} diff --git a/tisobe/src/B109Test.java b/tisobe/src/B109Test.java new file mode 100644 index 0000000000000000000000000000000000000000..017ca409d42aadcdf443e0eb511518eb342e2ded --- /dev/null +++ b/tisobe/src/B109Test.java @@ -0,0 +1,85 @@ +package tisobe.src; + +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class B109Test { + public static class Seatクラス内のメソッドが正常に動作する場合 { + private int seatHeight; + private int seatWidth; + private int goodViewX; + private int goodViewY; + private List> initialSeats = new ArrayList<>(); + + @Before + public void setUp() { + seatHeight = 3; + seatWidth = 3; + goodViewX = 1; + goodViewY = 1; + for (int i = 0; i < seatHeight; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < seatWidth; j++) { + row.add(0); + } + initialSeats.add(row); + } + } + + @Test + public void calcManhattanDistanceTest() { + MahattanDitance manhattanDistance = new MahattanDitance(seatHeight, seatWidth, goodViewX, + goodViewY, initialSeats); + List> expectedSeats = Arrays + .asList( + Arrays.asList(2, 1, 2), + Arrays.asList(1, 0, 1), + Arrays.asList(2, 1, 2)); + assertEquals("マンハッタン距離の計算が正しくありません", expectedSeats, manhattanDistance + .calcManhattanDistance()); + } + + public static class ManhattanDistanceクラス内のメソッドが正常に動作する場合 { + private int seatHeight; + private int seatWidth; + private int goodViewX; + private int goodViewY; + private List> initialSeats = new ArrayList<>(); + + @Before + public void setUp() { + seatHeight = 3; + seatWidth = 3; + goodViewX = 1; + goodViewY = 1; + for (int i = 0; i < seatHeight; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < seatWidth; j++) { + row.add(0); + } + initialSeats.add(row); + } + } + + @Test + public void seachSeat() { + ReserveSeat seat = new ReserveSeat(seatHeight, seatWidth, initialSeats); + MahattanDitance manhattanDistance = new MahattanDitance(seatHeight, seatWidth, goodViewX, + goodViewY, initialSeats); + manhattanDistance.calcManhattanDistance(); + List> expectedSeats = Arrays + .asList( + Arrays.asList(1, 1)); + assertEquals("もっとも見やすい席ではありません", expectedSeats, seat + .searchSeat(manhattanDistance.searchMinManhattanDistance())); + } + } + } +}