sincerit 1295 Move Move Look

探讨了在一个黑白棋交替排列的棋盘上,如何通过最少的步骤将棋子重新排列的问题。通过递归解法,将问题分解为更小规模的子问题,最终找出最少的移动步骤。

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

1295 Move Move Look
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1198 Accepted Submission(s): 727

Problem Description
There are two color (white and black) chessmen in a row. The number of white chessman is equal to the number of black chessman. The left half of the row is black chessmen and the right half of the row is white chessmen at first. There are two rules for the game.

  1. Only two neighbor chessmen (double black, double white or one black and one white) can be moved to vacancy at one time. The new vacancy can be filled by the two new neighbor chessmen. Repeat moving chessmen until all chessmen are interphase with black chessman and white chessman.
  2. The number of move must be the minimum.

The case n=3 is shown in Fig. 1. Your task is to get the number of the minimum steps.
在这里插入图片描述

Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer means the number of black chessmen (the same as white chessmen) n(3<=n<=1000).

Output
For each test case, there is only one integer means the minimum steps.

Sample Input
3

Sample Output
3
递归解法把复杂的状态每次分解成规模更小的,当规模小到可以解决时整个问题就解决了
注意是黑棋在前白棋在后
当n = 3时
初始状态 ●●●○○○
第一步: ●○○○●●
第二步: ●○○ ●○●
第三步: ○●○●○●
当n = 4时
初始状态 ●●●●○○○○
第一步: ●●○○○○●●
第二步: ○●●●○○ ○● --规模转化为2的(中间的四个)
第三步: ○● ○○●●○●
第四步: ○●○●○●○●
…规律就是 多少对就要多少步数

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    if (n == 1) cout << "0\n";
    else cout << n << "\n";
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值