#10030. 「一本通 1.4 练习 2」Keyboarding

news2024/7/6 18:33:02

[ICPC2015 WF]Keyboarding

题面翻译

给定一个 r r r c c c 列的在电视上的「虚拟键盘」,通过「上,下,左,右,选择」共 5 5 5 个控制键,你可以移动电视屏幕上的光标来打印文本。一开始,光标在键盘的左上角,每次按方向键,光标总是跳到下一个在该方向上与当前位置不同的字符,若不存在则不移动。每次按选择键,则将光标所在位置的字符打印出来。
现在求打印给定文本(要在结尾打印换行符)的最少按键次数。

题目描述

How many keystrokes are necessary to type a text message? You may think that it is equal to the number of characters in the text, but this is correct only if one keystroke generates one character. With pocket-size devices, the possibilities for typing text are often limited. Some devices provide only a few buttons, significantly fewer than the number of letters in the alphabet. For such devices, several strokes may be needed to type a single character. One mechanism to deal with these limitations is a virtual keyboard displayed on a screen, with a cursor that can be moved from key to key to select characters. Four arrow buttons control the movement of the cursor, and when the cursor is positioned over an appropriate key, pressing the fifth button selects the corresponding character and appends it to the end of the text. To terminate the text, the user must navigate to and select the Enter key. This provides users with an arbitrary set of characters and enables them to type text of any length with only five hardware buttons.

In this problem, you are given a virtual keyboard layout and your task is to determine the minimal number of strokes needed to type a given text, where pressing any of the five hardware buttons constitutes a stroke. The keys are arranged in a rectangular grid, such that each virtual key occupies one or more connected unit squares of the grid. The cursor starts in the upper left corner of the keyboard and moves in the four cardinal directions, in such a way that it always skips to the next unit square in that direction that belongs to a different key. If there is no such unit square, the cursor does not move.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JOTYh3Sf-1687073211325)(https://vj.z180.cn/4393d80e3f068a496c9b906fdca621d9?v=1603457188)]

Figure 1: Sample Input 1. An example virtual keyboard and hardware buttons.

Figure 1, illustrating Sample Input 1, shows a possible way to type CONTEST using 30 strokes on an example virtual keyboard. The red dots represent the virtual keys where the select button was pressed.

输入格式

The first line of the input contains two integers r r r and c c c ( 1 ≤ r , c ≤ 50 1 \leq r, c \leq 50 1r,c50), giving the number of rows and columns of the virtual keyboard grid. The virtual keyboard is specified in the next r r r lines, each of which contains c c c characters. The possible values of these characters are uppercase letters, digits, a dash, and an asterisk (representing Enter). There is only one key corresponding to any given character. Each key is made up of one or more grid squares, which will always form a connected region. The last line of the input contains the text to be typed. This text is a non-empty string of at most 10   000 10\, 000 10000 of the available characters other than the asterisk.

输出格式

Display the minimal number of strokes necessary to type the whole text, including the Enter key at the end. It is guaranteed that the text can be typed.

样例 #1

样例输入 #1

4 7
ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ**
CONTEST

样例输出 #1

30

样例 #2

样例输入 #2

5 20
12233445566778899000
QQWWEERRTTYYUUIIOOPP
-AASSDDFFGGHHJJKKLL*
--ZZXXCCVVBBNNMM--**
--------------------
ACM-ICPC-WORLD-FINALS-2015

样例输出 #2

160

样例 #3

样例输入 #3

2 19
ABCDEFGHIJKLMNOPQZY
X*****************Y
AZAZ

样例输出 #3

19

样例 #4

样例输入 #4

6 4
AXYB
BBBB
KLMB
OPQB
DEFB
GHI*
AB

样例输出 #4

7

提示

Time limit: 3000 ms, Memory limit: 1048576 kB.

International Collegiate Programming Contest (ACM-ICPC) World Finals 2015

大致思路

  • 数据范围不大,暴力BFS即可
  • BFS队列中的元素记录当前位置和当前匹配的位数
  • 但是要求下一个字符与当前位置不同,可能会有大量无用计算
  • 需要预处理出每个位置在每个方向上下一个位置
void init(){//预处理
		sy[0]=sy[2]=y,sx[1]=sx[3]=x;
		for(int i=x;i&&mi[i][y]==a;i--) sx[0]=i;sx[0]--;
		for(int i=x;i<=n&&mi[i][y]==a;i++) sx[2]=i;sx[2]++;
		for(int j=y;j<=m&&mi[x][j]==a;j++) sy[1]=j;sy[1]++;
		for(int j=y;j&&mi[x][j]==a;j--) sy[3]=j;sy[3]--;
	}

剩下的依旧是bfs模板

void bfs(){
	queue<node> q;
	q.push(node(1, 1, 0, 0));
	while(!q.empty()){//逐个匹配
		node k=q.front();
		q.pop();
		if(mp[k.a][k.b].a==s[k.t]){
			if(k.t==len){
				printf("%d\n",k.step+1);
				return;
			}
			vis[k.a][k.b] =k.t+1;
			q.push(node(k.a,k.b,k.t+1,k.step+1));
			continue;
		}
		for(int i=0;i<4;i++) {
			int dx=mp[k.a][k.b].sx[i],dy=mp[k.a][k.b].sy[i];
			if(dx<1||dx>n||dy<1||dy>m) continue;
			if(dx==k.a&&dy==k.b) continue;
			if(vis[dx][dy]>=k.t) continue;
			vis[dx][dy]=k.t;
			q.push(node(dx,dy,k.t,k.step+1));
		}
	}
}
  • AC CODE

#include<bits/stdc++.h>
using namespace std;
int n,m,ans,len,vis[55][55],mi[55][55];
char s[10005];
struct point{
	int a,x,y,sx[4],sy[4];
	void init(){//预处理
		sy[0]=sy[2]=y,sx[1]=sx[3]=x;
		for(int i=x;i&&mi[i][y]==a;i--) sx[0]=i;sx[0]--;
		for(int i=x;i<=n&&mi[i][y]==a;i++) sx[2]=i;sx[2]++;
		for(int j=y;j<=m&&mi[x][j]==a;j++) sy[1]=j;sy[1]++;
		for(int j=y;j&&mi[x][j]==a;j--) sy[3]=j;sy[3]--;
	}
}mp[55][55];
struct node{
	int a, b, t, step;
	node(int aa,int bb,int tt,int ss){
		a=aa,b=bb,t=tt,step=ss;
	}
};
void bfs(){
	queue<node> q;
	q.push(node(1, 1, 0, 0));
	while(!q.empty()){//逐个匹配
		node k=q.front();
		q.pop();
		if(mp[k.a][k.b].a==s[k.t]){
			if(k.t==len){
				printf("%d\n",k.step+1);
				return;
			}
			vis[k.a][k.b] =k.t+1;
			q.push(node(k.a,k.b,k.t+1,k.step+1));
			continue;
		}
		for(int i=0;i<4;i++) {
			int dx=mp[k.a][k.b].sx[i],dy=mp[k.a][k.b].sy[i];
			if(dx<1||dx>n||dy<1||dy>m) continue;
			if(dx==k.a&&dy==k.b) continue;
			if(vis[dx][dy]>=k.t) continue;
			vis[dx][dy]=k.t;
			q.push(node(dx,dy,k.t,k.step+1));
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++){
			mi[i][j]=mp[i][j].a=s[j];
			mp[i][j].x=i,mp[i][j].y=j;
			vis[i][j]=-1;
		}
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++) mp[i][j].init();
	scanf("%s", s);
	len=strlen(s);
	s[len]='*';
	bfs();
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/661639.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32 MCO+SPI获取24位模数转换(24bit ADC)高速芯片ADS1271采样数据

STM32 MCOSPI获取24位模数转换&#xff08;24bit ADC&#xff09;高速芯片ADS1271采样数据 STM32大部分芯片只有12位的ADC采样性能&#xff0c;如果要实现更高精度的模数转换如24位ADC采样&#xff0c;则需要连接外部ADC实现。ADS1271是 TI公司一款高速24位Σ-Δ型模数转换器(…

[每周一更]-(第50期):Go的垃圾回收GC

参考文章&#xff1a; https://juejin.cn/post/7111515970669117447https://draveness.me/golang/docs/part3-runtime/ch07-memory/golang-garbage-collector/https://colobu.com/2022/07/16/A-Guide-to-the-Go-Garbage-Collector/https://liangyaopei.github.io/2021/01/02/g…

【前端布局篇】浮动、定位、弹性布局,固比固、双飞翼、圣杯布局

一、布局方式介绍 布局模型是基于盒模型基础上进行的拓展&#xff0c;关于布局有流式布局&#xff08;标准的布局&#xff09;&#xff0c;浮动布局、定位布局、flex布局等。 1.1 标准流&#xff08;流动模型&#xff09; 描述&#xff1a;元素按照自己默认的元素类型在页面…

社区问答精选——长安链开发知多少?(6月)

此次整理的内容为5-6月社群内的问答供更多开发者参考&#xff08;社群中部分优质问题连贯性不足未能收录&#xff0c;欢迎点击公众号菜单栏加入社群共同交流&#xff09;。有更多问答在社区issue中描述更为细致&#xff0c;开发者提问前可以先按照关键词进行搜索。欢迎各位开发…

Doris FE启动流程源码解读

FE启动流程分析 Doris中FE主要负责接收和返回客户端请求、元数据以及集群管理、查询计划生成等工作。 本文主要看一下Doris的fe在启动时做了什么。 启动流程分析 启动流程图&#xff1a; 代码路径&#xff1a; doris/fe/fe-core/src/main/java/org/apache/doris/DorisFE.j…

华为云CodeArts Build快速上手编译构建-进阶玩家体验

华为云CodeArts Build编译构建为开发者提供配置简单的混合语言构建平台&#xff0c;实现编译构建云端化&#xff0c;支撑企业实现持续交付&#xff0c;缩短交付周期&#xff0c;提升交付效率。支持编译构建任务一键创建、配置和执行&#xff0c;实现获取代码、构建、打包等活动…

react+antd实现表格封装,可动态控制列显示隐藏。

实现效果 import { Table, Pagination, Button, Dropdown, Checkbox, message } from antd; import { useState, useEffect } from react; import { PicRightOutlined } from ant-design/icons;import ./index.less;const TableComponent (props) > {const powerList JSON…

【方法】Excel表格的“打开密码”不想要了,如何取消?

对于重要的Excel表格&#xff0c;很多小伙伴都会设置“打开密码”&#xff0c;这样就无法随意打开表格&#xff0c;只有输入正确的密码才可以打开。 如果后续表格不再需要保护&#xff0c;每次打开都要输一次密码&#xff0c;这样操作也是很麻烦。 那不想要“打开密码”&…

(5)(5.8) 保存微调和自动微调

文章目录 前言 1 保存微调 2 自动微调 3 保存微调和自动微调的视频演示 4 桌面方法 前言 当然&#xff0c;风对你的旋翼飞机有很大的影响&#xff0c;会把它推来推去。然而&#xff0c;你可能也会发现&#xff0c;在自稳模式下飞行时&#xff0c;即使在无风的环境中&#xff0…

chatgpt赋能python:Python重新编辑引擎优化(SEO)文章

Python重新编辑引擎优化(SEO)文章 介绍 Python是一种多用途的高级编程语言&#xff0c;用于开发网络应用程序&#xff0c;算法&#xff0c;科学计算和数据分析等。 随着越来越多的网站和应用程序采用Python编写&#xff0c;优化Python代码以提高搜索引擎优化(SEO)变得越来越重…

基于html+css的图展示133

准备项目 项目开发工具 Visual Studio Code 1.44.2 版本: 1.44.2 提交: ff915844119ce9485abfe8aa9076ec76b5300ddd 日期: 2020-04-16T16:36:23.138Z Electron: 7.1.11 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.19044 项目…

科技孵化制造蝶变:国际智造节上,群硕获评2023杰出数字化创新企业

论起2023年的热点有哪些&#xff1f;由ChatGPT掀起的智能科技浪潮&#xff0c;绝对是逃不开的话题。 6月16日&#xff0c;以“科技驱动&#xff0c;智造未来”为主题的2023国际智造节&#xff0c;在北京隆重举行。通过此次活动&#xff0c;在数字化领域深耕二十年的群硕软件&a…

K8s Kubectl 技巧集锦

kubectl 是 Kubernetes 的一个命令行管理工具&#xff0c;可用于 Kubernetes 上的应用部署和日常管理。本文列举了 9 个常见的 kubectl 命令&#xff0c;并总结了一些使用技巧&#xff0c;希望可以帮助系统管理员简化管理工作。 一、使用 Kubectl 查询、创建、编辑和删除资源 …

【C数据结构】循环队列_CyclicQueue

目录 循环队列_CyclicQueue 【1】循环队列 【1.1】循环队列的各个接口 【1.2】循环队列初始化 【1.3】循环队列初销毁 【1.4】循环队列插入 【1.5】循环队列删除 【1.6】循环队列获取头位置数据 【1.7】循环队列获取尾位置数据 【1.8】循环队列判满 【1.9】循环队列…

MMOE(Multi-gate Mixture-of-Experts)

1.前提和动机 随着推荐技术的发展&#xff0c;目前越来越多的推荐场景&#xff0c;往往并不是单独的优化一个指标&#xff0c;比如&#xff1a; 视频推荐领域&#xff1a;推荐排序任务不仅需要考虑到用户点击率、完播率&#xff0c;也需要考虑到一些满意度指标&#xff0c;例如…

优秀java实习报告范文5篇

优秀java实习报告范文(一) 一:实习介绍 1)实习题目 学生信息管理系统的设计与开发 2)实习目的 《Java程序设计》课程是电子商务专业的一门专业必修课程&#xff0c;特针对此课程进行为期三周的实践教学&#xff0c;旨在提升本专业学生对此课程中的理论知识的综合应用能力、提高…

微信研发体系下的分布式配置系统设计实践

腾小云导读 对很多的开发者而言&#xff0c;处理运营素材反复变更等需求场景不是一件轻松的事。开发者通常需要定制化地进行数据清理、格式转换和工具开发等等。在这个时候&#xff0c;建设分布式配置系统就显得尤为重要。本文旨在分析分布式配置系统的必要性、可行性及其关键…

SpringBoot的日志

SpringBoot的日志 &#x1f50e;日志是什么&#x1f50e;日志的作用&#x1f50e;日志级别日志级别的作用日志级别的分类日志级别的设置 &#x1f50e;打印日志打印日志具体内容划分 &#x1f50e;常用的日志框架为什么这样设计对比System.out.ptintln()与日志框架 &#x1f50…

Kubernetes那点事儿——暴露服务之Ingress

Kubernetes那点事儿——暴露服务之Ingress 前言一、ingress负载均衡器Ingress Controller路由规则Ingress 二、Ingress Controller三、案例 前言 在 k8s 集群中&#xff0c;如果我们将服务暴露出来&#xff0c;提供访问&#xff0c;可以使用Nodeport方式&#xff0c;但是Nodepo…

SIG Mesh协议学习

1. 简介 Bluetooth SIG组织在2017年7月17日发布了蓝牙Mesh标准. 蓝牙Mesh不同于传统Bluetooth Low Energy(BLE)协议的1对1, 1对多的通信方式, 它实现了多对多的通信. 这使得mesh网络中的各个节点之间可以相互通信. 蓝牙Mesh协议建立在BLE的物理层和链路层之上, 也就是说它可以…