Shooting competition (POJ 1719)

Source: Internet
Author: User

"Problem description"

We assume that the target of the shot is a rectangular grid of r*c (2≤r≤c≤1000) small squares. Each column in the grid has 2 white squares and R-2 a small black square. The rows that define the grid are numbered 1~r from top to bottom, and the columns are numbered 1~c from left to right. The shooter can shoot C times. In a continuous C shot, the shot is correct if there is exactly one white square shot in each column and no white squares are shot in the row. Now gives the n set of data, for each set of data, if there is the correct shooting method, then ask to find it, if not present, output No.

"Sample Input"

2
4 4

2 4

3 4

1 3

1 4

5 5

1 5

2 4

3 4

2 4

2 3

"Sample Output"

2 3 1 4

NO

"Problem-solving ideas"

This is titled 1997 Ceoi last Question, the thinking of solving problems mainly lies in the greedy strategy and the proof of greed.

Greedy strategy:

1. Count the number of white cells that all rows contain.

2. Select a minimum number of white lattices from a row that has not yet been shot.

3. Check the selected row (1) If the selected row has a white number of 0, then the output is no solution, or (2) Otherwise, select one of the white cells of the selected row as the shooting cell, and subtract 1 from the number of rows in the other white grid that is in the same column as the grid.

4. Return to the 2nd step until all the lines have a shot lattice.

5, if there is no selection of shooting lattice, then in the column to choose a white lattice as a shooting lattice can be.

Greedy Proof:

Use H[i] to denote the number of white cells in line I.        If at first: ①min{h[i]}=0: line I has no way to find the white lattice that can be used as a shooting lattice, then the problem can only be solved. ②min{h[i]}=1: Then the first line of this white lattice must be used as a shooting lattice, otherwise there will be no shooting lattice on line I to cause problems without solution.

③min{h[i]}≥2: So in this line of choice a white lattice, at most, will only cause the remaining line has a line h value of 1, and then processing that line, at most will only cause the remaining line has a line h value of 1, so reciprocating, will maintain the H value of 1 of the number of rows not more than 1 lines, Finally, the worst case scenario is that the H value of the last line is 1, and all rows are selected for the shot lattice. Therefore, if the original problem has a solution, the greedy method must be able to find a correct scheme. It can be proved that this greedy method is correct. Determine the greedy standard.

"Code Implementation"
1 varA:Array[1.. +] ofLongint;2B:Array[1.. +,1.. +] ofBoolean;3FR,FC:Array[1.. +] ofBoolean;4AnsArray[1.. +] ofLongint;5 N,i,j,r,c,x1,x2,k,pos,min,code,q:longint;6 Flag:boolean;7 begin8 READLN (code);9   forq:=1  toCode DoTen   begin One Fillchar (Fr,sizeof (FR), false); A Fillchar (fc,sizeof (FC), false); -Fillchar (A,sizeof (a),0); -Fillchar (b,sizeof (b),0);//note initialization, not initializing WA several times ... the readln (r,c); -     fori:=1  toC Do -     begin - readln (x1,x2); +b[x1,i]:=true;b[x2,i]:=true; - Inc (A[X1]); Inc (A[X2]); +     End; A    Repeat atmin:=Maxlongint; -flag:=true; -      fori:=1  toR Do -      if  not(Fr[i]) Then - Break ; -     if  not(Fr[i]) Then inflag:=false; -     ifFlag ThenBreak ; to     fori:=1  toR Do +     if(a[i]<min) and( not(Fr[i])) Then -      begin themin:=A[i]; *pos:=i; $      End;Panax Notoginseng    ifa[pos]=0  Then -     begin theWriteln ('NO'); + Break ; A     End; thefr[pos]:=true; +     forj:=1  toC Do -     if(B[pos,j]) and( not(Fc[j])) Then $      begin $ans[j]:=pos;fc[j]:=true; -        fork:=1  toR Do -        if(B[k,j]) and(K<>pos) Then the Dec (a[k]); - Break ;Wuyi      End; the    untilFlag; -    ifa[pos]=0  Then Wu continue; -     fori:=1  toC Do About     ifans[i]=0  Then $       forj:=1  toR Do -       ifB[j,i] Then -        begin -ans[i]:=J; A Break ; +        End; theWrite (ans[1]); -     fori:=2  toC Do $Write' ', Ans[i]); the Writeln; the   End; the End.
View Code

Shooting competition (POJ 1719)

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.