24h購物| | PChome| 登入
2012-05-11 07:26:21| 人氣1,042| 回應0 | 上一篇 | 下一篇

[UVA][字串搜尋] 422 - Word-Search Wonder

推薦 0 收藏 0 轉貼0 訂閱站台


 Word-Search Wonder 

The Pyrates Restaurant was starting to fill up as Valentine McKee walked in. She scanned the crowd for her sister, brother-in-law, and nephew. Seeing her sister waving from the far end of the restaurant, she made her way back to their booth. ``Hi, Valentine,'' her sister and brother-in-law, Niki and Dennis Chapman, greeted her.

``Hi, guys,'' she replied. ``What are you doing, Wade?'' she asked her nephew. He was busy working on one of the restaurant's activity sheets with a crayon.

``I'm doing a word search game,'' Wade explained. ``I have to find all of these words in this big mess of letters. This is really hard.'' Wade looked intently at the paper in front of him.

``Can I help?'' asked Valentine, looking across the table at the activity sheet.

``Sure. These are the words we're looking for. They're the names of different kinds of Planes, Trains, and Automobiles.''

Input

The first line of input will specify the length (in characters) of the sides of the letter matrix (the matrix of letters will be square). The length, l, will be in the range tex2html_wrap_inline28 . The next l lines of input will be the matrix itself, each line will contain l uppercase letters.

A list of words will follow. Each word will be on a line by itself; there will be 100 or fewer words. Each word will be 100 or fewer characters long, and will only contain uppercase letters.

The final line of input will contain a single zero character.

Output

Your program should attempt to find each word from the word list in the puzzle. A word is ``found'' if all the characters in the word can be traced in a single (unidirectional) horizontal, vertical, or diagonal line in the letter matrix. Words may not ``wrap around'' rows or columns, but horizontal and diagonal words may proceed from right to left (``backwards''). For each word that is found, your program should print the coordinates of its first and last letters in the matrix on a single line, separated by a single space. Coordinates are pairs of comma-separated integers (indexed from 1), where the first integer specifies the row number and the second integer specifies the column number.

If a word is not found, the string ``Not found'' should be output instead of a pair of coordinates.

Each word from the input can be ``found'' at most once in the puzzle.

Sample Input

5
EDEEE
DISKE
ESEEE
ECEEE
EEEEE
DISC
DISK
DISP
0

Sample Output

1,2 4,2
2,1 2,4
Not found


#include <stdio.h>
#include <string.h>
int n, i, j;
int find(char str[], char map[][105]) {
    static int D[][2] = {{0,1},{1,0},{-1,0},{0,-1},
                        {1,1},{1,-1},{-1,1},{-1,-1}};
    int i, j, k, x, y, idx;
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            for(k = 0; k < 8; k++) {
                x = i, y = j, idx = 0;
                while(map[x][y] == str[idx]) {
                    idx++;
                    if(str[idx] == '\0') {
                        printf("%d,%d %d,%d\n", i+1, j+1, x+1, y+1);
                        return 1;
                    }
                    x += D[k][0], y += D[k][1];
                    if(x < 0 || y < 0 || x >= n || y >= n)
                        break;
                }
            }
        }
    }
    return 0;
}
int main() {
    while(scanf("%d", &n) == 1) {
        char map[105][105], str[105];
        for(i = 0; i < n; i++) {
            scanf("%s", &map[i]);
        }
        while(scanf("%s", str) == 1) {
            if(!strcmp(str, "0"))
                break;
            if(!find(str, map))
                puts("Not found");
        }
    }
    return 0;
}


台長: Morris
人氣(1,042) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][SA] 11512 - GATTACA
此分類上一篇:[UVA][字串搜尋] 10010 - Where's Waldorf?

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文