From 51e392480df1dbcf4faad3e0a660bb8d5ba91627 Mon Sep 17 00:00:00 2001 From: isobe Date: Wed, 23 Jul 2025 15:44:10 +0900 Subject: [PATCH 1/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB109?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=81=A8=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tisobe/src/B109.java | 124 +++++++++++++++++++++++++++++++++++++++ tisobe/src/B109Test.java | 56 ++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 tisobe/src/B109.java create mode 100644 tisobe/src/B109Test.java diff --git a/tisobe/src/B109.java b/tisobe/src/B109.java new file mode 100644 index 0000000..c53a8e5 --- /dev/null +++ b/tisobe/src/B109.java @@ -0,0 +1,124 @@ +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 getSituation() { + return id; + } +} + + +public class B109 { + public static void main(String[] args) { + // 自分の得意な言語で + // Let's チャレンジ!! + Scanner sc = new Scanner(System.in); + final int NUMBER_OF_RESERVED = sc.nextInt(); + final int SEAT_HEIGHT = sc.nextInt(); + final int SEAT_WIDTH = sc.nextInt(); + final int GOOD_VIEW_Y = sc.nextInt(); + final int GOOD_VIEW_X = sc.nextInt(); + List> seats = new ArrayList<>(); + + for (int i = 0; i < SEAT_HEIGHT; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < SEAT_WIDTH; j++) { + row.add(0); + } + seats.add(row); + } + + Seat seat = new Seat(SEAT_HEIGHT, SEAT_WIDTH, GOOD_VIEW_X, GOOD_VIEW_Y, seats); + seats = seat.calcManhattan(); + + for (int i = 0; i < NUMBER_OF_RESERVED; i++) { + int reservedSeatY = sc.nextInt(); + int reservedSeatX = sc.nextInt(); + for (int y = 0; y < SEAT_HEIGHT; y++) { + for (int x = 0; x < SEAT_WIDTH; x++) { + if (x == reservedSeatX && y == reservedSeatY) { + seats.get(y).set(x, Seat_situation.RESERVED.getSituation()); + } + } + } + } + seats = seat.seachSeat(); + for (int i = 0; i < seats.size(); i++) { + if (seats.get(i).size() != 0) { + for (int j = 0; j < seats.get(i).size(); j++) { + System.out + .print(seats.get(i).get(j) + + " "); + } + System.out.println(); + } + } + sc.close(); + + } +} + + +class Seat { + private int seat_height; + private int seat_width; + private int good_view_x; + private int good_view_y; + private List> seats; + + public Seat(final int seat_height, final int seat_width, final int good_view_x, + final int good_view_y, final List> seats) { + this.seat_height = seat_height; + this.seat_width = seat_width; + this.good_view_x = good_view_x; + this.good_view_y = good_view_y; + this.seats = seats; + } + + public Seat() {} + + public List> calcManhattan() { + for (int y = 0; y < seat_height; y++) { + for (int x = 0; x < seat_width; x++) { + + seats.get(y).set(x, Math.abs(x - good_view_x) + Math.abs(y - good_view_y)); + } + } + return seats; + } + + public List> seachSeat() { + List> most_Good_seats = new ArrayList<>(); + int minManhattan = Seat_situation.INITIAL_VALUE.getSituation(); + for (int y = 0; y < seat_height; y++) { + for (int x = 0; x < seat_width; x++) { + if (seats.get(y).get(x) < minManhattan) { + minManhattan = seats.get(y).get(x); + } + } + } + for (int y = 0; y < seat_height; y++) { + for (int x = 0; x < seat_width; x++) { + if (seats.get(y).get(x) == minManhattan) { + List row = new ArrayList<>(); + row.add(y); + row.add(x); + most_Good_seats.add(row); + } + } + } + return most_Good_seats; + } +} diff --git a/tisobe/src/B109Test.java b/tisobe/src/B109Test.java new file mode 100644 index 0000000..82bdaae --- /dev/null +++ b/tisobe/src/B109Test.java @@ -0,0 +1,56 @@ +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 serUp() { + 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 calcManhattanTest() { + Seat seat = new Seat(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, seat.calcManhattan()); + } + + @Test + public void seachSeat() { + Seat seat = new Seat(seatHeight, seatWidth, goodViewX, goodViewY, initialSeats); + seat.calcManhattan(); + List> expectedSeats = Arrays.asList( + Arrays.asList(1, 1) + ); + assertEquals("もっとも見やすい席ではありません", expectedSeats, seat.seachSeat()); + } + } +} -- GitLab From a3752a55cef6506ba196cc46b27eec2dd4ca78b4 Mon Sep 17 00:00:00 2001 From: isobe Date: Thu, 24 Jul 2025 15:11:23 +0900 Subject: [PATCH 2/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB109?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tisobe/src/B109.java | 146 +++++++++++++++++++++------------------ tisobe/src/B109Test.java | 10 +-- 2 files changed, 84 insertions(+), 72 deletions(-) diff --git a/tisobe/src/B109.java b/tisobe/src/B109.java index c53a8e5..855f2df 100644 --- a/tisobe/src/B109.java +++ b/tisobe/src/B109.java @@ -14,7 +14,7 @@ enum Seat_situation { this.id = id; } - public int getSituation() { + public int getsituation() { return id; } } @@ -25,100 +25,112 @@ public class B109 { // 自分の得意な言語で // Let's チャレンジ!! Scanner sc = new Scanner(System.in); - final int NUMBER_OF_RESERVED = sc.nextInt(); - final int SEAT_HEIGHT = sc.nextInt(); - final int SEAT_WIDTH = sc.nextInt(); - final int GOOD_VIEW_Y = sc.nextInt(); - final int GOOD_VIEW_X = sc.nextInt(); + 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<>(); - for (int i = 0; i < SEAT_HEIGHT; i++) { - List row = new ArrayList<>(); - for (int j = 0; j < SEAT_WIDTH; j++) { - row.add(0); - } - seats.add(row); - } + initializeSeats(seatHeight, seatWidth, seats); - Seat seat = new Seat(SEAT_HEIGHT, SEAT_WIDTH, GOOD_VIEW_X, GOOD_VIEW_Y, seats); - seats = seat.calcManhattan(); + Seat seat = new Seat(seatHeight, seatWidth, goodViewX, goodViewY, seats); + seats = seat.calcManhattanDistance(); - for (int i = 0; i < NUMBER_OF_RESERVED; i++) { + for (int i = 0; i < numberOfReserved; i++) { int reservedSeatY = sc.nextInt(); int reservedSeatX = sc.nextInt(); - for (int y = 0; y < SEAT_HEIGHT; y++) { - for (int x = 0; x < SEAT_WIDTH; x++) { - if (x == reservedSeatX && y == reservedSeatY) { - seats.get(y).set(x, Seat_situation.RESERVED.getSituation()); - } - } + seat.checkReservedSeat(reservedSeatX, reservedSeatY); + } + seats = seat.searchSeat(seat.searchMinManhattanDistance()); + printAnswer(seats); + sc.close(); + } + + 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); } - seats = seat.seachSeat(); + } + + static void printAnswer(List> seats) { for (int i = 0; i < seats.size(); i++) { - if (seats.get(i).size() != 0) { - for (int j = 0; j < seats.get(i).size(); j++) { - System.out - .print(seats.get(i).get(j) - + " "); - } - System.out.println(); + 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(); } - sc.close(); - } } class Seat { - private int seat_height; - private int seat_width; - private int good_view_x; - private int good_view_y; - private List> seats; - - public Seat(final int seat_height, final int seat_width, final int good_view_x, - final int good_view_y, final List> seats) { - this.seat_height = seat_height; - this.seat_width = seat_width; - this.good_view_x = good_view_x; - this.good_view_y = good_view_y; + final private int seatHeight; + final private int seatWidth; + final private int goodViewX; + final private int goodViewY; + final private List> seats; + + public Seat(final int seatHeight, final int seatWidth, final int goodViewX, + final int goodViewY, final List> seats) { + this.seatHeight = seatHeight; + this.seatWidth = seatWidth; + this.goodViewX = goodViewX; + this.goodViewY = goodViewY; this.seats = seats; } - public Seat() {} + public List> calcManhattanDistance() { + for (int y = 0; y < seatHeight; y++) { + for (int x = 0; x < seatWidth; x++) { - public List> calcManhattan() { - for (int y = 0; y < seat_height; y++) { - for (int x = 0; x < seat_width; x++) { - - seats.get(y).set(x, Math.abs(x - good_view_x) + Math.abs(y - good_view_y)); + seats.get(y).set(x, Math.abs(x - goodViewX) + Math.abs(y - goodViewY)); } } return seats; } - public List> seachSeat() { - List> most_Good_seats = new ArrayList<>(); - int minManhattan = Seat_situation.INITIAL_VALUE.getSituation(); - for (int y = 0; y < seat_height; y++) { - for (int x = 0; x < seat_width; x++) { - if (seats.get(y).get(x) < minManhattan) { - minManhattan = seats.get(y).get(x); - } + public int searchMinManhattanDistance() { + int minManhattanDistance = Seat_situation.INITIAL_VALUE.getsituation(); + 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; + } + + 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); } } - for (int y = 0; y < seat_height; y++) { - for (int x = 0; x < seat_width; x++) { - if (seats.get(y).get(x) == minManhattan) { - List row = new ArrayList<>(); - row.add(y); - row.add(x); - most_Good_seats.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.getsituation()); } } - return most_Good_seats; } } diff --git a/tisobe/src/B109Test.java b/tisobe/src/B109Test.java index 82bdaae..e84cafa 100644 --- a/tisobe/src/B109Test.java +++ b/tisobe/src/B109Test.java @@ -19,7 +19,7 @@ public class B109Test { private List> initialSeats = new ArrayList<>(); @Before - public void serUp() { + public void setUp() { seatHeight = 3; seatWidth = 3; goodViewX = 1; @@ -34,23 +34,23 @@ public class B109Test { } @Test - public void calcManhattanTest() { + public void calcManhattanDistanceTest() { Seat seat = new Seat(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, seat.calcManhattan()); + assertEquals("マンハッタン距離の計算が正しくありません", expectedSeats, seat.calcManhattanDistance()); } @Test public void seachSeat() { Seat seat = new Seat(seatHeight, seatWidth, goodViewX, goodViewY, initialSeats); - seat.calcManhattan(); + seat.calcManhattanDistance(); List> expectedSeats = Arrays.asList( Arrays.asList(1, 1) ); - assertEquals("もっとも見やすい席ではありません", expectedSeats, seat.seachSeat()); + assertEquals("もっとも見やすい席ではありません", expectedSeats, seat.searchSeat(seat.searchMinManhattanDistance())); } } } -- GitLab From 8ca8b0a718935e439a062564028db7ed77e7a77f Mon Sep 17 00:00:00 2001 From: isobe Date: Fri, 25 Jul 2025 17:41:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB109?= =?UTF-8?q?=E3=81=AE2=E5=9B=9E=E7=9B=AE=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tisobe/src/B109.java | 98 ++++++++++++++++++++----------------- tisobe/src/B109Test.java | 103 +++++++++++++++++++++++++-------------- 2 files changed, 119 insertions(+), 82 deletions(-) diff --git a/tisobe/src/B109.java b/tisobe/src/B109.java index 855f2df..c192c7f 100644 --- a/tisobe/src/B109.java +++ b/tisobe/src/B109.java @@ -14,7 +14,7 @@ enum Seat_situation { this.id = id; } - public int getsituation() { + public int getSeat_situation() { return id; } } @@ -31,33 +31,22 @@ public class B109 { final int goodViewY = sc.nextInt(); final int goodViewX = sc.nextInt(); List> seats = new ArrayList<>(); + ReserveSeat.initializeSeats(seatHeight, seatWidth, seats); - initializeSeats(seatHeight, seatWidth, seats); - - Seat seat = new Seat(seatHeight, seatWidth, goodViewX, goodViewY, seats); - seats = seat.calcManhattanDistance(); + 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(seat.searchMinManhattanDistance()); + seats = seat.searchSeat(manhattan.searchMinManhattanDistance()); printAnswer(seats); sc.close(); } - 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); - } - } - static void printAnswer(List> seats) { for (int i = 0; i < seats.size(); i++) { if (seats.get(i).size() == 0) @@ -71,43 +60,27 @@ public class B109 { } -class Seat { +class ReserveSeat { final private int seatHeight; final private int seatWidth; - final private int goodViewX; - final private int goodViewY; - final private List> seats; + private List> seats; - public Seat(final int seatHeight, final int seatWidth, final int goodViewX, - final int goodViewY, final List> seats) { + public ReserveSeat(final int seatHeight, final int seatWidth, List> seats) { this.seatHeight = seatHeight; this.seatWidth = seatWidth; - this.goodViewX = goodViewX; - this.goodViewY = goodViewY; this.seats = seats; } - - public 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; - } - - public int searchMinManhattanDistance() { - int minManhattanDistance = Seat_situation.INITIAL_VALUE.getsituation(); - 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); + + 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); } - return minManhattanDistance; } - + public List> searchSeat(int minManhattanDistance) { List> mostGoodSeats = new ArrayList<>(); for (int y = 0; y < seatHeight; y++) { @@ -129,8 +102,43 @@ class Seat { continue; if (y != reservedSeatY) continue; - seats.get(y).set(x, Seat_situation.RESERVED.getsituation()); + 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 index e84cafa..017ca40 100644 --- a/tisobe/src/B109Test.java +++ b/tisobe/src/B109Test.java @@ -10,47 +10,76 @@ import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; @RunWith(Enclosed.class) -public class B109Test { - public static class Seatクラス内のメソッドが正常に動作する場合{ +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() { - Seat seat = new Seat(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, seat.calcManhattanDistance()); - } - - @Test - public void seachSeat() { - Seat seat = new Seat(seatHeight, seatWidth, goodViewX, goodViewY, initialSeats); - seat.calcManhattanDistance(); - List> expectedSeats = Arrays.asList( - Arrays.asList(1, 1) - ); - assertEquals("もっとも見やすい席ではありません", expectedSeats, seat.searchSeat(seat.searchMinManhattanDistance())); - } + + @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())); + } + } } } -- GitLab