Where's+Waldorf

Source Code code format="java5" import java.util.Scanner;

public class WhereIsWaldorf {

public static void main(String[] args) { Scanner scan = new Scanner(System.in);

int m = scan.nextInt; int n = scan.nextInt;

Grid grid = new Grid(m, n); for (int i = 0; i < m; i++) { String line = scan.next.toLowerCase;

for (int j = 0; j < n; j++) { grid.values[i][j] = line.charAt(j); } }

int k = scan.nextInt;

for (int i = 0; i < k; i++) { grid.search(scan.next.toLowerCase); } } public static class Grid { char[][] values; private int row; private int col;

public Grid(int row, int col) { this.row = row; this.col = col; values = new char[row][col]; } public void search(String word) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (searchLU(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchU(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchRU(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchL(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchR(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchLD(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchD(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } if (searchRD(i, j, word)) { System.out.println((i + 1) + " " + (j + 1)); return; } } } }

private boolean searchLU(int r, int c, String word) { if (word.isEmpty) { return true; } if (r < 0 || c < 0) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchLU(r - 1, c - 1, word.substring(1)); }

private boolean searchU(int r, int c, String word) { if (word.isEmpty) { return true; } if (r < 0) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchU(r - 1, c, word.substring(1)); } private boolean searchRU(int r, int c, String word) { if (word.isEmpty) { return true; } if (r < 0 || c >= col) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchRU(r - 1, c + 1, word.substring(1)); } private boolean searchL(int r, int c, String word) { if (word.isEmpty) { return true; } if (c < 0) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchL(r, c - 1, word.substring(1)); } private boolean searchR(int r, int c, String word) { if (word.isEmpty) { return true; } if (c >= col) { return false; } if (values[r][c] != word.charAt(0)) { return false; }

return searchR(r, c + 1, word.substring(1)); } private boolean searchLD(int r, int c, String word) { if (word.isEmpty) { return true; } if (r >= row || c < 0) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchLD(r + 1, c - 1, word.substring(1)); } private boolean searchD(int r, int c, String word) { if (word.isEmpty) { return true; } if (r >= row) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchD(r + 1, c, word.substring(1)); } private boolean searchRD(int r, int c, String word) { if (word.isEmpty) { return true; } if (r >= row || c >= col) { return false; }

if (values[r][c] != word.charAt(0)) { return false; }

return searchRD(r + 1, c + 1, word.substring(1)); } } } code

Comment 격자에서 문자열을 찾는 알고리즘을 짜는 문제다. 8방향 각각으로 재귀적으로 탐색하는 메소드를 만들었다. 개인적인 생각으로 오목 게임에 이 알고리즘을 응용해보면 좋을 것 같다.