diff --git a/knoda/src/B089_SecretWord.java b/knoda/src/B089_SecretWord.java new file mode 100644 index 0000000000000000000000000000000000000000..e1741ef454b7a21cff6788798af17cf813b457db --- /dev/null +++ b/knoda/src/B089_SecretWord.java @@ -0,0 +1,107 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B089_SecretWord { + + public static void main(String[] args) { + final int INDEX_ZERO = 0; // 0番目のインデックス + final int INDEX_ONE = 1; // 1番目のインデックス + + Scanner scan = new Scanner(System.in); + + final int boardSize = scan.nextInt(); // boardSize : 基盤のサイズ + final int wordNum = scan.nextInt(); // wordNum : 単語の数 + final List characterList = new ArrayList(); // characterList : 基盤に記載されている文字 + + // 基盤に記載されている文字を入力する + for (int i = 0; i < boardSize; i++) { + characterList.add(scan.next()); + } + + // 単語を入力する + final List wordList = new ArrayList(); + for (int i = 0; i < wordNum; i++) { + wordList.add(scan.next()); + } + + + SecretWord secretWord = new SecretWord(); + + // 単語の先頭文字の位置を取得する + final List> rowAndColumnList = + secretWord.getColumnAndRowInWord(characterList, wordList, boardSize); + // 単語の先頭文字の位置を出力する + for (List rowAndColumn : rowAndColumnList) { + System.out.print((rowAndColumn.get(INDEX_ZERO) + 1) + " "); + System.out.println(rowAndColumn.get(INDEX_ONE) + 1); + } + scan.close(); + } + +} + +// 単語の先頭文字の位置を取得するクラス +class SecretWord { + + // それぞれの単語の先頭文字の位置を取得するメソッド + public List> getColumnAndRowInWord(final List characterList, final List wordList, + final int boardSize) { + // columnAndRowList : それぞれの単語の先頭文字の位置を格納するリスト + final List> columnAndRowList = new ArrayList>(); + + // それぞれの単語の先頭文字の位置を取得する + for (String word : wordList) { + // columnAndRow : ある単語の先頭文字の位置を格納するリスト + // ある単語の先頭文字の位置を取得する + List columnAndRow = getColumnAndRow(characterList, word, boardSize); + columnAndRowList.add(columnAndRow); + } + return columnAndRowList; + } + + // ある単語の先頭文字の位置を取得するメソッド + private List getColumnAndRow(final List characterList, final String word, + final int boardSize) { + + // columnAndRow : ある単語の先頭文字の位置を格納するリスト + final List columnAndRow = new ArrayList(); + + boolean isFindWord = false; // isFindWord : 単語が見つかったかの判定(true : 見つかった、false : 見つかっていない) + + // 基盤に書かれている文字を1文字ずつ走査する + for (int row = 0; row < boardSize - word.length() + 1; row++) { + for (int column = 0; column < boardSize - word.length() + 1; column++) { + int tmpRow = row; + int tmpColumn = column; + int wordIndex = 0; // wordIndex : 単語の文字の要素番号 + + // 単語が基盤に存在しているか調べる + while (wordIndex < word.length()) { + if (characterList.get(tmpRow).charAt(tmpColumn) == word.charAt(wordIndex)) { + tmpRow++; + tmpColumn++; + wordIndex++; + } else { + break; + } + } + + // 単語が基盤に存在しているとき + if (wordIndex == word.length()) { + columnAndRow.add(column); + columnAndRow.add(row); + isFindWord = true; + } + } + + // 単語が見つかったら終了 + if (isFindWord) { + break; + } + } + return columnAndRow; + } +} diff --git a/knoda/test/B089_SecretWordTest.java b/knoda/test/B089_SecretWordTest.java new file mode 100644 index 0000000000000000000000000000000000000000..db2d734e7b9a668f092ceffdb199b031ac1b3e1f --- /dev/null +++ b/knoda/test/B089_SecretWordTest.java @@ -0,0 +1,98 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class B089_SecretWordTest { + List characterList; + List wordList; + int boardSize; + SecretWord sut; + + @Test + public void 基盤に単語が存在しているテスト1() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 6; + + characterList.add("HPPLLM"); + characterList.add("UROQUV"); + characterList.add("FBSRZY"); + characterList.add("DPEFKT"); + characterList.add("GBBEUY"); + characterList.add("EMCQFY"); + + wordList.add("BEEF"); + wordList.add("PORK"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3)), + new ArrayList(Arrays.asList(2, 1)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + + @Test + public void 基盤に単語が存在しているテスト2() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 4; + + characterList.add("ACEG"); + characterList.add("HBDF"); + characterList.add("EGAC"); + characterList.add("DFHB"); + + wordList.add("ABA"); + wordList.add("BAB"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 1)), + new ArrayList(Arrays.asList(2, 2)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + + @Test + public void 基盤に単語が存在していないテスト() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 4; + + characterList.add("ACEG"); + characterList.add("HBDF"); + characterList.add("EGAC"); + characterList.add("DFHB"); + + wordList.add("HAF"); + wordList.add("BAB"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(), + new ArrayList(Arrays.asList(2, 2)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + +}