牛奶供应(三)

时间:2024-07-18 19:38:14
【C4】【搜索】【回溯】N皇后

Serendipity-Solitude: #include<bits/stdc++.h> using namespace std; long long The_length,Chessboard[14][14],sum; long long l[14],Diagonal_1[500],Diagonal_2[500]; void Search_and_trace_back(long long Place){ if(Place>The_length){ sum++;return; }for(long long i=1;i<=The_length;i++){ if(l[i]==0&&Diagonal_1[Place-i+The_length]==0&&Diagonal_2[Place+i]==0){ Chessboard[Place][i]=1;l[i]=1; Diagonal_1[Place-i+The_length]=1; Diagonal_2[Place+i]=1; Search_and_trace_back(Place+1); Chessboard[Place][i]=0;l[i]=0; Diagonal_1[Place-i+The_length]=0; Diagonal_2[Place+i]=0; } } }int main(){ cin>>The_length; Search_and_trace_back(1); cout<<sum; }//If there are similarities, it is purely a coincidence, manual translation. 这是N皇后