From a523ebc323ffd9a71351d509a91a87108ab29f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 1 Aug 2022 11:49:16 +0900 Subject: [PATCH 1/4] =?UTF-8?q?B057=20=E6=9C=80=E9=95=B7=E3=82=B9=E3=83=AF?= =?UTF-8?q?=E3=82=A4=E3=83=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A057_Swipe.java | 178 ++++++++++++++++++++++++++++++++ ykoiso/test/A057_SwipeTest.java | 68 ++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 ykoiso/src/A057_Swipe.java create mode 100644 ykoiso/test/A057_SwipeTest.java diff --git a/ykoiso/src/A057_Swipe.java b/ykoiso/src/A057_Swipe.java new file mode 100644 index 0000000..554358e --- /dev/null +++ b/ykoiso/src/A057_Swipe.java @@ -0,0 +1,178 @@ +import java.util.Scanner; + +public class A057_Swipe { + public static void main(String[] args) { + final Scanner scan = new Scanner(System.in); + final int N = scan.nextInt(); + final int[][] squaresNum = new int[N][N]; + // 最大連続数 + int max = 0; + // 連続数の計算結果 + int currentMax = 0; + String str; + String[] strings; + // splitしたものをint型に直す + for (int y = 0; y < N; y++) { + str = scan.next(); + strings = str.split(""); + for (int x = 0; x < N; x++) { + squaresNum[y][x] = Integer.parseInt(strings[x]); + } + } + // それぞれほ方向でmaxを更新していく + for (int y = 0; y < N; y++) { + for (int x = 0; x < N; x++) { + currentMax = checkBeside(squaresNum, N, y, x); + max = checkMax(max, currentMax); + currentMax = checkVertical(squaresNum, N, y, x); + max = checkMax(max, currentMax); + currentMax = checkRightDia(squaresNum, N, y, x); + max = checkMax(max, currentMax); + currentMax = checkLeftDia(squaresNum, N, y, x); + max = checkMax(max, currentMax); + } + } + // 結果の出力 + System.out.println(max); + scan.close(); + } + + // maxを更新できるかの判定 + public static int checkMax(final int max, final int currentMax) { + if (currentMax > max) { + return currentMax; + } + return max; + } + + // 横に向けての計算 + // 増減両方判断するので反対側は必要なし + public static int checkBeside(final int[][] squaresNum, final int N, final int y, final int x) { + final int ruleNum; + // 連続できないなら1を返す + if (x == N - 1) { + return 1; + } + int target = squaresNum[y][x]; + int targetBeside = squaresNum[y][x + 1]; + // 連続の規則性を判断 + // 連続しない場合1を返す + if (target + 1 == targetBeside) { + ruleNum = +1; + } else if (target - 1 == targetBeside) { + ruleNum = -1; + } else { + return 1; + } + // 連続数の計算 + int max = 1; + for (int currentX = x; currentX < N - 1; currentX++) { + target = squaresNum[y][currentX]; + targetBeside = squaresNum[y][currentX + 1]; + if (target + ruleNum == targetBeside) { + max++; + } else { + break; + } + } + return max; + } + + // 縦に対しての計算 + public static int checkVertical(final int[][] squaresNum, final int N, final int y, + final int x) { + final int ruleNum; + if (y == N - 1) { + return 1; + } + int target = squaresNum[y][x]; + int targetVertical = squaresNum[y + 1][x]; + if (target + 1 == targetVertical) { + ruleNum = +1; + } else if (target - 1 == targetVertical) { + ruleNum = -1; + } else { + return 1; + } + int max = 1; + for (int currentY = y; currentY < N - 1; currentY++) { + target = squaresNum[currentY][x]; + targetVertical = squaresNum[currentY + 1][x]; + if (target + ruleNum == targetVertical) { + max++; + } else { + break; + } + } + return max; + } + + // 右斜め下に対しての計算 + public static int checkRightDia(final int[][] squaresNum, final int N, final int y, + final int x) { + final int ruleNum; + if (y == N - 1 || x == N - 1) { + return 1; + } + int target = squaresNum[y][x]; + int targetRightDia = squaresNum[y + 1][x + 1]; + if (target + 1 == targetRightDia) { + ruleNum = +1; + } else if (target - 1 == targetRightDia) { + ruleNum = -1; + } else { + return 1; + } + // xとyの両方が変わるためwhile文に + int currentX = x; + int currentY = y; + int max = 1; + while (currentY < N - 1 && currentX < N - 1) { + target = squaresNum[currentY][currentX]; + targetRightDia = squaresNum[currentY + 1][currentX + 1]; + if (target + ruleNum == targetRightDia) { + max++; + // 値のインクリメント + currentX++; + currentY++; + } else { + break; + } + } + return max; + } + + // 左斜めに対しての計算 + public static int checkLeftDia(final int[][] squaresNum, final int N, final int y, + final int x) { + final int ruleNum; + if (y == N - 1 || x == 0) { + return 1; + } + int target = squaresNum[y][x]; + int targetLeftDia = squaresNum[y + 1][x - 1]; + if (target + 1 == targetLeftDia) { + ruleNum = +1; + } else if (target - 1 == targetLeftDia) { + ruleNum = -1; + } else { + return 1; + } + int currentX = x; + int currentY = y; + int max = 1; + while (currentY < N - 1 && currentX > 0) { + target = squaresNum[currentY][currentX]; + targetLeftDia = squaresNum[currentY + 1][currentX - 1]; + if (target + ruleNum == targetLeftDia) { + max++; + // 反対方向なのでxはデクリメント + currentX--; + currentY++; + } else { + break; + } + } + return max; + } +} diff --git a/ykoiso/test/A057_SwipeTest.java b/ykoiso/test/A057_SwipeTest.java new file mode 100644 index 0000000..e37ab37 --- /dev/null +++ b/ykoiso/test/A057_SwipeTest.java @@ -0,0 +1,68 @@ +import static org.junit.Assert.assertThat; +import org.junit.Test; +import static org.hamcrest.CoreMatchers.*; + +public class A057_SwipeTest { + int[][] map = { + {1, 2, 3, 4, 5,}, + {2, 5, 1, 4, 5,}, + {3, 5, 3, 2, 5,}, + {5, 2, 5, 5, 3,}, + {1, 5, 5, 5, 5,} + }; + final int N = 5;; + int x; + int y; + A057_Swipe swipe = new A057_Swipe(); + + @Test + public void 横に対して計算できる() { + y = 0; + x = 0; + final int actual = swipe.checkBeside(map, N, y, x); + final int expected = 5; + assertThat(actual, is(expected)); + } + @Test + public void 縦に対して計算できる() { + y = 0; + x = 0; + final int actual = swipe.checkVertical(map, N, y, x); + final int expected = 3; + assertThat(actual, is(expected)); + } + @Test + public void 斜め右に対して計算できる() { + y = 1; + x = 2; + final int actual = swipe.checkRightDia(map, N, y, x); + final int expected = 3; + assertThat(actual, is(expected)); + } + @Test + public void 斜め左に対して計算できる() { + y = 0; + x = 4; + final int actual = swipe.checkLeftDia(map, N, y, x); + final int expected = 5; + assertThat(actual, is(expected)); + } + @Test + public void 計算できない位置で呼び出したとき1を返す() { + y = 2; + x = 4; + final int actual = swipe.checkBeside(map, N, y, x); + final int expected = 1; + assertThat(actual, is(expected)); + } + @Test + public void 連続していないとき1を返す() { + y = 1; + x = 0; + final int actual = swipe.checkBeside(map, N, y, x); + final int expected = 1; + assertThat(actual, is(expected)); + } + + +} -- GitLab From 2f392e064bd742d8ab1bc0162538f588dfdf37e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 1 Aug 2022 15:30:02 +0900 Subject: [PATCH 2/4] =?UTF-8?q?A057=20=E6=9C=80=E9=95=B7=E3=82=B9=E3=83=AF?= =?UTF-8?q?=E3=82=A4=E3=83=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A057_Swipe.java | 62 ++++++++++++++++----------------- ykoiso/test/A057_SwipeTest.java | 25 ++++++++----- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/ykoiso/src/A057_Swipe.java b/ykoiso/src/A057_Swipe.java index 554358e..9a33005 100644 --- a/ykoiso/src/A057_Swipe.java +++ b/ykoiso/src/A057_Swipe.java @@ -5,10 +5,10 @@ public class A057_Swipe { final Scanner scan = new Scanner(System.in); final int N = scan.nextInt(); final int[][] squaresNum = new int[N][N]; - // 最大連続数 - int max = 0; - // 連続数の計算結果 - int currentMax = 0; + // スワイプの最大回数 + int maxSwipeCount = 0; + // 計算ごとの連続スワイプ数 + int currentSwipeCount = 0; String str; String[] strings; // splitしたものをint型に直す @@ -19,30 +19,30 @@ public class A057_Swipe { squaresNum[y][x] = Integer.parseInt(strings[x]); } } - // それぞれほ方向でmaxを更新していく + // それぞれの方向で最大連続数を更新していく for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { - currentMax = checkBeside(squaresNum, N, y, x); - max = checkMax(max, currentMax); - currentMax = checkVertical(squaresNum, N, y, x); - max = checkMax(max, currentMax); - currentMax = checkRightDia(squaresNum, N, y, x); - max = checkMax(max, currentMax); - currentMax = checkLeftDia(squaresNum, N, y, x); - max = checkMax(max, currentMax); + currentSwipeCount = checkBeside(squaresNum, N, y, x); + maxSwipeCount = updateMaxSwipeCount(maxSwipeCount, currentSwipeCount); + currentSwipeCount = checkVertical(squaresNum, N, y, x); + maxSwipeCount = updateMaxSwipeCount(maxSwipeCount, currentSwipeCount); + currentSwipeCount = checkRightDia(squaresNum, N, y, x); + maxSwipeCount = updateMaxSwipeCount(maxSwipeCount, currentSwipeCount); + currentSwipeCount = checkLeftDia(squaresNum, N, y, x); + maxSwipeCount = updateMaxSwipeCount(maxSwipeCount, currentSwipeCount); } } // 結果の出力 - System.out.println(max); + System.out.println(maxSwipeCount); scan.close(); } - // maxを更新できるかの判定 - public static int checkMax(final int max, final int currentMax) { - if (currentMax > max) { - return currentMax; + // 最大連続数を更新できるかの判定 + public static int updateMaxSwipeCount(final int maxSwipeCount, final int currentSwipeCount) { + if (currentSwipeCount > maxSwipeCount) { + return currentSwipeCount; } - return max; + return maxSwipeCount; } // 横に向けての計算 @@ -65,17 +65,17 @@ public class A057_Swipe { return 1; } // 連続数の計算 - int max = 1; + int maxSwipeCount = 1; for (int currentX = x; currentX < N - 1; currentX++) { target = squaresNum[y][currentX]; targetBeside = squaresNum[y][currentX + 1]; if (target + ruleNum == targetBeside) { - max++; + maxSwipeCount++; } else { break; } } - return max; + return maxSwipeCount; } // 縦に対しての計算 @@ -94,17 +94,17 @@ public class A057_Swipe { } else { return 1; } - int max = 1; + int maxSwipeCount = 1; for (int currentY = y; currentY < N - 1; currentY++) { target = squaresNum[currentY][x]; targetVertical = squaresNum[currentY + 1][x]; if (target + ruleNum == targetVertical) { - max++; + maxSwipeCount++; } else { break; } } - return max; + return maxSwipeCount; } // 右斜め下に対しての計算 @@ -126,12 +126,12 @@ public class A057_Swipe { // xとyの両方が変わるためwhile文に int currentX = x; int currentY = y; - int max = 1; + int maxSwipeCount = 1; while (currentY < N - 1 && currentX < N - 1) { target = squaresNum[currentY][currentX]; targetRightDia = squaresNum[currentY + 1][currentX + 1]; if (target + ruleNum == targetRightDia) { - max++; + maxSwipeCount++; // 値のインクリメント currentX++; currentY++; @@ -139,7 +139,7 @@ public class A057_Swipe { break; } } - return max; + return maxSwipeCount; } // 左斜めに対しての計算 @@ -160,12 +160,12 @@ public class A057_Swipe { } int currentX = x; int currentY = y; - int max = 1; + int maxSwipeCount = 1; while (currentY < N - 1 && currentX > 0) { target = squaresNum[currentY][currentX]; targetLeftDia = squaresNum[currentY + 1][currentX - 1]; if (target + ruleNum == targetLeftDia) { - max++; + maxSwipeCount++; // 反対方向なのでxはデクリメント currentX--; currentY++; @@ -173,6 +173,6 @@ public class A057_Swipe { break; } } - return max; + return maxSwipeCount; } } diff --git a/ykoiso/test/A057_SwipeTest.java b/ykoiso/test/A057_SwipeTest.java index e37ab37..a2087bc 100644 --- a/ykoiso/test/A057_SwipeTest.java +++ b/ykoiso/test/A057_SwipeTest.java @@ -7,16 +7,16 @@ public class A057_SwipeTest { {1, 2, 3, 4, 5,}, {2, 5, 1, 4, 5,}, {3, 5, 3, 2, 5,}, - {5, 2, 5, 5, 3,}, - {1, 5, 5, 5, 5,} + {5, 2, 5, 5, 1,}, + {1, 5, 5, 4, 5,} }; - final int N = 5;; + final int N = 5; int x; int y; A057_Swipe swipe = new A057_Swipe(); @Test - public void 横に対して計算できる() { + public void 横方向に対しての値の増加をカウントできる() { y = 0; x = 0; final int actual = swipe.checkBeside(map, N, y, x); @@ -24,7 +24,7 @@ public class A057_SwipeTest { assertThat(actual, is(expected)); } @Test - public void 縦に対して計算できる() { + public void 縦に対しての値の増加をカウントできる() { y = 0; x = 0; final int actual = swipe.checkVertical(map, N, y, x); @@ -32,15 +32,15 @@ public class A057_SwipeTest { assertThat(actual, is(expected)); } @Test - public void 斜め右に対して計算できる() { + public void 斜め右に対して値の上下を正しくカウントできる() { y = 1; x = 2; final int actual = swipe.checkRightDia(map, N, y, x); - final int expected = 3; + final int expected = 2; assertThat(actual, is(expected)); } @Test - public void 斜め左に対して計算できる() { + public void 斜め左に対して値の減少をカウントできる() { y = 0; x = 4; final int actual = swipe.checkLeftDia(map, N, y, x); @@ -63,6 +63,13 @@ public class A057_SwipeTest { final int expected = 1; assertThat(actual, is(expected)); } - + @Test + public void 値が上下するような場合はカウントされない() { + y = 4; + x = 2; + final int actual = swipe.checkBeside(map, N, y, x); + final int expected = 2; + assertThat(actual, is(expected)); + } } -- GitLab From 2d6672492d148bed1971054018c682b583248b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 1 Aug 2022 15:43:53 +0900 Subject: [PATCH 3/4] =?UTF-8?q?A057=20=E6=9C=80=E9=95=B7=E3=82=B9=E3=83=AF?= =?UTF-8?q?=E3=82=A4=E3=83=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A057_Swipe.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ykoiso/src/A057_Swipe.java b/ykoiso/src/A057_Swipe.java index 9a33005..9630bee 100644 --- a/ykoiso/src/A057_Swipe.java +++ b/ykoiso/src/A057_Swipe.java @@ -78,7 +78,8 @@ public class A057_Swipe { return maxSwipeCount; } - // 縦に対しての計算 + // 縦に向かって数字が連続しているかを計算 + //いくつ連続しているかを返す public static int checkVertical(final int[][] squaresNum, final int N, final int y, final int x) { final int ruleNum; -- GitLab From 34d03e8b1f249679fc0f20e241058a931d97f678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 1 Aug 2022 16:15:47 +0900 Subject: [PATCH 4/4] =?UTF-8?q?A057=20=E6=9C=80=E9=95=B7=E3=82=B9=E3=83=AF?= =?UTF-8?q?=E3=82=A4=E3=83=97=E4=BF=AE=E6=AD=A32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A057_Swipe.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ykoiso/src/A057_Swipe.java b/ykoiso/src/A057_Swipe.java index 9630bee..357dd57 100644 --- a/ykoiso/src/A057_Swipe.java +++ b/ykoiso/src/A057_Swipe.java @@ -37,7 +37,8 @@ public class A057_Swipe { scan.close(); } - // 最大連続数を更新できるかの判定 + // 最大連続数を可能であれば更新する + //(判定する場合にはcan) public static int updateMaxSwipeCount(final int maxSwipeCount, final int currentSwipeCount) { if (currentSwipeCount > maxSwipeCount) { return currentSwipeCount; -- GitLab