Ski Resort Construction{GoldQuestion2}
[Problem description]
The design map of the ski resort is a matrix of M * nm x n (1 <= m, n <= 100). Each lattice uses a letter R (indicating rough) or S (indicating flat ).
For example:
Rsrsss
Rsrsss
Rsrsss
Farmer John's tractor can mark all B * B (B <= m, B <= N) Areas B * B (B <= m, B <= N) each time) he wants B to be as big as possible. A grid can be marked multiple times. The next tag can overwrite the previous one. Each grid can be marked at least once.
[File input]
In the first line, the two integers separated by spaces represent M and N respectively.
Next 2. m + 1 line, each line contains an M Symbol, R or S. Indicates the target State marked.
[File output]
A total of rows. An integer indicates the maximum value of B.
[Input example]
3 6
Rsrsss
Rsrsss
Rsrsss
[Output example]
3
[Example]
First, mark all columns 1st to 3rd as R, then all columns 2nd to 4th as s, and then all columns 3rd to 5 as R, finally, mark all columns 4th to 6 as S.
Idea: Consider the maximum side length of a square that can be placed in the last step each time, and mark each vertex on the square as arbitrary (R or S), and so on, until all vertices are marked as arbitrary and the running ends. Find f [I, j]: = min (F [I-1, J], F [I, J-1]), f [I-1, j-1]) + 1. In this case, the last point will time out. In this case, we need to use a little bit of tips. It is called the card-time algorithm. It is estimated that the best operation can be achieved several times, and then the card is stuck. The code below
1 var 2 A, F, G: array [-1 .. 100,-1 .. 100] of longint; // A is the letter code 1 that represents R, 2 that represents S, 0 that represents arbitrary; F, G represents the maximum square 3 used ending with R and S respectively: array [-1 .. 100,-1 .. 100] of Boolean; // used indicates whether the vertex has been processed 4 I, j, n, m, TX, Ty, ANS, Posi, posj, flag, time, maxfg: longint; // The number of remaining unprocessed points in the flag record; time is used for timing 5 6 Procedure openit; 7 begin 8 assign (input, 'skicourse. in '); assign (output, 'skicourse. out'); 9 reset (input); rewrite (output); 10 end; 11 12 Procedure closeit; 13 inin14 close (input); close (output); 15 end; 16 17 procedure dataIn; // Data Reading and processing 18 var I, j: longint; 19 CH: Char; 20 begin21 readln (n, m); 22 flag: = N * m; ans: = 10000000; Time: = 0; 23 // fillchar (used, sizeof (used), false); 24 // fillchar (F, sizeof (F), 0); 25 // fillchar (G, sizeof (G), 0); 26 for I: = 1 to n do27 begin28 for J: = 1 to M do29 begin30 read (CH); 31 if CH = 'R' then a [I, j]: = 132 else a [I, j]: = 2; 33 end; 34 readln; 35 end; 36 end; 37 38 function min (A, B: longint): longint; 39 begin40 if a <B then min: = a41 else min: = B; 42 end; 43 44 function max (A, B: longint): longint; 45 begin46 if A> B then Max: = a47 else MAX: = B; 48 end; 49 50 51 begin52 openit; 53 dataIn; 54 while flag> 0 do55 begin56 for I: = TX to n do // process the side length of the largest square currently 57 for J: = ty to M do58 begin59 f [I, j]: = min (F [I-1, J], F [I, J-1]), f [I-1, J-1]) + 1; 60g [I, j]: = min (G [I-1, J], G [I, J-1]), g [I-1, J-1]) + 1; 61 If a [I, j] = 1 then G [I, j]: = 0; 62 if a [I, j] = 2 then f [I, j]: = 0; 63 end; 64 posi: = 1; posj: = 1; maxfg: = 0; 65 for I: = 1 to n do // find the location of the largest square 66 for J: = 1 to M do67 if (max (F [I, j], G [I, j])> maxfg) and not used [I, j] then68 begin69 posi: = I; posj: = J; 70 maxfg: = max (F [I, j], G [I, j]); 71 end; 72 If maxfg <ans then ans: = maxfg; 73 used [Posi, posj]: = true; 74 for I: = posi-maxfg + 1 to posi do // modify the source image 75 for J: = posj-maxfg + 1 to posj do76 if a [I, j] <> 0 then77 begin78 Dec (FLAG); 79 A [I, j]: = 0; 80 end; 81 TX: = posi-maxfg + 1; TY: = posj-maxfg + 1; 82 Inc (time); 83 if time = 5000 then break; // card time 84 end; 85 writeln (ANS); 86 closeit; 87 end.
Usaco 2014 Jan ski resort construction {gold Question 2}