https://leetcode.com/problems/n-queens-ii/
跟上一题完全一样啊
class Solution { public: int totalNQueens(int n) { int ret = 0; for(int i=0;i<n;i++) vis[i] = 0; int pos[n]; dfs(ret, pos, 0, n ); return ret; } void dfs( int &ret, int pos[], int ptr, int n ) { if(ptr > n) return; if(ptr == n) { ret++; return; } for(int i=0;i<n;i++) { if(vis[i])continue; int flag = 1; for(int j=0;j<ptr;j++) { if(ptr-j == i-pos[j] || ptr-j == pos[j]-i ) { flag = 0; break; } } if(flag) { vis[i] = 1; pos[ptr] = i; dfs(ret, pos, ptr+1, n); vis[i] = 0; } } } private: int vis[10001]; };