The eight queens problem is distorted, and can be solved with a deep search and backtracking approach. The program has made some changes, can output all the pieces of the scheme.
Input Format:
First, the input data is generated according to the topic requirement.
The first line of data is two positive integers, N m, separated by a space, indicating that the chessboard will be described in a matrix of n*n, and the number of pieces of M to be placed.
The next n lines describe the shape of the chessboard: N characters per line, where # represents the board area. Indicates a restricted area
For example:
4 4
####
.. ##
#.. #
###.
Problem Solving Ideas:
When a piece of chess on the board, this line and the same column can no longer lazi, remove this line, this column, the rest is quite a (n-1) * (n-1) of the board, the Lazi rule and the same as the last, all Lazi each step has the same sub-structure, you can use a recursive structure to solve the problem.
Use DFS (int r,int k) to do a deep search function, r to indicate the number of rows, and K for the number of pieces that have been placed. When the number of pieces placed is equal to the number of schemes and returned. Otherwise, try all the points of this line one at a time, when this point can place a pawn and this column has not let go of the pawn, put the pieces here, so that the K value plus one, mark this column has placed pieces, and search the next line. Also, after this line has been searched, Dfs is called to search for the next row, but the K value is not changed, because the number of pieces placed can be less than n, and there are some rows where the pieces are not placed.
output Format:
Each row outputs a scheme; each pair of numbers represents the row and column of the pawn. The last line outputs all the number of scenarios.
The following code is compiled in vc6.0 by:
#include <iostream> using namespace std; #define MAX 100+1//#代表的是棋盘区域, '. ' Represents a blank area cannot be placed chess pieces char A[max][max]; BOOL Col[max]; Used to record if column I is placing a pawn int n,m,count,row[max]; n is the size of the chessboard, M is the number of pieces to be placed, the row number where the placed piece is stored in row "", the first parameter represents the number of rows, the second parameter represents the number of pieces that have been filled//the first line here is denoted by 1, and so on void Dfs (int r,int k) {int i; /When m pieces are all placed, output all the points where the pieces are placed and add one to the scheme number. {count++;/*for (i=1;i<=n;i++) if (Col[i]) cout<<i<< "" << row[i]<< ":"; cout<<endl;*/ Return } if (r>n) return; for (i=1;i<=n;i++) {if (!col[i]&&a[r][i]== ' # ') {col[i]=1;//row[i]=r;//record its corresponding line number//This column is populated//Line down search DFS (r+1,k +1); Search for different columns of the same row//Mark this column filled with false col[i]=0; }} (R+1,k);//indicates that the R line failed to place the pawn//Because the pawn tree may be smaller than the number of checkerboard rows} int main () {while (cin>>n>>m)//accept and process the input {if (n==-1&&m== -1) break; count=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) cin>>a[i][j]; memset (col,0,sizeof (col)); The first parameter represents the first line of Dfs (1,0); cout<<count<<endl; } return 0; }
the comment out is used to output the said scheme.
This is a homework, so write more detailed. In the future will continue in this blog, I wrote all the worth of written ACM here, and everyone to communicate.
Come on, all acmer.
Just try your best.