2 solutions

  • 0
    @ 2024-3-10 19:27:01

    简洁明了

    AC呆马

    #include<bits/stdc++.h>
    using namespace std;
    int n,m,flag[110][110],ans;
    int fx[10]={2,1,-2,-1,2,1,-2,-1};
    int fy[10]={1,2,-1,-2,-1,-2,1,2};
    void dg(int x,int y,int count){
        if(count==n*m){
            ans++;
            return;
        }
        for(int i=0;i<8;i++){
            if(x+fx[i]>=0 && x+fx[i]<n && y+fy[i]>=0 && y+fy[i]<m && flag[x+fx[i]][y+fy[i]]==0){
                flag[x+fx[i]][y+fy[i]]=1;
                dg(x+fx[i],y+fy[i],count+1);
                flag[x+fx[i]][y+fy[i]]=0;
            }
        }
    }
    int main(){
        int t,sx,sy;
        cin>>t;
        while(t--){
            cin>>n>>m>>sx>>sy;
            flag[sx][sy]=1;
            dg(sx,sy,1);
            cout<<ans<<'\n';
            ans=0;
            flag[sx][sy]=0;
        }
        return 0;
    }
    
  • 0
    @ 2024-1-24 10:26:43

    **AC呆马:

    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <string>
    using namespace std;
    
    const int MAX = 10;
    int n, m, cnt = 0;
    bool visited[MAX][MAX];
    int dx[8] = {1,2,2,1,-1,-2,-2,-1};
    int dy[8] = {2,1,-1,-2,-2,-1,1,2};
    
    bool isValid(int x, int y)
    {
    	return x >= 0 && x < n && y >= 0 && y < m && !visited[x][y];
    }
    
    void dfs(int x, int y, int step)
    {
    	if(step == n * m - 1)
    	{
    		cnt++;
    		return;
    	}
    	visited[x][y] = true;
    	for(int i = 0; i< 8; i++)
    	{
    		int nx = x + dx[i];
    		int ny = y + dy[i];
    		if(isValid(nx,ny))
    			dfs(nx, ny, step + 1);
    	}
    	visited[x][y] = false;
    }
    int main()
    {
    	int t;
    	cin >> t;
    	while(t--)
    	{
    		cnt = 0;
    		memset(visited, 0, sizeof(visited));
    		int x, y;
    		cin >> n >> m >> x >> y;
    		dfs(x, y, 0);
    		cout << cnt << endl;
    	}
    	return 0;
    }
    
  • 1

Information

ID
705
Time
1000ms
Memory
256MiB
Difficulty
4
Tags
# Submissions
69
Accepted
30
Uploaded By