码迷,mamicode.com
首页 > 其他好文 > 详细

青蛙交换(转)

时间:2015-08-31 13:47:17      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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;

}


青蛙交换(转)

标签:

原文地址:http://my.oschina.net/u/942328/blog/499422

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!