You are not logged in. Please login at www.codechef.com to post your questions!

×

CIRCLE - Editorial

 3 Problem link : contest practice Difficulty : Easy-Medium Pre-requisites : DP, Flood-fill Problem : Determine, whether it's possible choose a cell in such a way that all the normal minions would not be trapped (will be free) after the antidote usage at that cell. Explanation : At first, let's find all the connected components of normal minions in the given matrix. Every connected component should should contain the maximal number of minions, that is, there should not be any possibility to extend each of the components with a normal minion. Then, if we take any component, all the minions from it will be either free or not. We can use a flood-fill for the components search. For every component we can find the leftmost and the rightmost column and the topmost and the bottommost row. Having that found, it's easy to determine, whether the correspondent group of minions trapped or not. It it is not trapped, we don't have to do anything in order to rescue them, so we can just ignore this component. Let's see what happens else. Formally, we need to place a "cross" in the matrix, so we can consider a bounding rectangle of this area (i.e. a connected component of trapped minions). If the cross intersects the bounding rectangle then group will be free after placing the cross with the center in this cell. Otherwise, it will not, so the cell will not be applicable for us. Consider the bounding rectangle of some connected component. Let its bounding rectangle bottom-left and upper-right coordinates be (X1, Y1) and (X2, Y2). The cross with the center at (X, Y) will not cross this rectangle if at least one of the following conditions will be held: X2 < X, Y2 < Y. That means that the figure lies completely in the upper-left quadrant relatively (X, Y). X1 > X, Y2 < Y. That means that the figure lies completely in the upper-right quadrant relatively (X, Y). X2 < X, Y1 > Y. That means that the figure lies completely in the bottom-left quadrant relatively (X, Y). X1 > X, Y1 > Y. That means that the figure lies completely in the bottom-right quadrant relatively (X, Y). We can check all the above statements for all the rectangles at once via prefix sums on a rectangle, that is a pretty popular technique. We can have four prefix-sums arrays for the different cases, that will make the solution easier. So, summing it all up again, we get the following solution: Using flood fill, find all the connected components of the trapped minions. For each of the components find it's bounding rectangle. Mark the corners of the bounding rectangles in their respective rectangle prefix sums. Iterate through all the cells of a matrix and check every cell with the usage of our previously built prefix sums. Each check can be performed in O(1) time, so we get overall complexity of O(N2). Solutions: Setter Tester This question is marked "community wiki". asked 27 Apr '14, 14:00 719●43●63●77 accept rate: 0% How can prefix sums be used on a rectangle? (22 May '14, 03:31)

 0 I have one small comment about the solution, I think the solution does not handle the edge case where the line just touches the boundary of the rectangle (Which should also free the trapped minions) Example: 1 7 5 BBBBB BBWBB BBBBB BBBWB BBBBB BWBBB BBBBB  Here the current author code gives a output of "NO" however it should be possible to get all the trapped minions out. In the author solution, we can consider a bigger rectangle during flood fill to handle the above mentioned edge case: // top-left, bottom-right ppi flood_fill(int i,int j){ if(i < 0 or j < 0 or i >= n or j >= m or mp[i][j] == 'B' or vis[i][j]) return ppi(pii(inf,inf),pii(-inf,-inf)); vis[i][j]=true; #ifdef AUTHOR_VERSION ppi ret=ppi(pii(i,j),pii(i,j)); #else ppi ret=ppi(pii(i - 1,j - 1),pii(i + 1,j + 1)); #endif ret=largest(ret,flood_fill(i+1,j)); ret=largest(ret,flood_fill(i-1,j)); ret=largest(ret,flood_fill(i,j+1)); ret=largest(ret,flood_fill(i,j-1)); return ret; }  You can find the updated author's solution here: https://www.codechef.com/viewsolution/15580376 answered 02 Oct '17, 00:42 3★doi_mach 1●1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,852
×2,212
×1,722
×16
×6

question asked: 27 Apr '14, 14:00

question was seen: 2,742 times

last updated: 02 Oct '17, 00:43