(基于此Math.SE问题,该问题还提供了一些图形)
我有一根看起来像这样的棍子:
我希望它看起来像这样:
我不是专业画家,因此,在开始这个雄心勃勃的DIY项目之前,我想确保自己不会感到头疼。
您的程序应该告诉我,画这根棍子需要多少步骤。每个步骤都涉及用纯色绘制连续的区域,该区域覆盖先前的油漆层。对于上面的示例,我可以先将左半部分涂成蓝色,右半部分涂成红色,然后将两个单独的绿色区域分别涂成4步(绿色不连续涂成)。
这是ASCII:
------
bbb---
bbbrrr
bgbrrr
bgbrgr
有两种不同的方法可以绘制这根棍子并最终得到相同的结果。但是,我只对时间估计感兴趣,这是四个步骤。
目标
您的程序应输出以给定的配色方案涂满棍子所需的最少步骤数。绘制方案将采用字符串形式,而输出将为数字。这是代码高尔夫。最短的程序获胜。
输入值
您的程序将以一串字母的形式接收一根棒的着色方案。每个唯一字母(区分大小写)代表唯一的颜色。
YRYGR
grG
GyRyGyG
pbgbrgrp
hyghgy
输出量
这些数字是油漆棒所需的最少步骤数。
4
3
4
5
4
说明
这就是我得出上述数字的方式。您的程序不需要输出以下内容:
-----
YYY--
YRY--
YRYG-
YRYGR
---
g--
gr-
grG
-------
GGGGGGG
GyyyGGG
GyRyGGG
GyRyGyG
--------
pppppppp
pbbbpppp
pbbbrrrp
pbgbrrrp
pbgbrgrp
------
-yyyyy
-ygggy
hygggy
hyghgy
编辑:如果它们证明是更困难的测试用例,我将添加更多的测试用例。