//
//ViewController.m
//CocoTest_1
//
//Created by S u p e r m a n on 2017/3/14.
//Copyright © 2017年张浩. All rights reserved.
//
#import"ViewController.h"
staticintcount =1;
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
chessAction(4);
}
/**
问题描述:有2n个棋子排成一行,白子用0代表,黑子用1代表。n=5的状态为:
0000011111_ _ (右边至少两个空位)
移动规则:
(1)每次必须移动相邻的两个棋子,这两个棋子不能互换位置
(2)移动的颜色不限,移动的方向不限
要求:
最后成为_ _ 0101010101的状态(中间无空格)。
*/
voidchessAction(intn) {
if(n <4) {
return;
}elseif(n ==4) {//递归结束的
//当递归到规模为4的时候
//00001111__
//0123456789
//1:000__11101
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,3,4,8,9);
//2:0001011__1
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,7,8,3,4);
//3:0__1011001
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,1,2,7,8);
//4:010101__01
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,6,7,1,2);
//5:__01010101
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,0,1,6,7);
}else{
//开始递归
//把规模为n的问题转化成规模为n-1的问题
//1.把第n-1和n和空格(2n,2n+1)交换
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,n-1,n,2*n,2*n+1);
//2.把第2n-2和2n-2和空格(n-1,n)交换
printf("第%d步:%d&%d和空格(%d&%d)交换\n",count++,2*n-2,2*n-2,n-1,n);
//经过上步操作变成了问题为n-1的规模
chessAction(n-1);
}
}@end