package com.sarojaba.algospot;
import java.util.Scanner;
* [http://algospot.com/judge/problem/read/BOGGLE](http://algospot.com/judge/problem/read/BOGGLE)
*
* @author 준혁
*
*/
public class BOGGLE {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int C = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < C; i++) {
Board b = new Board(5, 5);
for (int j = 0; j < 5; j++) {
String line = scanner.nextLine();
for (int k = 0; k < 5; k++) {
b.set(j, k, line.charAt(k));
}
}
int N = Integer.parseInt(scanner.nextLine());
for (int j = 0; j < N; j++) {
String line = scanner.nextLine();
String yn = b.search(line) ? "YES" : "NO";
System.out.println(String.format("%s %s", line, yn));
}
}
scanner.close();
}
}
* implementation of boggle board
*
* @author 준혁
*
*/
class Board {
* board
*/
private char[][] board;
* constructor
*
* @param r
* row
* @param c
* col
*/
public Board(int r, int c) {
board = new char[r][c];
}
* setter
*
* @param r
* index of row
* @param c
* index of column
* @param value
*/
public void set(int r, int c, char value) {
board[r][c] = value;
}
* exhaustive search
*
* @param word
* @return match
*/
public boolean search(String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (search(i, j, word)) {
return true;
}
}
}
return false;
}
*
* @param r
* row
* @param c
* col
* @param word
* @return match
*/
private boolean search(int r, int c, String word) {
if (word.isEmpty()) {
return true;
}
if (r < 0 || r > board.length - 1) {
return false;
}
if (c < 0 || c > board[r].length - 1) {
return false;
}
if (board[r][c] != word.charAt(0)) {
return false;
}
String w = word.substring(1);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if(i == 0 && j == 0) {
continue;
}
if (search(r + i, c + j, w)) {
return true;
}
}
}
return false;
}
}