diff --git a/ykoiso/src/B034_Robot.java b/ykoiso/src/B034_Robot.java new file mode 100644 index 0000000000000000000000000000000000000000..1aef6d77643c8b36ddf74b599f95256442141ec1 --- /dev/null +++ b/ykoiso/src/B034_Robot.java @@ -0,0 +1,190 @@ +import java.util.Scanner; + +public class B034_Robot { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 各定数読み込み + // 初期座標 + final int X = scan.nextInt(); + final int Y = scan.nextInt(); + // 移動量 + final int W = scan.nextInt(); + final int D = scan.nextInt(); + final int S = scan.nextInt(); + final int A = scan.nextInt(); + // 命令数 + final int N = scan.nextInt(); + // ロボットの生成 + Robot robot = new Robot(X, Y, W, A, S, D); + String order, direction; + // 命令の実行 + for (int i = 0; i < N; i++) { + order = scan.next(); + direction = scan.next(); + robot.executeOrder(order, direction); + } + // 現在の座標の出力 + robot.showCoodinate(); + + scan.close(); + } + + static class Robot { + private int x, y, W, A, S, D; + // ロボットの向いている方向 + // 角度で表すdeg表記 + private int deg = 0; + + // コンストラクタ + Robot(int x, int y, int W, int A, int S, int D) { + // 座標 + this.x = x; + this.y = y; + // 移動量 + this.W = W; + this.A = A; + this.S = S; + this.D = D; + } + + // 方向取得 + // テスト用 + public int getDeg() { + return deg; + } + + // 座標取得 + // テスト用 + public int getX() { + return x; + } + + public int getY() { + return y; + } + + // 座標表示 + public void showCoodinate() { + System.out.println(x + " " + y); + } + + // 命令実行関数 + public void executeOrder(String order, String direction) { + if (order.equals("m")) { + // 移動命令判定 + // 移動関数は絶対座標系 + // ここで相対から絶対に変更 + switch (deg) { + case 0: + moveFront(direction); + break; + case 90: + moveRight(direction); + break; + case 180: + moveBack(direction); + break; + case 270: + moveLeft(direction); + break; + } + } else { + changeDirection(direction); + } + } + + // 方向変更関数 + public void changeDirection(String direction) { + switch (direction) { + case "R": + deg += 90; + break; + case "B": + deg += 180; + break; + case "L": + deg += 270; + break; + } + if (deg >= 360) { + deg -= 360; + } + } + + // 前方移動関数 + // 絶対座標系 + public void moveFront(String direction) { + switch (direction) { + case "F": + y += W; + break; + case "R": + x += D; + break; + case "B": + y -= S; + break; + case "L": + x -= A; + break; + } + } + + // 右方向移動関数 + // 絶対座標系 + public void moveRight(String direction) { + switch (direction) { + case "F": + x += W; + break; + case "R": + y -= D; + break; + case "B": + x -= S; + break; + case "L": + y += A; + break; + } + } + + // 後方移動関数 + // 絶対座標系 + public void moveBack(String direction) { + switch (direction) { + case "F": + y -= W; + break; + case "R": + x -= D; + break; + case "B": + y += S; + break; + case "L": + x += A; + break; + } + } + + // 左方向移動関数 + // 絶対座標系 + public void moveLeft(String direction) { + switch (direction) { + case "F": + x -= W; + break; + case "R": + y += D; + break; + case "B": + x += S; + break; + case "L": + y -= A; + break; + } + } + } +} diff --git a/ykoiso/src/B109_Eiga.java b/ykoiso/src/B109_Eiga.java new file mode 100644 index 0000000000000000000000000000000000000000..c3882cfcabdce196ab5b0adea83c8c8c0382bda1 --- /dev/null +++ b/ykoiso/src/B109_Eiga.java @@ -0,0 +1,91 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +public class B109_Eiga { + // 予約されていない席の中で最も見やすい席を探す + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 予約済みの席 + final int N = scan.nextInt(); + final int H = scan.nextInt(); + final int W = scan.nextInt(); + // 最も見やすい席の座標 + final int bestx = scan.nextInt(); + final int besty = scan.nextInt(); + // 映画館の生成 + Theatre theatre = new Theatre(H, W, bestx, besty); + // 予約済みの席の登録 + for (int i = 0; i < N; i++) { + int x = scan.nextInt(); + int y = scan.nextInt(); + theatre.addReserved(x, y); + } + // 見やすい席の探索 + List> goodSeat = theatre.findGoodSeat(); + // 探索結果の表示xとyがMapで格納されている + for (int i = 0; i < goodSeat.size(); i++) { + System.out.println(goodSeat.get(i).get("x") + " " + goodSeat.get(i).get("y")); + } + scan.close(); + } + + static class Theatre { + public int height, width, bestx, besty; + // false:空席 true:予約済み + public boolean[][] theatre; + + // コンストラクタ + // 任意の大きさの映画館生成 + Theatre(int height, int width, int bestx, int besty) { + this.height = height; + this.width = width; + this.bestx = bestx; + this.besty = besty; + theatre = new boolean[this.height][this.width]; + } + + // 予約済みの席の登録を行う関数 + public void addReserved(int x, int y) { + theatre[x][y] = true; + } + + // 見やすい席の探索 + public List> findGoodSeat() { + // 現時点で一番短いマンハッタン距離 + // 初期値は最大値+1 + int minTaxiDistance = height * width + 1; + List> ans = new ArrayList<>(); + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + // マンハッタン距離計算 + if (theatre[H][W] == true) { + continue; + } + int taxiDistance = Math.abs(H - bestx) + Math.abs(W - besty); + if (taxiDistance <= minTaxiDistance) { + minTaxiDistance = taxiDistance; + } + } + } + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + if (theatre[H][W] == true) { + continue; + } + int taxiDistance = Math.abs(H - bestx) + Math.abs(W - besty); + if (taxiDistance == minTaxiDistance) { + // 見やすい席の座標をMapにぶち込む + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", H); + seat_coordinate.put("y", W); + ans.add(seat_coordinate); + } + } + } + return ans; + } + } +} diff --git a/ykoiso/src/Eiga.java b/ykoiso/src/Eiga.java new file mode 100644 index 0000000000000000000000000000000000000000..ae816505855bf70eee63709362461ea7968610d6 --- /dev/null +++ b/ykoiso/src/Eiga.java @@ -0,0 +1,87 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Eiga { + // 予約されていない席の中で最も見やすい席を探す + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 予約済みの席 + final int N = scan.nextInt(); + final int H = scan.nextInt(); + final int W = scan.nextInt(); + // 最も見やすい席の座標 + final int bestx = scan.nextInt(); + final int besty = scan.nextInt(); + // 映画館の生成 + Theatre theatre = new Theatre(H, W, bestx, besty); + // 予約済みの席の登録 + for (int i = 0; i < N; i++) { + int x = scan.nextInt(); + int y = scan.nextInt(); + theatre.addReserved(x, y); + } + // 見やすい席の探索 + List goodSeat = theatre.findGoodSeat(); + // 探索結果の表示x、y、の順に格納されている + for (int i = 0; i < goodSeat.size(); i++) { + System.out.println(goodSeat.get(i) + " " + goodSeat.get(++i)); + } + scan.close(); + } + + static class Theatre { + public int height, width, bestx, besty; + // false:空席 true:予約済み + public boolean[][] theatre; + + // コンストラクタ + // 任意の大きさの映画館生成 + Theatre(int height, int width, int bestx, int besty) { + this.height = height; + this.width = width; + this.bestx = bestx; + this.besty = besty; + theatre = new boolean[this.height][this.width]; + } + + // 予約済みの席の登録を行う関数 + public void addReserved(int x, int y) { + theatre[x][y] = true; + } + + // 見やすい席の探索 + public List findGoodSeat() { + // 現時点で一番短いマンハッタン距離 + // 初期値は最大値+1 + int minTaxiDistance = height * width + 1; + List ans = new ArrayList<>(); + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + // マンハッタン距離計算 + if (theatre[H][W] == true) { + continue; + } + int taxiDistance = Math.abs(H - bestx) + Math.abs(W - besty); + if (taxiDistance <= minTaxiDistance) { + minTaxiDistance = taxiDistance; + } + } + } + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + if (theatre[H][W] == true) { + continue; + } + int taxiDistance = Math.abs(H - bestx) + Math.abs(W - besty); + if (taxiDistance == minTaxiDistance) { + // 見やすい席の座標をx,yの順にぶち込む + ans.add(H); + ans.add(W); + } + } + } + return ans; + } + } +} diff --git a/ykoiso/test/B034_RobotTest.java b/ykoiso/test/B034_RobotTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e33fe0a0c7fc925fb0e173946f10fc9915111d29 --- /dev/null +++ b/ykoiso/test/B034_RobotTest.java @@ -0,0 +1,211 @@ +import static org.junit.Assert.assertThat; +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import static org.hamcrest.CoreMatchers.*; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class B034_RobotTest { + public static class 方向転換テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 1, 1, 1); + } + + @Test + public void 右に方向転換できる() { + sut.executeOrder("t", "R"); + int actual = sut.getDeg(); + assertThat(actual, is(90)); + } + + @Test + public void 後ろに方向転換できる() { + sut.executeOrder("t", "B"); + int actual = sut.getDeg(); + assertThat(actual, is(180)); + } + + @Test + public void 左に方向転換できる() { + sut.executeOrder("t", "L"); + int actual = sut.getDeg(); + assertThat(actual, is(270)); + } + + } + public static class 前向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 1}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {2, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -3}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-4, 0}; + assertThat(actual, is(expected)); + } + + } + public static class 右向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.changeDirection("R"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {1, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -2}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-3, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 4}; + assertThat(actual, is(expected)); + } + + } + + + + public static class 後ろ向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.executeOrder("t", "B"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -1}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-2, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 3}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {4, 0}; + assertThat(actual, is(expected)); + } + + } + public static class 左向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.changeDirection("L"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-1, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 2}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {3, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -4}; + assertThat(actual, is(expected)); + } + + } +} diff --git a/ykoiso/test/B109_EigaTest.java b/ykoiso/test/B109_EigaTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f6766f7063d135e77052ddf07f6711ca9534fc85 --- /dev/null +++ b/ykoiso/test/B109_EigaTest.java @@ -0,0 +1,119 @@ +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.*; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RunWith(Enclosed.class) +public class B109_EigaTest { + public static class 大きさ3の映画館で何も予約していないとき { + B109_Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると中心の座標を返す() { + sut = new B109_Eiga.Theatre(3, 3, 1, 1); + List> actual = sut.findGoodSeat(); + List> expected = new ArrayList<>(); + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 1); + expected.add(seat_coordinate); + assertThat(actual, is(expected)); + } + + @Test + public void 左端を見やすい席にすると左端の座標を返す() { + sut = new B109_Eiga.Theatre(3, 3, 0, 0); + List> actual = sut.findGoodSeat(); + List> expected = new ArrayList<>(); + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 0); + seat_coordinate.put("y", 0); + expected.add(seat_coordinate); + assertThat(actual, is(expected)); + } + } + public static class 大きさ3の映画館で中心を予約しているとき { + B109_Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると中心の上下左右1マスの座標を返す() { + sut = new B109_Eiga.Theatre(3, 3, 1, 1); + sut.addReserved(1, 1); + List> actual = sut.findGoodSeat(); + List> expected = new ArrayList<>(); + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 0); + seat_coordinate.put("y", 1); + expected.add(seat_coordinate); + seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 0); + expected.add(seat_coordinate); + seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 2); + expected.add(seat_coordinate); + seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 2); + seat_coordinate.put("y", 1); + expected.add(seat_coordinate); + assertThat(actual, is(expected)); + } + } + public static class 大きさ3の映画館で中心とその上の席を予約しているとき { + B109_Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると上を除く上下左右1マスの座標を返す() { + sut = new B109_Eiga.Theatre(3, 3, 1, 1); + sut.addReserved(1, 1); + sut.addReserved(0, 1); + List> actual = sut.findGoodSeat(); + List> expected = new ArrayList<>(); + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 0); + expected.add(seat_coordinate); + seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 2); + expected.add(seat_coordinate); + seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 2); + seat_coordinate.put("y", 1); + expected.add(seat_coordinate); + assertThat(actual, is(expected)); + } + } + public static class 大きさ3の映画館で中心以外の8か所を予約しているとき { + B109_Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると中心の座標を返す() { + sut = new B109_Eiga.Theatre(3, 3, 1, 1); + sut.addReserved(0, 0); + sut.addReserved(0, 1); + sut.addReserved(0, 2); + sut.addReserved(1, 0); + sut.addReserved(1, 2); + sut.addReserved(2, 0); + sut.addReserved(2, 1); + sut.addReserved(2, 2); + List> actual = sut.findGoodSeat(); + List> expected = new ArrayList<>(); + Map seat_coordinate = new HashMap<>(); + seat_coordinate.put("x", 1); + seat_coordinate.put("y", 1); + expected.add(seat_coordinate); + assertThat(actual, is(expected)); + } + } +} diff --git a/ykoiso/test/EigaTest.java b/ykoiso/test/EigaTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4c1f89e195ab62f15ea44ad86e12c91aab8da218 --- /dev/null +++ b/ykoiso/test/EigaTest.java @@ -0,0 +1,62 @@ +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.*; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@RunWith(Enclosed.class) +public class EigaTest { + public static class 大きさ3の映画館で何も予約していないとき { + Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると中心の座標を返す() { + sut = new Eiga.Theatre(3, 3, 1, 1); + List actual = sut.findGoodSeat(); + List expected = new ArrayList<>(); + expected.add(1); + expected.add(1); + assertThat(actual, is(expected)); + } + + @Test + public void 左端を見やすい席にすると左端の座標を返す() { + sut = new Eiga.Theatre(3, 3, 0, 0); + List actual = sut.findGoodSeat(); + List expected = new ArrayList<>(); + expected.add(0); + expected.add(0); + assertThat(actual, is(expected)); + } + } + public static class 大きさ3の映画館で中心を予約しているとき { + Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると中心の上下左右1マスの座標を返す() { + sut = new Eiga.Theatre(3, 3, 1, 1); + sut.addReserved(1, 1); + List actual = sut.findGoodSeat(); + List expected = new ArrayList<>(Arrays.asList(0, 1, 1, 0, 1, 2, 2, 1)); + assertThat(actual, is(expected)); + } + } + public static class 大きさ3の映画館で中心とその上の席を予約しているとき { + Eiga.Theatre sut; + + @Test + public void 中心を見やすい席にすると上を除く上下左右1マスの座標を返す() { + sut = new Eiga.Theatre(3, 3, 1, 1); + sut.addReserved(1, 1); + sut.addReserved(0, 1); + List actual = sut.findGoodSeat(); + List expected = new ArrayList<>(Arrays.asList(1, 0, 1, 2, 2, 1)); + assertThat(actual, is(expected)); + } + } +}