UVa279 - Spin(递推)

本文介绍了一种解决特定滑动拼图游戏的算法,该算法利用递推方法计算将拼图滑动到胜利位置所需的最少步骤数。游戏包含可旋转的磁盘,玩家的目标是通过移动磁盘使滑块的左边缘与`Win`标记对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Spin 
tex2html_wrap62tex2html_wrap64

A disk can be rotated between horizontal and vertical only if it is positioned over theindentation marked `0' and the disk on its right is vertical . The right-mostdisk can always rotate if it is in position `0' since it has no disk on its right.

The aim is to free the slide by moving it so its left edge aligns with the `Win' mark:

Your task is to write a program which will take several part-solved puzzles and computethe number of steps needed to move the slide to position `Win' for each puzzle.

Input

There will be several puzzles in the input file. The first line of the file willcontain an integer n specifying the number of puzzles. There will then be nlines, each of the form:

length orientations position

where length(length < 30) is an integer indicating the number of disks on theslide, orientations is a string of length characters from the set {h,v}giving the orientation of each disk from left to right, and position is an integerfrom 0 to length specifying the numbered mark which aligns with the left edge ofthe slide.

Output

For each puzzle, your program should output one integer on a line which counts theminimum number of steps needed to win the puzzle. A step is either a movement of theslide, one unit left or right, or the rotation of a disk.

Sample Input

2
2 vv 2
7 vhhhvhh 4

Output for the SampleInput

7
357

Diagram of the 1stPuzzle

用递推方法

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

const int N = 30;

typedef long long LL;

LL f[N + 5];
string orientation;
int length;
int position;

void init()
{
	f[0] = 0;
	for (int i = 1; i <= N; i++) {
		f[i] = f[i - 1] * 2 + 2 * i - 1;
	}
}

void input()
{
	cin >> length >> orientation >> position;
}

void solve()
{
	size_t len = orientation.length();

	LL ans = 0;
	int sign = 1;

	for (size_t i = 0; i < len; i++) {
		if (orientation[i] == 'v') {
			ans += sign * (f[length - i]);
			sign *= -1;
		}
	}

	ans += position + 1;
	cout << ans << endl;
}

int main(int argc, char **argv)
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	init();

	int t ;
	cin >> t;
	while (t--) {
		input();
		solve();
	}
	return 0;
}


<think>我们正在讨论的是CSS伪元素`-webkit-outer-spin-button`和`-webkit-inner-spin-button`,它们用于控制数字输入框(`<input type="number">`)的旋转按钮(上下箭头)的样式。 根据引用[2]和引用[3],我们可以知道这两个伪元素是Webkit浏览器(如Chrome、Safari)特有的,用于自定义数字输入框旁边的微调按钮(spinner)的样式。 主要属性: 1. `-webkit-appearance`:这是最常用的属性,用于重置或修改默认的外观。通常设置为`none`来移除默认样式,然后可以自定义样式。 2. 其他常规CSS属性:一旦移除了默认样式(`-webkit-appearance: none;`),就可以像普通元素一样设置背景、边框、颜色、大小等。 示例代码(引用[2]): ```css input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; } ``` 此外,引用[3]中也展示了如何设置这些按钮的颜色: ```css ::-webkit-calendar-picker-indicator, ::-webkit-clear-button, ::-webkit-inner-spin-button, ::-webkit-outer-spin-button { color: var(--brand); } ``` 因此,我们可以设置的属性包括但不限于: - `background` - `border` - `color` - `width` / `height` - `padding` / `margin` - 等等 但是,请注意,这些伪元素不是标准的CSS,只在Webkit内核的浏览器中有效。 下面是一个自定义旋转按钮的完整示例: 1. 首先移除默认样式 2. 然后添加自定义样式 例如,我们可以用自定义图片作为按钮的背景: ```css input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; background: url('path/to/your-image.png') no-repeat center center; width: 20px; height: 20px; opacity: 0.5; /* 可选:设置透明度 */ } ``` 注意:由于这些是伪元素,所以不能像普通元素那样设置所有属性,但大多数表现属性(如背景、边框、尺寸等)都可以设置。 另外,引用[2]中还提到了使用`:in-range`和`:out-of-range`伪类来设置输入值在范围内和范围外的样式,但这与旋转按钮本身无关,而是与输入框内容相关。 最后,请注意兼容性:这些伪元素只在Webkit/Blink浏览器中有效,对于Firefox等浏览器,可以使用`-moz-appearance: textfield;`来隐藏旋转按钮(但无法自定义样式): ```css input[type=number] { -moz-appearance: textfield; } ``` 因此,总结一下,`-webkit-outer-spin-button`和`-webkit-inner-spin-button`的主要属性就是通过`-webkit-appearance:none;`来重置默认样式,然后使用常规CSS属性来自定义。 生成相关问题: 1. 如何自定义数字输入框的旋转按钮样式? 2. 除了Chrome和Safari,其他浏览器如何隐藏数字输入框的旋转按钮? 3. 如何为数字输入框添加范围验证样式? 4. 在自定义旋转按钮时,如何确保跨浏览器的一致性?</think>`-webkit-outer-spin-button` 和 `-webkit-inner-spin-button` 是 WebKit 浏览器(如 Chrome、Safari)特有的 CSS 伪元素,用于控制 `<input type="number">` 数字输入框的微调按钮(上下箭头)。以下是主要属性和用法: ### 核心属性 1. **`-webkit-appearance`** - 关键属性,用于移除默认样式: ```css input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; /* 移除默认箭头 */ } ``` - 移除后可通过其他 CSS 自定义样式[^2][^3]。 2. **尺寸与布局属性** 支持常规盒模型属性: ```css input::-webkit-inner-spin-button { width: 20px; /* 宽度 */ height: 30px; /* 高度 */ padding: 2px; /* 内边距 */ margin: 0; /* 外边距 */ } ``` 3. **背景与颜色** 支持背景和颜色定制: ```css input::-webkit-inner-spin-button { background-color: #f0f0f0; /* 背景色 */ background-image: url("arrow.png"); /* 背景图片 */ color: hotpink; /* 图标颜色(需浏览器支持) */ } ``` 4. **边框与圆角** 可添加边框效果: ```css input::-webkit-inner-spin-button { border: 1px solid #ccc; border-radius: 4px; /* 圆角 */ } ``` 5. **交互状态** 支持伪类状态: ```css input::-webkit-inner-spin-button:hover { background-color: #e0e0e0; /* 悬停状态 */ } input::-webkit-inner-spin-button:active { transform: scale(0.95); /* 点击状态 */ } ``` ### 使用场景示例 ```css /* 移除默认箭头并自定义样式 */ input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; background: url("custom-arrow.svg") center/70% no-repeat; width: 24px; height: 24px; opacity: 0.8; margin: 0; } /* 范围验证(搭配 :in-range/:out-of-range) */ input:out-of-range { background: #ffe6e6; /* 输入值越界时背景变红 */ } ``` [^2][^3] --- ### 注意事项 1. **浏览器兼容性** 仅适用于 WebKit 内核浏览器(Chrome、Safari、Edge)。Firefox 需用 `-moz-appearance: textfield` 隐藏箭头: ```css input[type="number"] { -moz-appearance: textfield; /* Firefox */ } ``` 2. **功能限制** 移除箭头后仍需通过 JavaScript 实现完整的数值校验逻辑(如最小值/最大值)[^2]。 3. **样式覆盖优先级** 自定义样式需在 `-webkit-appearance: none` 后声明,否则无效。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值