Kongmingqi-like C language experiment, optimization problem of overlapping sub-results in depth-first search

A few days ago, I helped a friend write a Kong Mingqi-like algorithm.
question above      
5*5 chessboard . means no piece o means there are pieces
eg: oooooo
         ooooo
         Yes Yes 
         ooooo
         ooooo

The way of walking is the same as that of Kong Ming.
The beginning is directly a deep search without using an array of data structures
#include<stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char data[5][5]; //chessboard
int tag; //Record how many pieces are on the board
int n=0; //Record the number of programs
int scheme [25][4];
int code; //record data

int path(int z){ //Recursive restore data plus output data
	if(z==0){ //Condition for the end of the recursion
	for(int i=0;i<5;i++){
	  for(int j=0;j<5;j++)
	    printf("%c",data[i][j]);
	printf("\n");    	
	}
	printf("\n");
	}
	else{
	data[(scheme[z][0]+scheme[z][2])/2][(scheme[z][1]+scheme[z][3])/2]='o';
	data[scheme[z][0]][scheme[z][1]]='o';
	data[scheme[z][2]][scheme[z][3]]='.';
	path(z-1); //Recursion above is to restore data and below is to move chess pieces
	data[(scheme[z][0]+scheme[z][2])/2][(scheme[z][1]+scheme[z][3])/2]='.';
	data[scheme[z][0]][scheme[z][1]]='.';
	data[scheme[z][2]][scheme[z][3]]='o';
	for(int i=0;i<5;i++){
	  for(int j=0;j<5;j++)
	    printf("%c",data[i][j]);
	printf("\n");
	}
	printf("\n");
	}
}
					 
int dfs(int x,int y,int z){                                        //深搜
	if(tag==1) {
	    n++; //Record the feasible number
	    path(z-1); // Initial output chessboard
		/* //Method output
		printf("Program is: ");
		for(int i=0;i<z;i++)
		printf("%d.%d->%d.%d  ",scheme[i][0],scheme[i][1],scheme[i][2],scheme[i][3]);
		printf("\n");
		*/	
		return 1;
	}
	
	for(int i=0;i<5;i++){
	   for(int j=0;j<5;j++){                       
	 	if(data[i][j]=='o') { //Double loop plus judgment to find out the position of the pieces  
	 		x=i;
	 		y=j; //Determine whether each direction can go
	        if(x>=2&&data[x-1][y]=='o'&&data[x-2][y]=='.'){            //上
	              data[x][y]='.';                                 
		          data[x-1][y]='.';
		          data[x-2][y]='o';
		          scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x-2;  scheme[z][3]=y;
	              tag--;
				  code=dfs(0,0,z+1); //The above is to move the position record position to reduce the number of pieces
		          if(code==1) return 1; // recurse to the next level
		          data[x][y]='o'; //The following is to restore the position to increase the number of pieces  
		          data[x-1][y]='o';
		          data[x-2][y]='.';
		          tag++;
		          
	        }
           	if(x<=2&&data[x+1][y]=='o'&&data[x+2][y]=='.'){     //下
		          data[x][y]='.';
		          data[x+1][y]='.';
	              data[x+2][y]='o';
	              scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x+2;  scheme[z][3]=y;
		          tag--;
		          code=dfs(0,0,z+1);
	              if(code==1) return 1;
		          data[x][y]='o';
	              data[x+1][y]='o';
		          data[x+2][y]='.';
	              tag++;
	       }
	       if(y>=2&&data[x][y-1]=='o'&&data[x][y-2]=='.'){     //左
	              data[x][y]='.';
		          data[x][y-1]='.';
	              data[x][y-2]='o';
	              scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x;  scheme[z][3]=y-2;
		          tag--;
		          if(dfs(0,0,z+1)==1) return 1;
	              data[x][y]='o';
	              data[x][y-1]='o';
		          data[x][y-2]='.';
		          tag++;
	       }
	       if(y<=2&&data[x][y+1]=='o'&&data[x][y+2]=='.'){     //右
		         data[x][y]='.';
		         data[x][y+1]='.';
		         data[x][y+2]='o';
		         scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x;  scheme[z][3]=y+2;
	             tag--;
	             code=dfs(0,0,z+1);
		         if(code==1) return 1;
		         data[x][y]='o';
		         data[x][y+1]='o';
		         data[x][y+2]='.';
		         tag++;
	       }   
	 	}
	 }
	}
    return 0;
	
}


int main(){
	int k=0;
	for(int i=0;i<5;i++){
	   for(int j=0;j<5;j++){
	   	cin>>data[i][j];
	   }
	} //input
	for(int i=0;i<5;i++){
	   for(int j=0;j<5;j++){
	   	if(data[i][j]=='o') k++;
	   } //Record the initial number of pieces
	}
	day=k;
	printf("\n");
	dfs(0,0,0); //deep search
	if(n==0) printf("无解\n");
	return 0;	
}

Then the problem came. 2 hours did not run out of results. Simple depth-first search is simply exhaustive, so it needs to be pruned
Later, I plan to use space for time, add a mark, and remove overlapping sub-problems
bool record[32][32][32][32][32]; //mark this to record infeasible subproblems
later revisions



#include<stdio.h>
#include <iostream>
#include <time.h>
#include <string.h>
using namespace std;
char data[6][6]; //chessboard
int tag; //Record how many pieces are on the board
int n=0; //Record the number of programs
int scheme [25][4];
int code; //record data
bool record[32][32][32][32][32]; //mark
int sum 5

void path(int z){ //Recursive restore data plus output data
	if(z==0){ //Condition for the end of the recursion
	for(int i=0;i<5;i++){
	  for(int j=0;j<5;j++)
	    printf("%c",data[i][j]);
	printf("\n");    	
	}
	printf("\n");
	}
	else{
	data[(scheme[z][0]+scheme[z][2])/2][(scheme[z][1]+scheme[z][3])/2]='o';
	data[scheme[z][0]][scheme[z][1]]='o';
	data[scheme[z][2]][scheme[z][3]]='.';
	path(z-1); //Recursion above is to restore data and below is to move chess pieces
	data[(scheme[z][0]+scheme[z][2])/2][(scheme[z][1]+scheme[z][3])/2]='.';
	data[scheme[z][0]][scheme[z][1]]='.';
	data[scheme[z][2]][scheme[z][3]]='o';
	for(int i=0;i<5;i++){
	  for(int j=0;j<5;j++)
	    printf("%c",data[i][j]);
	printf("\n");
	}
	printf("\n");
	}
}
					 
int dfs(int x,int y,int z){                                        //深搜
	if(tag==1) {
		for(int i=0;i<5;i++)
		  for(int j=0;j<5;j++){
		  	if(data[i][j]==0) data[i][j]='.';
		  	else data[i][j]='o';
		  }
		
	    n++;
	    path(z-1);
		/*
		printf("Program is: ");
		for(int i=0;i<z;i++)
		printf("%d.%d->%d.%d  ",scheme[i][0],scheme[i][1],scheme[i][2],scheme[i][3]);
		printf("\n");
		*/
		return 1;
	}
	for(int i=0;i<5;i++)
	 sum[i]=data[i][0]*16+data[i][1]*8+data[i][2]*4+data[i][3]*2+data[i][4];
	if(record [sum[0]][sum[1]][sum[2]][sum[3]][sum[4]]==false)  return 0;
	
	
	for(int i=0;i<5;i++){
	   for(int j=0;j<5;j++){                       
	 	if(data[i][j]==1) { //Double loop plus judgment to find out the position of the pieces  
	 		x=i;
	 		y=j; //Determine whether each direction can go
	        if(x>=2&&data[x-1][y]==1&&data[x-2][y]==0){            //上
	              data[x][y]=0;                                 
		          data[x-1][y]=0;
		          data[x-2][y]=1;
		          scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x-2;  scheme[z][3]=y;
	              tag--;
				  code=dfs(0,0,z+1); //The above is to move the position record position to reduce the number of pieces
		          if(code==1) return 1; // recurse to the next level
		          data[x][y]=1; //The following is to restore the position to increase the number of pieces  
		          data[x-1][y]=1;
		          data[x-2][y]=0;
		          tag++;
		          
	        }
           	if(x<=2&&data[x+1][y]==1&&data[x+2][y]==0){     //下
		          data[x][y]=0;
		          data[x+1][y]=0;
	              data[x+2][y]=1;
	              scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x+2;  scheme[z][3]=y;
		          tag--;
		          code=dfs(0,0,z+1);
	              if(code==1) return 1;
		          data[x][y]=1;
	              data[x+1][y]=1;
		          data[x+2][y]=0;
	              tag++;
	       }
	       if(y>=2&&data[x][y-1]==1&&data[x][y-2]==0){     //左
	              data[x][y]=0;
		          data[x][y-1]=0;
	              data[x][y-2]=1;
	              scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x;  scheme[z][3]=y-2;
		          tag--;
		          if(dfs(0,0,z+1)==1) return 1;
	              data[x][y]=1;
	              data[x][y-1]=1;
		          data[x][y-2]=0;
		          tag++;
	       }
	       if(y<=2&&data[x][y+1]==1&&data[x][y+2]==0){     //右
		         data[x][y]=0;
		         data[x][y+1]=0;
		         data[x][y+2]=1;
		         scheme[z][0]=x;  scheme[z][1]=y;  scheme[z][2]=x;  scheme[z][3]=y+2;
	             tag--;
	             code=dfs(0,0,z+1);
		         if(code==1) return 1;
		         data[x][y]=1;
		         data[x][y+1]=1;
		         data[x][y+2]=0;
		         tag++;
	       }   
	 	}
	 }
	}
	for(int i=0;i<5;i++)
	  sum[i]=data[i][0]*16+data[i][1]*8+data[i][2]*4+data[i][3]*2+data[i][4];
     record[i[0]][sum[1]][sum[2]][sum[3]][sum[4]=false;
    return 0;
	
}


int main(){
	int k=0;
    for(int i=0;i<5;i++){
    	for(int j=0;j<5;j++)
    	 cin>>data[i][j];
    }
                           //enter
	for(int i=0;i<5;i++){
	   for(int j=0;j<5;j++){
	   	if(data[i][j]=='o') {
	   		k++;
	   		data[i][j]=49-'0';
	   	}
	   	else {
		    data[i][j]=49-'1';
		}    
	   } //Record the initial number of pieces
	}
	for(int i1=0;i1<32;i1++) //Record the number of initial pieces
	 for(int i2=0;i2<32;i2++)
	  for(int i3=0;i3<32;i3++)
	   for(int i4=0;i4<32;i4++)
	    for(int i5=0;i5<32;i5++){
	    	record[i1][i2][i3][i4][i5]=true;
	    }
	day=k;
	printf("\n");
	dfs(0,0,0); //deep search
	if(n==0) printf("无解\n");
	return 0;	
}

The running time of the revised version is within 1S 
In addition, attach a Kongming-like chess algorithm written by others that I saw.
https://blog.csdn.net/tmljs1988/article/details/6039101

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763731&siteId=291194637