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

[UVA][字串搜尋] 10010 - Where's Waldorf?

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


 Where's Waldorf? 

Given a m by n grid of letters, ($1 leq m,n leq 20$), and a listof words, find the location in the grid at which the word can be found.A word matches a straight, uninterrupted line of letters in the grid.A word can match the letters in the grid regardless of case (i.e. upperand lower case letters are to be treated as the same). The matchingcan be done in any of the eight directions either horizontally, verticallyor diagonally through the grid.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input begins with a pair of integers, m followed by n, $1 leqm,n leq 50$in decimal notation on a single line. The next m linescontain n letters each; this is the grid of letters in which thewords of the list must be found. The letters in the grid may be inupper or lower case. Following the grid of letters, another integerk appears on a line by itself ($1 leq k leq 20$). The next klines of input contain the list of words to search for, one word perline. These words may contain upper and lower case letters only (nospaces, hyphens or other non-alphabetic characters).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each word in the word list, a pair of integers representing thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer is theline in the grid where the first letter of the given word can be found(1 represents the topmost line in the grid, and m represents thebottommost line). The second integer is the column in the grid wherethe first letter of the given word can be found (1 represents theleftmost column in the grid, and n represents the rightmost column inthe grid). If a word can be found more than once in the grid, then thelocation which is output should correspond to the uppermost occurenceof the word (i.e. the occurence which places the first letter of theword closest to the top of the grid). If two or more words areuppermost, the output should correspond to the leftmost of theseoccurences. All words can be found at least once in the grid.

Sample Input 

18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert

Sample Output 

2 52 31 27 8

上下左右任一方向, 不可中途改變, 且不會穿過一邊從另一邊出來, 找尋第一個座標

#include <stdio.h>
#define toLower(c) ((c >= 'A' && c <= 'Z') ? (c-'A'+'a') : (c))
int t, n, m, i, j, k;
void find(char str[], char map[][60]) {
    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 < m; j++) {
            for(k = 0; k < 8; k++) {
                x = i, y = j, idx = 0;
                while(map[x][y] == str[idx]) {
                    x += D[k][0], y += D[k][1];
                    idx++;
                    if(str[idx] == '\0') {
                        printf("%d %d\n", i+1, j+1);
                        return;
                    }
                    if(x < 0 || y < 0 || x >= n || y >= m)
                        break;
                }
            }
        }
    }
}
int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &m);
        char map[60][60], str[60];
        for(i = 0; i < n; i++) {
            scanf("%s", &map[i]);
            for(j = 0; j < m; j++)
                map[i][j] = toLower(map[i][j]);
        }
        scanf("%d", &k);
        while(k--) {
            scanf("%s", str);
            for(i = 0; str[i]; i++)
                str[i] = toLower(str[i]);
            find(str, map);
        }
        if(t)
            puts("");
    }
    return 0;
}

台長: Morris
人氣(1,669) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][字串搜尋] 422 - Word-Search Wonder
此分類上一篇:[UVA][字串處理] 444 - Encoder and Decoder

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