青蛙交换(转)

http://britton.disted.camosun.bc.ca/frog_puzzle.htm

#include <iostream>

using namespace std;


int IfSuccess(int* p_result){
	int success = 1;

	for(int i = 0; i < 3; i ++){
		if(p_result[i] == 0
			|| p_result[i] < 4)
		{
			success = 0;
			break;
		}

	}
	for(int i = 4; success && i < 7; i ++){
		if(p_result[i] == 0
			|| p_result[i] >= 4)
		{
			success = 0;
			break;
		}

	}

	return success;
}

int Jump(int* p_step[], int blank, int step){
	int success = 0;
	int step_count = 0;

	success = IfSuccess(*(p_step + step));
	if(success){
		step_count = step + 1;
		return step_count;
	}

	if(blank -2 >= 0 && (*(p_step + step))[blank - 2] < 4){
		for(int i = 0; i < 7; i ++){
			(*(p_step + step + 1))[i] = (*(p_step + step))[i];
		}
		(*(p_step + step + 1))[blank] = (*(p_step + step))[blank - 2];
		(*(p_step + step + 1))[blank - 2] = 0;	  	
		success = Jump(p_step, blank - 2, step + 1);
	}
	if(!success && blank -1 >= 0 && (*(p_step + step))[blank - 1] < 4){
		for(int i = 0; i < 7; i ++){
			(*(p_step + step + 1))[i] = (*(p_step + step))[i];
		}
		(*(p_step + step + 1))[blank] = (*(p_step + step))[blank - 1];
		(*(p_step + step + 1))[blank - 1] = 0;	  	
		success = Jump(p_step, blank - 1, step + 1);
	}
	if(!success && blank + 2 < 7 && (*(p_step + step))[blank + 2] >= 4){
		for(int i = 0; i < 7; i ++){
			(*(p_step + step + 1))[i] = (*(p_step + step))[i];
		}
		(*(p_step + step + 1))[blank] = (*(p_step + step))[blank + 2];
		(*(p_step + step + 1))[blank + 2] = 0;	  	
		success = Jump(p_step, blank + 2, step + 1);
	} 
	if(!success && blank + 1 < 7 && (*(p_step + step))[blank + 1] >= 4){
		for(int i = 0; i < 7; i ++){
			(*(p_step + step + 1))[i] = (*(p_step + step))[i];
		}
		(*(p_step + step + 1))[blank] = (*(p_step + step))[blank + 1];
		(*(p_step + step + 1))[blank + 1] = 0;	  	
		success = Jump(p_step, blank + 1, step + 1);
	}
	return success;
}


int main(void){
	int quest[7] = {1, 2, 3, 0, 4, 5, 6};	

	int buffer[500][7];
	int* answer[500] = {0};

	for(int i = 0; i < 500; i ++){
		answer[i] = buffer[i];
	}
	for(int i = 0; i < 7; i ++){
		answer[0][i] = quest[i];	
	}
	int success = Jump(answer, 3, 0); 

	if(success){
		for( int j = 0; j < success; j ++){
			for(int k = 0; k < 7; k++){
				cout<<answer[j][k];
				if( answer[j][k] < 4 && answer[j][k] != 0){
					cout<<">";
				}else if( answer[j][k] >=4 && answer[j][k] != 0){
					cout<<"<";
				}		
				if(k < 6){
					cout<<",";
				}
			}
			cout<<endl;
		}
	}
	return 0;

}


转载于:https://my.oschina.net/u/942328/blog/499422

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值