先上题目
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.
Words are formed from letters that adjoin horizontally, vertically, or diagonally. However, no letter may be used more than once within a single word.
An example Boggle® letter grid, showing the formation of the words "taxes" and "rise".
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.
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.
Input
The first line of the input file contains a number N, the number of Boggle® games that follow.
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.
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.
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]=='