3 solutions

  • 0
    @ 2023-10-22 23:24:41
    #include<iostream>
    using namespace std;
    #define N 20
    struct point {
    	int x;
    	int y;
    };
    bool vis[N][N];
    point path[N*N];
    int dx[4]={2,1,-1,-2};
    int dy[4]={1,2,2,1};
    int cnt;
    void dfs(int x,int y,int step){
    	if(x==4&&y==8){
    		cnt++;
    		cout<<cnt<<":";
    		for(int i=0;i<step;i++)cout<<path[i].x<<","<<path[i].y<<"->";
    		cout<<"4,8"<<endl;
    		return ;
    	}
    	for(int i=0;i<4;i++){
    		int tx=x+dx[i];
    		int ty=y+dy[i];
    		if(tx<0||ty<0||tx>=5||ty>=9)continue;
    		step++;
    		path[step].x=tx;
    		path[step].y=ty;
    		vis[tx][ty]=true;
    		dfs(tx,ty,step);
    		vis[tx][ty]=false;
    		step--;
    	}
    }
    int main(){
    	path[0].x=0;
    	path[0].y=0;
    	vis[0][0]=true;
    	dfs(0,0,0);
    	return 0;
    }
    

    Information

    ID
    699
    Time
    1000ms
    Memory
    16MiB
    Difficulty
    5
    Tags
    # Submissions
    96
    Accepted
    36
    Uploaded By