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; }
时间: 2024-09-16 05:59:36