// Alex and Tess /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Wordsearch; import java.awt.Point; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; /** * * @author alexhuleatt */ public class holgar { static char[][] words; static HashMap> letter_posns; public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File("holger.in")); PrintWriter out = new PrintWriter("holger.out"); int numCases = sc.nextInt(); sc.nextLine(); sc.nextLine(); for (int i = 0; i < numCases; i++) { String[] dim = sc.nextLine().split("\\s"); //split along spaces int numRows = Integer.parseInt(dim[0]); //parse out dimensions int numCols = Integer.parseInt(dim[1]); words = new char[numRows][numCols]; letter_posns = new HashMap<>(); for (int j = 0; j < numRows; j++) { //scan in each row String str = sc.nextLine(); int k = 0; for (char c : str.toCharArray()) { //each letter c = Character.toLowerCase(c); //convert to lower case words[j][k] = c; if (letter_posns.get(c) == null) { letter_posns.put(c, new ArrayList()); } letter_posns.get(c).add(new Point(j, k)); k++; } } int numToLookFor = Integer.parseInt(sc.nextLine()); //scan in the number of cases for (int j = 0; j < numToLookFor; j++) { char[] lookingFor = sc.nextLine().toCharArray(); ArrayList letters = letter_posns.get(Character.toLowerCase(lookingFor[0])); if (letters == null) { //if the letter first letter does not exist out.println(-1 + " " + -1); } else { boolean found = false; thisIsMahLabel: //mah label for (int k = 0; k < letters.size(); k++) { //look at every instance that this letter occurs Point current = letters.get(k); int x = (int) current.getX(); int y = (int) current.getY(); for (int l = -1; l <= 1; l++) { //check every directions for (int m = -1; m <= 1; m++) { if (m != 0 || l != 0) { int n; for (n = 1; n < lookingFor.length; n++) { //look for each letter int tempX = x + l * n; int tempY = y + m * n; if (!isValid(tempX, tempY)) { //if the position is out of bounds, break break; } if (words[tempX][tempY] != Character.toLowerCase(lookingFor[n])) { //if the letter found is not the same break; } } if (n == lookingFor.length) { //if you scanned all the way to the end out.println((1 + x) + " " + (1 + y)); found = true; break thisIsMahLabel; //break all the way out of the loops } } } } } if (!found) { out.println(-1 + " " + -1); } } } out.println(); if (sc.hasNext()) { sc.nextLine(); } } out.close(); } public static boolean isValid(int x, int y) { return (x >= 0 && x < words.length && y >= 0 && y < words[0].length); } }