Checker Challenge(dfs深度搜索 打印八皇后)
来源:互联网 发布:java 获取locale 编辑:程序博客网 时间:2024/06/06 10:46
Judge Info
- Memory Limit: 65536KB
- Case Time Limit: 3000MS
- Time Limit: 3000MS
- Judger: Number Only Judger
Description
Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)
Column 1 2 3 4 5 6 -------------------------1 | | O | | | | | -------------------------2 | | | | O | | | -------------------------3 | | | | | | O | -------------------------4 | O | | | | | | -------------------------5 | | | O | | | | -------------------------6 | | | | | O | | -------------------------
The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:
This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.
Input
The first number means how many test cases will followed. For every test case will be a single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.
Output
For each test cases, print the follow contains: The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.
Sample Input
16
Sample Output
2 4 6 1 3 53 6 2 5 1 44 1 5 2 6 34
把八皇后的所有情况打印出来
#include<iostream>using namespace std; int plc[13];int plc_p;int map[13][13];bool flag[13];int n;int ans; void dfs(){if(plc_p==n){ans++;if(ans<4)for(int i=0;i<n;i++){if(i!=n-1) cout<<plc[i]+1<<" ";else cout<<plc[i]+1<<endl;}} for(int i=0;i<n;i++){if((i!=0)&&flag[i-1]==0)break;if(flag[i]==0){flag[i]=1;for(int j=0;j<n;j++){if(map[i][j]==0){for(int k=0;k<n;k++)map[k][j]++;for(int k=0;k<n;k++)map[i][k]++;int ii=i,jj=j;while(ii>-1&&jj>-1)map[ii--][jj--]++;ii=i,jj=j;while(ii<n&&jj<n)map[ii++][jj++]++;ii=i,jj=j;while(ii>-1&&jj<n)map[ii--][jj++]++;ii=i,jj=j;while(ii<n&&jj>-1)map[ii++][jj--]++;map[i][j]=1;plc[plc_p++]=j;dfs();plc_p--;for(int k=0;k<n;k++)map[k][j]--;for(int k=0;k<n;k++)map[i][k]--;ii=i,jj=j;while(ii>-1&&jj>-1)map[ii--][jj--]--;ii=i,jj=j;while(ii<n&&jj<n)map[ii++][jj++]--;ii=i,jj=j;while(ii>-1&&jj<n)map[ii--][jj++]--;ii=i,jj=j;while(ii<n&&jj>-1)map[ii++][jj--]--;map[i][j]=0;}}flag[i]=0;}}} int main(){int t;cin>>t;while(t--){cin>>n;for(int i=0;i<n;i++)int plc_p=0;for(int i=0;i<n;i++)for(int j=0;j<n;j++)map[i][j]=0;for(int i=0;i<n;i++)flag[i]=0;ans=0;dfs();cout<<ans<<endl;}return 0;}
dfs的基本过程:
dfs()
判明终止条件
for全部物件
标记
dfs()
恢复
- Checker Challenge(dfs深度搜索 打印八皇后)
- 【搜索】【USACO】Checker Challenge
- 八皇后(dfs)
- USACO 1.5 Checker Challenge (DFS)
- 深度优先搜索DFS:八皇后问题(去掉不能再一条斜线上的条件) C++
- USACO/Checker Challenge(位运算的搜索)
- HDU-N皇后问题-DFS深度搜索
- 八皇后问题与深度优先搜索
- 八皇后问题深度遍历搜索
- 洛谷Oj-八皇后-深度优先搜索
- USACO section 1.5 Checker Challenge(dfs深搜)
- USACO 1.5 Checker Challenge (DFS + 剪枝)
- USACO 1.5.4 Checker Challenge (N皇后问题) 位操作解法
- POJ 1321 棋盘问题类似八皇后 dfs搜索
- USACO Section 1.5 Checker Challenge - 普普通通的皇后问题~~
- Checker Challenge
- Checker Challenge
- Checker Challenge
- MySql数据库无法启动之1067错误
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
- ZOJ-1586 QS Network
- HDU2566 统计硬币
- Objective-C NSDictionary & NSSet
- Checker Challenge(dfs深度搜索 打印八皇后)
- ACdream区域赛指导赛之手速赛系列(4) C Captain Hammer
- Sublime-Text2 在Linux下的安装
- java大数的基本函数
- 【读书笔记】当析构函数遇到多线程 ── C++中线程安全的对象回调
- 【Nutch2.2.1基础教程之1】nutch相关异常
- JavaScript学习 jquery10 焦点事件
- Delphi XE4 iOS应用程序开发(1)
- WebSocket初探