当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。
Zuma is one of my favorite games of Fantasy Games series. In the game a lot of balls of different colors will enter the screen continuous, and the balls onscreen are rolling by a fixed track in a queue. You can control the stone frog located at the center of screen to fire balls. The balls fired can be inserted into the rolling balls queue. When there is a group of 3 or more balls of the same color touching, they explode! You should eliminate all the balls before they reach the skull which lies on the end of the track.
Let’s consider a simplified game mode. There is a queue of balls of different colors given. At each time you can fire a ball of arbitrary color into the queue at arbitrary position (either between two balls or at one end of the queue). Arbitrary means that you can choose the color and the position on your behalf. When there is a group of M or more balls of the same color touching, they explode. Explosion causes the balls on either side to slide together, perhaps chain-reacting into another explosion.
There are some restrictions following:
What is number of firings do you need at least to finish the game?
Input contains multiple test cases. Each test case starts with an integer M (2 ≤ M ≤ 20) in one line. Following is one line of nonempty capital letters (A to Z). Each character specifies one ball of the color represented by the letter. The length of each line is no more than 200.
There is one line for each test case, which is the number of firings you need at least to finish the game.
3 AAABAAA 3 ABBABBA
2 2
Case time limit(单组数据时间限制) | 7000MS |
时间上限 | 内存上限 |
35000 | 65536 |