1 solutions

  • 2
    @ 2022-7-16 18:53:22

    稍微有点不是那么简洁

    先处理黑棋再处理白棋

    #include<bits/stdc++.h>
    using namespace std;
    #define ioio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define endl "\n"
    #define debug(x) cout<<#x<<":"<<x<<endl;
    #define L(k) k<<1
    #define R(k) k<<1|1
    #define P pair
    #define P1 first
    #define P2 second
    #define u_map unordered_map
    #define p_queue priority_queue
    typedef long long ll;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    const int N = 10 + 7;
    int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
    /*-------------------------------------------------*/
    
    int n;
    int ma[N][N], vis[N][N];
    int ans;
    map<vector<int>, int>cnt;
    
    int cheak(int x, int y, int u) {
    	for (int i = 1; i <= n; i++) {
    		if (ma[i][y] == u && i != x)
    			return 0;
    		if (ma[x][i] == u && i != y)
    			return 0;
    	}
    	for (int i = x, j = y; i >= 1 && j >= 1; i--, j--) {
    		if (i == x && j == y)continue;
    		if (ma[i][j] == u)return 0;
    	}
    	for (int i = x, j = y; i >= 1 && j <= n; i--, j++) {
    		if (i == x && j == y)continue;
    		if (ma[i][j] == u)return 0;
    	}
    	for (int i = x, j = y; i <= n && j >= 1; i++, j--) {
    		if (i == x && j == y)continue;
    		if (ma[i][j] == u)return 0;
    	}
    	for (int i = x, j = y; i <= n && j <= n; i++, j++) {
    		if (i == x && j == y)continue;
    		if (ma[i][j] == u)return 0;
    	}
    	return 1;
    }
    void dfs_b(int x, int y, int b) {
    	if (b == n) {
    		ans++;
    		return ;
    	}
    	int flag = 0;
    	for (int i = x; i <= n; i++) {
    		for (int j = flag == 0 ? y : 1; j <= n; j++) {
    			flag = 1;
    			if (vis[i][j] == 1) {
    				vis[i][j] = 0;
    				ma[i][j] = 1;
    				if (cheak(i, j, 1))
    					dfs_b(i, j, b + 1);
    				ma[i][j] = -1;
    				vis[i][j] = 1;
    			}
    		}
    	}
    }
    void dfs_a(int x, int y, int a) {
    	if (a == n) {
    		for (int i = 1; i <= n; i++)
    			for (int j = 1; j <= n; j++)
    				if (vis[i][j] == 1) {
    					vis[i][j] = 0;
    					ma[i][j] = 1;
    					dfs_b(i, j, 1);
    					ma[i][j] = -1;
    					dfs_b(i, j, 0);
    					vis[i][j]=1;
    					goto en2;
    				}
    en2:
    		x=y;
    		return ;
    	}
    	int flag = 0;
    	for (int i = x; i <= n; i++) {
    		for (int j = flag == 0 ? y : 1; j <= n; j++) {
    			flag = 1;
    			if (vis[i][j] == 1) {
    				vis[i][j] = 0;
    				ma[i][j] = 0;
    				if (cheak(i, j, 0))
    					dfs_a(i, j, a + 1);
    				ma[i][j] = -1;
    				vis[i][j] = 1;
    			}
    		}
    	}
    }
    int main() {
    	cin >> n;
    	memset(ma, -1, sizeof ma);
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= n; j++)
    			cin >> vis[i][j];
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= n; j++)
    			if (vis[i][j] == 1) {
    				vis[i][j] = 0;
    				
    				ma[i][j] = 0;
    				dfs_a(i, j, 1);
    				
    				vis[i][j]=1;
    				ma[i][j] = -1;
    				//这里处理成j+1有点问题
    				dfs_a(i, j+1, 0);
    				goto en;
    			}
    en:
    	cout << ans << endl;
    	return 0;
    }
    
    • 1

    Information

    ID
    1817
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    # Submissions
    5
    Accepted
    1
    Uploaded By