diff --git a/sitou/src/B099.java b/sitou/src/B099.java new file mode 100644 index 0000000000000000000000000000000000000000..ea9bae9d551f700aac06e04fb81db378db44f73d --- /dev/null +++ b/sitou/src/B099.java @@ -0,0 +1,83 @@ +package src; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B099 { + private static int routeSize; + private static int dangerousRainfall; + private static final List rainfallList = new ArrayList<>(); + private PrintStream out; + + public B099(InputStream in, PrintStream out) { + final Scanner scan = new Scanner(in); + + try { + routeSize = scan.nextInt(); + dangerousRainfall = scan.nextInt(); + //ルートごとでなく、ルートのn番目に訪れる場所ごとのリスト + for (int pointNum = 0; pointNum < routeSize; pointNum++) { + List points = new ArrayList<>(); + for (int routeNum = 0; routeNum < routeSize; routeNum++) { + points.add(scan.nextInt()); + } + RainfallPoints p = new RainfallPoints(points); + rainfallList.add(p); + } + } finally { + scan.close(); + } + this.out = out; + } + + public static void main(String[] args) { + final B099 b099 = new B099(System.in, System.out); + b099.execute(); + } + + public void execute() { + final String result = searchRoute(); + output(result, out); + } + + public static String searchRoute() { + final List possibleRouteNum = new ArrayList<>(); + + for (int routeNum = 0; routeNum < routeSize; routeNum++) { + final List route = new ArrayList<>(); + + //ポイント番号ごとにRainfallPointsを呼んでルートを作成 + //routeNumが0のとき、pointNum=0の0番目,pointNum=1の0番目... + //routeNumが1のとき、pointNum=0の1番目,pointNum=1の1番目...と作成 + for (int pointNum = 0; pointNum < routeSize; pointNum++) { + RainfallPoints p = rainfallList.get(pointNum); + route.add(p.getPoint(routeNum)); + } + + Route r = new Route(route); + if (r.canAttend(dangerousRainfall)) { + possibleRouteNum.add(routeNum + 1); + } + } + + //通れる道があったか + if (possibleRouteNum.isEmpty()) { + return "wait"; + } else { + String result = ""; + for (Integer n : possibleRouteNum) { + result += n + " "; + } + //最後の空白を削除して返す + return result.substring(0, result.length() - 1); + } + } + + private static void output(final String result, final PrintStream out) { + out.println(result); + } + +} diff --git a/sitou/src/RainfallPoints.java b/sitou/src/RainfallPoints.java new file mode 100644 index 0000000000000000000000000000000000000000..d70ea872049c5a079245f7d1874e09eae1a9e9d4 --- /dev/null +++ b/sitou/src/RainfallPoints.java @@ -0,0 +1,21 @@ +package src; + +import java.util.ArrayList; +import java.util.List; + +public class RainfallPoints { +private List point = new ArrayList<>(); + + + public RainfallPoints(List list) { + point = list; + } + + public Object getPoint(){ + return point; + } + + public int getPoint(int routeNum) { + return point.get(routeNum); + } +} diff --git a/sitou/src/Route.java b/sitou/src/Route.java new file mode 100644 index 0000000000000000000000000000000000000000..81ff542aee0fa4d43cc345cb43b192fef557bcfa --- /dev/null +++ b/sitou/src/Route.java @@ -0,0 +1,22 @@ +package src; + +import java.util.ArrayList; +import java.util.List; + +public class Route { + private List route = new ArrayList<>(); + + public Route(final List route) { + this.route = route; + } + + public boolean canAttend(final int dangerousRainfall) { + //通れたらtrue、通れなければfalse + for (int rainfall : route) { + if (rainfall >= dangerousRainfall) { + return false; + } + } + return true; + } +} diff --git a/sitou/test/B099_Test.java b/sitou/test/B099_Test.java new file mode 100644 index 0000000000000000000000000000000000000000..ec1222261f98a8186fbdfa36a7d427b44264a981 --- /dev/null +++ b/sitou/test/B099_Test.java @@ -0,0 +1,126 @@ +package test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import src.B099; +import src.Route; + +public class B099_Test { + @Test + public void 正しく動くかテスト() { + final InputStream is = new ByteArrayInputStream(("3 200\n" + + "200 200 20\n" + + "100 20 20\n" + + "500 20 20\n").getBytes()); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final PrintStream ps = new PrintStream(baos); + + final B099 b099 = new B099(is, ps); + b099.execute(); + + final String result = baos.toString(); + assertEquals("3"+ System.lineSeparator(), result); + } + + @Test + public void 全ての道が通れないとき() { + final int routeSize = 3; + final int dangerousRainfall = 100; + final List> rainfallList = Arrays.asList(Arrays.asList(1000,1000,1000),Arrays.asList(1000,1000,1000),Arrays.asList(1000,1000,1000)); + + final List possibleRouteNum = new ArrayList<>(); + for (int i = 0; i < routeSize; i++) { + final List route = new ArrayList<>(); + for (int j = 0; j < routeSize; j++) { + route.add(rainfallList.get(j).get(i)); + } + Route r = new Route(route); + if (r.canAttend(dangerousRainfall)) { + possibleRouteNum.add(i + 1); + } + } + + String result = ""; + if (possibleRouteNum.isEmpty()) { + result = "wait"; + } else { + for (Integer n : possibleRouteNum) { + result += n + " "; + } + result = result.substring(0, result.length() - 1); + } + + assertThat(result, is("wait")); + } + + @Test + public void 全ての道が通れるとき() { + final int routeSize = 3; + final int dangerousRainfall = 50; + final List> rainfallList = Arrays.asList(Arrays.asList(40,40,40),Arrays.asList(40,40,40),Arrays.asList(40,40,40)); + + final List possibleRouteNum = new ArrayList<>(); + for (int i = 0; i < routeSize; i++) { + final List route = new ArrayList<>(); + for (int j = 0; j < routeSize; j++) { + route.add(rainfallList.get(j).get(i)); + } + Route r = new Route(route); + if (r.canAttend(dangerousRainfall)) { + possibleRouteNum.add(i + 1); + } + } + + String result = ""; + if (possibleRouteNum.isEmpty()) { + result = "wait"; + } else { + for (Integer n : possibleRouteNum) { + result += n + " "; + } + result = result.substring(0, result.length() - 1); + } + + assertThat(result, is("1 2 3")); + } + + @Test + public void danregousRainfall境界値() { + final int routeSize = 3; + final int dangerousRainfall = 100; + final List> rainfallList = Arrays.asList(Arrays.asList(99,100,101),Arrays.asList(99,100,101),Arrays.asList(99,100,101)); + + final List possibleRouteNum = new ArrayList<>(); + for (int i = 0; i < routeSize; i++) { + final List route = new ArrayList<>(); + for (int j = 0; j < routeSize; j++) { + route.add(rainfallList.get(j).get(i)); + } + Route r = new Route(route); + if (r.canAttend(dangerousRainfall)) { + possibleRouteNum.add(i + 1); + } + } + + String result = ""; + if (possibleRouteNum.isEmpty()) { + result = "wait"; + } else { + for (Integer n : possibleRouteNum) { + result += n + " "; + } + result = result.substring(0, result.length() - 1); + } + + assertThat(result, is("1")); + } +}