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

记录一个菜逼的成长。。

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值