"玲珑杯”ACM比赛 Round #8-D XJT Loves Boggle(dfs)

来源:互联网 发布:淘宝公安警用皮带 编辑:程序博客网 时间:2024/06/04 20:45

记录一个菜逼的成长。。

DESCRIPTION

Boggle is a word game designed by Allan Turoff and distributed by Hasbro. It involves a board made up of 16 cubic dice, where each die has a letter printed on each of its sides. At the beginning of the game, the 16 dice are shaken and randomly distributed into a 4-by-4 tray, with only the top sides of the dice visible. The players compete to accumulate points by building valid words out of the dice according to the following rules:

A valid word must be composed by following a sequence of adjacent dice—two dice are adjacent if they are horizontal, vertical, or diagonal neighbors.A valid word can use each dice at most once.A valid word must contain at least 3 letters.A valid word must be in the dictionary (which typically does not contain proper nouns).Words are scored according to their length, using this table:| Word length | points || :---------: | :----: ||     0-2     |   0    ||     3-4     |   1    ||      5      |   2    ||      6      |   3    ||      7      |   5    ||     8-9     |   11   |To simplify the problem, now we will play this game on a 3-by-3 board. And the rule 4 will be ignored. Now you will be given n words record which are given by players. You need to check the score of these words. If the word is not valid according to rules mentioned above except the rule 4, the score of it is zero.   

INPUT
The first line of input contains an integer T(T<=5) indicating the number of test cases.
For each test case, the first three lines, each line contains three uppercase English letters. The next line contains a integer n(1 <= n <= 50000), indicating the number of words. The following n lines, each line contains a word(word’s length <= 9). The word only consist uppercase English letters.
OUTPUT
For each case, output “Case #X:” in a line where X is the case number, staring from 1. Then for each word, output the score in a line.
SAMPLE INPUT
1
ABC
DEF
GHI
4
ADG
AFI
A
ABCFI
SAMPLE OUTPUT
Case #1:
1
0
0
2
SOLUTION
“玲珑杯”ACM比赛 Round #8

题目链接
题目大意:
给你3*3的字符矩阵,q次询问,每次询问给你一个单词,如果满足条件输出这个单词的分数,不满足则输出0。
1.单词组成有八个方向
2.每个格子的字母只能用一次
3.不能少于3个字母

直接暴力搜索即可。。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))char g[5][5],str[20];bool vis[5][5];int score[] = {0,0,0,1,1,2,3,5,11,11};int dx[] = {-1,-1,0,1,1,1,0,-1},dy[] = {0,1,1,1,0,-1,-1,-1};bool dfs(int x,int y,int pos,int n){    if(pos == n)return true;    for( int i = 0; i < 8; i++ ){        int nx = x + dx[i],ny = y + dy[i];        if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3)continue;        if(vis[nx][ny] || g[nx][ny] != str[pos])continue;        vis[nx][ny] = 1;        bool ret = dfs(nx,ny,pos+1,n);        vis[nx][ny] = 0;        if(ret)return true;    }    return false;}int main(){    int T,cas = 1;    scanf("%d",&T);    while(T--){        for( int i = 0; i < 3; i++ )            scanf("%s",g[i]);        int q;        scanf("%d",&q);        printf("Case #%d:\n",cas++);        while(q--){            scanf("%s",str);            cl(vis,0);            int len = strlen(str);            if(len > 9 || len < 3){                printf("0\n");                continue;            }            bool ret  = false;            for( int i = 0; i < 3; i++ ){                ret = false;                for( int j = 0; j < 3; j++ ){                    if(g[i][j] == str[0]){                        vis[i][j] = 1;                        ret = dfs(i,j,1,len);                        if(ret)break;                        vis[i][j] = 0;                    }                }                if(ret)break;            }            if(ret)printf("%d\n",score[len]);            else puts("0");        }    }    return 0;}
0 0
原创粉丝点击