Backtracking Method: 4th questions-chess placement

Source: Internet
Author: User

[Problem description]

In the M * n main lattice, any x grids are specified to form a checker. K pieces are placed on any of the composite checkerboards, and any two pieces must not be in the same row or column, all solutions that meet the conditions must be output. (Note that the Board is sparse, that is, x <m * n/2. 1 <m, n <10 ).

Programming requirements:

1. For a given board, find the maximum number of pieces that can be placed on the board p.

2. note the total number of schemes where di places I pawns on the board (1 <I <p). The schemes obtained through rotation and mirror reflection are recorded as different schemes. For each I, find the corresponding di.

3. The program should be able to process multiple boards consecutively and output P, D1, D2 ,..., DP. You only need to output numbers and do not need to output specific solutions.

Input: the first line is two numbers, representing the M and N of the first Board. The following is an m x n matrix consisting of only 0 and 1, if the value of a position is 1, the corresponding grid is located on the board. If the value is 0, the corresponding grid is not located on the board.

Output: the first line is the maximum number of pieces that can be placed on the board;

The second row lists the total number of moves from one pawn to the p-pawn.

[Sample input]

5 50 1 1 1 00 1 0 0 01 1 1 0 00 0 1 0 00 0 1 1 0

[Output example]

the maxnumber=41:102:283:244:5

[Problem Analysis]

This question is very similar to the question of Queen N, but it does not require that each column in each row have a pawn. Therefore, in order to maximize the placement, some rows or columns do not need to be placed in the pawns.

Because x <1/2mn is sparse, It is very inefficient to search by row. Therefore, you can open an array to record all the grids on the board.

The following code is provided:

VaR chess: array [1 .. 50] of record X, Y: integer; end; // This array is used to record the sum, M, N, P: byte; line, row: array [1 .. 10] of Boolean; // line. Row indicates a row. If a column has no pawns, true indicates none. False indicates D: array [1 .. 10] of longint; // used to record the number of solutions for placing the I-th piece. Please think about why the maximum number of arrays is 10? Procedure Init; // initialize var I, j, INT: integer; begin fillchar (row, sizeof (ROW), true); fillchar (line, sizeof (line), true ); fillchar (D, sizeof (D), 0); assign (input, 'word. in '); reset (input); assign (output, 'word. out'); rewrite (output); readln (m, n); sum: = 0; for I: = 1 to M do for J: = 1 to n do begin read (INT); If Int = 1 then begin Inc (SUM); chess [Sum]. x: = I; chess [Sum]. y: = J end; Procedure work (next, S: integer ); // search for a position where the second chess piece can be placed from the position where the next play can be placed var I, X, Y: integer; begin for I: = Next To sum do begin X: = chess [I]. x; Y: = chess [I]. y; // use the X and Y variables to indicate if line [X] and row [y] Then begin Inc (d [s]); line [x]: = false; row [y]: = false; Work (I + 1, S + 1); line [x]: = true; row [y]: = true; // backend; end; end; Procedure print; var I: integer; begin for I: = 1 to 10 do if d [I] = 0 Then break; P: = I-1; writeln ('the maxnumber = ', P); for I: = 1 to P do writeln (1,': ', d [I]); close (input ); close (output); end; begin Init; Work (1, 1); print; end.

Standard procedure:

var   chess:array [1..50] of  record                              x,y:integer;                           end;   row,col:array  [1..10]  of  boolean;   d:array [1..10]  of  longint;   m,n,p,sum:byte; procedure  init; var  i,j,int:integer; begin   sum:=0;   for i:=1 to m do     for j:=1 to n do       begin         read(int);         if  int=1  then           begin             inc(sum);             chess[sum].x:=i;             chess[sum].y:=j           end;       end;       fillchar(row,sizeof(row),true);       fillchar(col,sizeof(col),true);       fillchar(d,sizeof(d),0); end; procedure work(next,s:integer); var  i,x,y:integer; begin   for  i:=next  to  sum  do     begin       x:=chess[i].x;y:=chess[i].y;       if  (row[x])and(col[y])  then         begin           inc(d[s]); row[x]:=false;col[y]:=false;           work(i+1,s+1);           row[x]:=true;col[y]:=true         end;     end; end; procedure print; var i:integer; begin   for  i:=10 downto 1  do     if  d[i]>0  then break;   p:=i;   writeln(‘the maxnumber=‘,p);   for  i:=1  to  p  do     writeln(i,‘:‘,d[i]) end; begin  assign(input,‘word.in‘);reset(input);  assign(output,‘word.out‘);rewrite(output);  readln(m,n);  while (m<>0) and (n<>0) do    begin      init;      work(1,1);      print;      readln(m,n);    end; end.

I wrote something similar to a standard program, just read one.

This question is really simple, but I reversed the variable name ...... Then I made the call for one night ............ A waste of time

Do not rush to compile and run the program after writing the program, and watch the program after an error occurs ...... You should read the code carefully from start to end ......

This can save a lot of time and avoid unnecessary troubles ......

Pay special attention to this point when you click farming questions ........................

Lessons learned ..................

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.