UVa - 11283 - PLAYING BOGGLE

先上題目ios

Problem F

PLAYING BOGGLE

Boggle® is a classic word game played on a 4 by 4 grid of letters. The letter grid is randomly generated by shaking 16 cubes labeled with a distribution of letters similar to that found in English words. Players try to find words hidden within the grid.api

Words are formed from letters that adjoin horizontally, vertically, or diagonally. However, no letter may be used more than once within a single word.app

An example Boggle® letter grid, showing the formation of the words "taxes" and "rise".dom

The score awarded for a word depends on its length, with longer words being worth more points. Exact point values are shown in the table below. A word is only ever scored once, even if it appears multiple times in the grid.ide

No. of letters:
3
4
5
6
7
8 or more
Points:
1
1
2
3
5
11

In this problem, your task is to write a program that plays Boggle®. Given a letter grid and a dictionary of words, you are to calculate the total score of all the words in the dictionary that can be found in the grid.this

Input

The first line of the input file contains a number N, the number of Boggle® games that follow.spa

Each Boggle® game begins with 16 capital letters arranged in a 4 by 4 grid, representing the board configuration for that game. A blank line always precedes the letter grid. Following the letter grid is a single number M (1 ≤ M ≤ 100), the number of words in your dictionary for that game. The next M lines contain the dictionary words, one per line, in no particular order. Each word consists of between 3 and 16 capital letters. No single word will appear in the dictionary more than once for a given Boggle® game.code

Output

For each Boggle® game in the input, your program should output the total score for that game. Follow the format given in the sample output.orm

Sample Input

2

TNXO
AAEI
IOSR
BFRH
8
TAXES
RISE
ANNEX
BOAT
OATS
FROSH
HAT
TRASH

FNEI
OBCN
EERI
VSIR
1
BEER

Output for the Sample Input

Score for Boggle game #1: 6
Score for Boggle game #2: 1

  排位賽時這一題沒有作出來,由於題意理解錯了= =,覺得是找到一個單詞之後,這個單詞用過的格子所有都不能夠再用了,但其實不是這樣。這一題的題意是不一樣長度的單詞有不一樣的得分,給出字符矩陣讓你在其中找一系列單詞,找到一個單詞就獲得特定的分數,同一個單詞得分只計算
一次,固然,也有可能裏面找不到給出的單詞,若是是這樣就加0分,最後問你能夠獲得多少得分。因爲這一題給的矩陣是4*4,徹底就是一個裸的dfs,只需跑一邊dfs就出結果。

上代碼:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <map>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 map<string,int> M;
 9 char s[5][5],c[20];
10 bool mark[5][5];
11 
12 bool dfs(int i,int j,int n)
13 {
14     if(i<0 || j<0) return 0;
15     if(mark[i][j]) return 0;
16     if(s[i][j]==c[n])
17     {
18         if(c[n+1]=='\0') return 1;
19         mark[i][j]=1;
20         if(dfs(i-1,j-1,n+1)) return 1;  if(dfs(i-1,j,n+1)) return 1;  if(dfs(i-1,j+1,n+1)) return 1;
21         if(dfs(i,j-1,n+1)) return 1;                                  if(dfs(i,j+1,n+1)) return 1;
22         if(dfs(i+1,j-1,n+1)) return 1;  if(dfs(i+1,j,n+1)) return 1;  if(dfs(i+1,j+1,n+1)) return 1;
23         mark[i][j]=0;
24     }
25     return 0;
26 
27 }
28 
29 bool check()
30 {
31     int i,j;
32     for(i=0;i<4;i++)
33     {
34         for(j=0;j<4;j++)
35         {
36             if(s[i][j]==c[0])
37             {
38                 memset(mark,0,sizeof(mark));
39                 if(dfs(i,j,0)) return 1;
40             }
41         }
42     }
43     return 0;
44 }
45 
46 int main()
47 {
48     int m,t,i,j,maxn,da,k;
49     //freopen("data.txt","r",stdin);
50     scanf("%d",&t);
51     for(k=1;k<=t;k++)
52     {
53         M.clear();
54         memset(s,0,sizeof(s));
55         for(i=0;i<4;i++)
56         {
57             scanf("%s",s[i]);
58         }
59         scanf("%d",&m);
60         maxn=0;
61         for(j=0;j<m;j++)
62         {
63             scanf("%s",c);
64             if(M.count(c)>0) continue;
65             M[c]=1;
66             if(!check()) continue;
67             da=strlen(c);
68             if(da<3) continue;
69             switch(da)
70             {
71                 case 3:
72                 case 4: da=1;break;
73                 case 5: da=2;break;
74                 case 6: da=3;break;
75                 case 7: da=5;break;
76                 default :da=11;
77             }
78             maxn=da+maxn;
79         }
80         printf("Score for Boggle game #%d: %d\n",k,maxn);
81     }
82     return 0;
83 }
11283
相關文章
相關標籤/搜索