UVa - 11283 - PLAYING BOGGLE

news/2023/9/27 6:52:12

先上题目

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

2TNXO
AAEI
IOSR
BFRH
8
TAXES
RISE
ANNEX
BOAT
OATS
FROSH
HAT
TRASHFNEI
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

 


转载于:https://www.cnblogs.com/sineatos/p/3221458.html


https://dhexx.cn/news/show-18347.html

相关文章

Open-Feign整合hystrix降级熔断实战

Open-Feign整合hystrix降级熔断实战一、服务端1、配置文件2、控制层二、客户端1、依赖2、配置文件3、启动类4、在控制层当中调用5、创建一个类实现服务FeignClient接口6、在服务FeignClient接口上配置FallBack实现类三、测试1、场景一服务正常调用2、场景二当被调服务停止运行时…

图的全局最小割的Stoer-Wagner算法及例题

Stoer-Wagner算法基本思想&#xff1a;如果能求出图中某两个顶点之间的最小割&#xff0c;更新答案后合并这两个顶点继续求最小割&#xff0c;到最后就得到答案。 算法步骤&#xff1a; ------------------------------------------------------------------------------------…

学习消息中间件Kafka从配置到基本应用

学习消息中间件Kafka从配置到基本应用一、服务器安装配置Kafka1、配置介绍与修改2、启动3、配置开机自启4、如果不使用自带的zookeeper二、Kafka的使用场景1、异步处理2、应用解耦3、流量削锋4、日志处理5、消息通讯三、点对点消息传递模式1、介绍四、发布-订阅消息传递模式1、…

【译】ASP.NET MVC 5 教程 - 1:入门

本教程将教你使用Visual Studio 2013 预览版构建 ASP.NET MVC 5 Web 应用程序 的基础知识。本主题还附带了一个采用 C# 源代码的 Visual Web Developer 项目。下载C# 版本。 入门 Visual Studio 是一个集成的开发环境。就像您使用 Microsoft Word 写文档&#xff0c;您将使用 I…

Redis雪崩、穿透、击穿补充学习与布隆过滤器

Redis学习补充与布隆过滤器一、缓存雪崩1、介绍2、处理雪崩数据二、缓存穿透1、介绍2、处理穿透数据三、缓存击穿2、处理击穿数据四、Redis其他知识1、Redis为何这么快2、Redis的持久化策略五、布隆过滤器1、安装2、介绍布隆过滤器3、使用布隆过滤器BloomFilter解决Redis的缓存…

在线音频江湖:内容大战、场景之争、AI博弈

配图来自Canva可画 智能终端的普及&#xff0c;让“耳朵经济”有了更大的发挥空间&#xff0c;连带着在线音频平台也获得了新增长。近期&#xff0c;在线音频赛道喜讯连连&#xff0c;先是喜马拉雅宣布首次单季度盈利&#xff0c;荔枝也紧跟着宣布首次实现全年盈利。 喜马拉雅…

ExtJs 4.0 动态生成Grid

每写一篇文章都是一部血泪史啊。最近公司的需求要是使用到Ext的动态生成Grid&#xff0c;公司用的是4.0&#xff0c;由于版本问题在网上找了很多都不实用&#xff0c;所以自己研究了下。 现在给大家分享出来。 后台Json: callback内容&#xff1a; 1 callback : function(optio…

Java多线程与各种锁

Java多线程与各种锁一、Synchronize线程同步二、各种Lock锁1、普通锁2、公平锁与非公平锁3、乐观锁与悲观锁以及CAS优化乐观锁4、重入锁与重入自旋锁一、Synchronize线程同步 public class BuyController {public static void main(String[] args) {MyThread myThread1 new M…

Zookeeper分布式协调

Zookeeper分布式协调一、Zookeeper是什么&#xff1f;1、开启zookeeper服务及使用二、使用zookeeper1、连接zookeeper工具类2、参数介绍3、监听服务上下线提示4、分布式锁三、CuratorLock框架实现分布式锁四、实践五、其他1、如何关闭 org.apache.zookeeper.clientcnxn 的(控制…

什么是阶梯电价

什么是阶梯电价&#xff1f; 阶梯电价全名为阶梯式累进电价&#xff0c;是指将现行单一形式的居民电价&#xff0c;改为按照用户消费的电量分段定价&#xff0c;用电价格随用电量增加逐级递增的一种电价定价机制。即把居民每个月的用电分成基本用电、正常用电、高质量用电三档。…