24h購物| | PChome| 登入
2013-06-13 20:47:51| 人氣1,335| 回應0 | 上一篇 | 下一篇

[UVA] 604 - The Boggle Game

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


  The Boggle Game 

The language PigEwu has a very simple syntax. Each word in this language has exactly 4 letters. Also each word contains exactly two vowels (y is consider a vowel in PigEwu). For instance, "maar" and "even" are legitimate words, "arts" is not a legal word.

In the game boggle, you are given a 4x4 array of letters and asked to find all words contained in it. A word in our case (PigEwu) will thus be a sequence of 4 distinct squares (letters) that form a legal word and such that each square touches (have a corner or edge in common) the next square.

For example:

      A  S  S  D 
      S  B  E  Y
      G  F  O  I
      H  U  U  K

In this board a (partial) list of legal words include:

ASGU    SABO    FOIK    FOYD    SYDE    HUFO

BEBO is a legal word but it is not on this boggle board (there are no two B's here).

Write a program that reads a pair of Boggle boards and lists all PigEwu words that are common to both boards.

Input 

The input file will include a few data sets. Each data set will be a pair of boards as shown in the sample input. All entries will be upper case letters. Two consecutive entries on same board will be separated by one blank. The first row in the first board will be on the same line as the first row of the second board. They will be separated by four spaces, the same will hold for the remaining 3 rows. Board pairs will be separated by a blank line. The file will be terminated by `#'.

Output 

For each pair of boggle boards, output an alphabetically-sorted list of all common words, each word on a separate line; or the statement "There are no common words for this pair of boggle boards."

Separate the output for each pair of boggle boards with a blank line.

Sample Input 

D F F B    W A S U
T U G I    B R E T
O K J M    Y A P Q
K M B E    L O Y R

Z W A V    G S F U
U N C O    A H F T
Y T G I    G N A L
H G P M    B O O B

#

Sample Output 

There are no common words for this pair of boggle boards.

ANGO
AOGN
GNAO
GOAN
NAOG
NGOA
OANG
OGNA



Miguel A. Revilla
1999-03-24

題目描述:

可以選擇的方向是八個方位,走過不能在走,且四個字母中必須恰好有兩個是母音(AEIOUY)。
然後問兩個盤面的共同字串。

題目解法:

窮舉盤面上所有符合的字串,兩個盤面取交集。

#include <stdio.h>
#include <map>
#include <iostream>
using namespace std;
char g1[10][10], g2[10][10], buf[10];
int dx[] = {0,0,1,1,1,-1,-1,-1};
int dy[] = {1,-1,0,1,-1,0,1,-1};
int used[10][10];
map<int, int> R;
void dfs(int x, int y, int vow, int len, int id) {
    if(vow == 3 || (len == 3 && vow == 0))
        return ;
    if(len == 4) {
        if(vow == 2) {
            int val = 0, i;
            for(i = 0; i < 4; i++)
                val = (val<<5)|(buf[i]-'A');
            int &v = R[val];
            v |= id;
        }
        return ;
    }
    int i, tx, ty;
    for(i = 0; i < 8; i++) {
        tx = x+dx[i], ty = y+dy[i];
        if(tx < 0 || ty < 0 || tx >= 4 || ty >= 4 || used[tx][ty])
            continue;
        if(id == 1) {
            char &v = g1[tx][ty];
            used[tx][ty] = 1, buf[len] = v;
            if(v == 'A' || v == 'E' || v == 'I' || v == 'O' || v == 'U' || v == 'Y')
                dfs(tx, ty, vow+1, len+1, id);
            else
                dfs(tx, ty, vow, len+1, id);
            used[tx][ty] = 0;
        } else {
            char &v = g2[tx][ty];
            used[tx][ty] = 1, buf[len] = v;
            if(v == 'A' || v == 'E' || v == 'I' || v == 'O' || v == 'U' || v == 'Y')
                dfs(tx, ty, vow+1, len+1, id);
            else
                dfs(tx, ty, vow, len+1, id);
            used[tx][ty] = 0;
        }
    }
}
int main() {
    int i, j;
    char s[10];
    int cases = 0;
    while(true) {
        R.clear();
        for(i = 0; i < 4; i++) {
            for(j = 0; j < 4; j++) {
                scanf("%s", s);
                if(s[0] == '#')
                    goto END;
                g1[i][j] = s[0];
            }
            for(j = 0; j < 4; j++) {
                scanf("%s", s);
                g2[i][j] = s[0];
            }
        }
        for(i = 0; i < 4; i++) {
            for(j = 0; j < 4; j++) {
                dfs(i, j, 0, 0, 1);
                dfs(i, j, 0, 0, 2);
            }
        }
        if(cases)    puts("");
        ++cases;
        int common = 0;
        for(map<int, int>::iterator it = R.begin();
            it != R.end(); it++) {
            if(it->second == 3) {
                for(i = 0; i < 4; i++)
                    putchar((((it->first)>>((3-i)*5))&31)+'A');
                puts("");
                common = 1;
            }
        }
        if(common == 0)
            puts("There are no common words for this pair of boggle boards.");
    }
    END:
    return 0;
}

台長: Morris
人氣(1,335) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][LIS] 1062 - Containers
此分類上一篇:[UVA] 11357 - Ensuring Truth

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