棋子移动问题。

booming 2005-10-18 04:05:08
棋子移动问题。
(1)起始条件:有黑白各n个棋子(n≥4)排成一行,开始时白色棋子全部在左边,黑色棋子全部在右边。
(2)移动规则:
• 每次移动两个棋子,颜色不限;
• 可以移动到左边的空位,也可以移动到右边的空位上;
• 不能调换两个棋子的左右位置;
• 每次移动必须跳过若干棋子(不可平移)。
(3)要求:最后移成黑白相间的一行。
试给出移动程序。


能给出程序嘛?
...全文
1350 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZeroGts 2006-01-31
  • 打赏
  • 举报
回复
每次一定要移动两个吗?移动一个不行吗?
chenzhichao2008 2006-01-20
  • 打赏
  • 举报
回复
改:
if( M%2 )
{
MoveChess( chess, 0, M-1, M/2-2, M/2+1 );
}
else
{
MoveChess( chess, 0, M-1, M/2-2, M/2+1 );
}

为:MoveChess( chess, 0, M-1, M/2-2, M/2+1 );

chenzhichao2008 2006-01-20
  • 打赏
  • 举报
回复

#define M (19+2)
void InitChess( int chess[], int m )
{
chess[ 0 ] = 0;
chess[ m-1 ] = 0;
for( int i = 1; i < m-1; i++ )
{
chess[ i ] = i/(m/2)+1;
}
}

void DispChess( int chess[], int m )
{
for( int i = 0; i < m; i++)
{
cout<<chess[ i ]<<",";
}
cout<<endl;
}

void MoveToSpace( int chess[], int space, int index )
{
if( space != index && chess[index] )
{
chess[ space ] = chess[ index ] ;
chess[ index ] = 0;
//DispChess( chess, M );
}
}

void MoveChess( int chess[], int space1, int space2, int i, int j )
{
if( i < 0 || j > M-1 )
{
return;
}
else
{
MoveToSpace( chess, space2, i );
MoveToSpace( chess, space1, j );
DispChess( chess, M );
MoveToSpace( chess, i, space1 );
MoveToSpace( chess, j, space2 );
DispChess( chess, M );
MoveChess( chess, space1, space2, i-2, j+2 );
}
}

void TestChessMove()
{
int chess[ M ] ;
InitChess( chess, M );
DispChess( chess, M );
if( M%2 )
{
MoveChess( chess, 0, M-1, M/2-2, M/2+1 );
}
else
{
MoveChess( chess, 0, M-1, M/2-2, M/2+1 );
}
}
aheadyes 2006-01-19
  • 打赏
  • 举报
回复
汗,题目要求好象不太一样。这个是把原色进行对调,而且一次只能移动一个棋子。
aheadyes 2006-01-19
  • 打赏
  • 举报
回复
author: dengsf
初始时,空格在正中央(0处),上一步所走的颜色 设为其中一种。
如果空格两边的颜色不同,则根据“上一步所走颜色”来将同色的那钉子往它的方向移动。
如果空格两边的颜色相同,先决定是否能够移动异色的钉子(也就是用跳的,往它的方向);
不行的话移动其中一颗,往自己的方向如果空格的某一边是边界,则移动需要向该方向移动的钉子。比如在最左边时,移动白色的钉子。
当没有符合以上规则的移动时,表示已经结束了,退出。

#include<stdio.h>
#include<stdlib.h>
enum Color{ BLUE, WHITE };
#define COLOROF(a) (a>0 ? WHITE : BLUE)
void display(int* a, int n);

int main(){
// - n ~ n
int n;
if( !scanf("%d", &n) ){
return 0;
}
getchar();

if( n < 1 || n > 10000 ){
printf("don't cheat me as a fool!");
return 0;
}

// represent the nails in the axis
int *a = NULL;
if(! ( a = (int*)malloc(sizeof(int)*(2*n+1)) ) ){
return 0;
}
for(int i=0; i<2*n+1; i++){
a[i] = i - n;
}
// initize position of the space
int position = n;
// last color
Color last = BLUE;

// routine
while(1){
display(a, n);
//left boundary
if( position == 0 ){
if( COLOROF(a[1]) == WHITE ){
a[0] = a[1];
a[1] = 0;
position = 1;
continue;
}else if(COLOROF(a[2]) == WHITE){
a[0] = a[2];
a[2] = 0;
position = 2;
continue;
}
break;
//right boundary
}else if( position == 2*n ) {
if( COLOROF(a[2*n-1 ]) == BLUE ){
a[2*n] = a[2*n-1];
a[2*n-1] = 0;
position = 2*n -1;
last = WHITE;
continue;
}else if( COLOROF(a[2*n-2]) == BLUE ){
a[2*n] = a[2*n-2];
a[2*n-2] = 0;
position = 2*n -2;
last = BLUE;
continue;
}
break;
//between the ones of the same color
}else if( COLOROF(a[position-1]) == COLOROF(a[position+1]) ){
if( COLOROF(a[position-1]) == BLUE ){
if(position+2<2*n+1 && COLOROF(a[position+2]) == WHITE ){
a[position] = a[position+2];
a[position+2] = 0;
position += 2;
last = WHITE;
continue;
}
a[position] = a[position-1];
a[position-1] = 0;
position -= 1;
last = BLUE;
continue;
}else{
if( position-2>=0 && COLOROF(a[position-2]) == BLUE ){
a[position] = a[position-2];
a[position-2] = 0;
position -= 2;
last = BLUE;
continue;
}
a[position] = a[position+1];
a[position+1] = 0;
position += 1;
last = WHITE;
continue;
}
break;
//between the ones of different colors
}else{
if( last == BLUE ){
if( COLOROF(a[position-1]) == BLUE ){
a[position] = a[position-1];
a[position-1] = 0;
position -= 1;
continue;
}else if( position-2>=0 && COLOROF(a[position-2]) == BLUE ){
a[position] = a[position-2];
a[position-2] = 0;
position -= 2;
continue;
}else if( position+2<2*n+1 && COLOROF(a[position+2])==WHITE ){
a[position] = a[position+2];
a[position+2] = 0;
position += 2;
last = WHITE;
continue;
}
break;
}else{
if( COLOROF(a[position+1]) == WHITE ){
a[position] = a[position+1];
a[position+1] = 0;
position += 1;
continue;
}else if( position+2<2*n+1 && COLOROF(a[position+2]) == WHITE ){
a[position] = a[position+2];
a[position+2] = 0;
position += 2;
continue;
}else if( position-2>=0 && COLOROF(a[position-2])==BLUE ){
a[position] = a[position-2];
a[position-2] = 0;
position -= 2;
continue;
}
break;
}
break;
}
}
getchar();
return 0;
}

void display(int* a, int n){
for(int i=0; i<2*n+1; i++){
printf("%3d ", a[i]);
}
printf("\n");
}
sailor_Song 2006-01-19
  • 打赏
  • 举报
回复
不大明白意思,不过应该很好弄,因为走动的限制条件不多。
booming 2005-12-30
  • 打赏
  • 举报
回复
ui[
booming 2005-11-27
  • 打赏
  • 举报
回复
up
booming 2005-11-26
  • 打赏
  • 举报
回复
能给出代码吗?谢谢!
Bobby136 2005-11-23
  • 打赏
  • 举报
回复
a*搜索,不难但是很烦
boxer_tony 2005-11-21
  • 打赏
  • 举报
回复
不限制步数吗?对于限制只能使用n 步的情况,我曾经编程验证到n<=15。因为算法时间复杂度太高无法继续下去,我也一直希望能找出效率更高的算法。
booming 2005-11-20
  • 打赏
  • 举报
回复
3. 棋子移动问题。
(1)起始条件:有黑白各n个棋子(n≥4)排成一行,开始时白色棋子全部在左边,黑色棋子全部在右边。
(2)移动规则:
· 每次移动两个棋子,颜色不限;
· 可以移动到左边的空位,也可以移动到右边的空位上;
· 不能调换两个棋子的左右位置;
· 每次移动必须跳过若干棋子(不可平移)。
(3)要求:最后移成黑白相间的一行。
试给出移动程序。
mmmcd 2005-10-19
  • 打赏
  • 举报
回复
题目不清楚啊
起始空位在哪里?几个空位?
“不能调换两个棋子的左右位置”跟“跳过若干棋子”如何理解?
waterfirer 2005-10-19
  • 打赏
  • 举报
回复
没太看懂题目的意思,每次移动两个棋子是什么意思?
以n=4为例,举个例子好吗?
如00001111
....
01010101
booming 2005-10-19
  • 打赏
  • 举报
回复
up

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧