Artillery Position Problem Solving report

Source: Internet
Author: User
Tags bitset
Artillery Position Problem Solving report

Description
The generals of the Headquarters intend to deploy their artillery troops on the grid map of N * m. A n * m map consists of N rows and M columns. Each grid of the map may be a mountain (represented by "H") or a plain (represented by "p ), for example. A maximum of one artillery unit can be deployed on each plain terrain (the artillery unit cannot be deployed on the mountain). The black area of an artillery unit in the attack range on the map is shown:

If an artillery force is deployed on the gray plain marked by the map, the Black Grid in the figure indicates the area to which it can attack: two grids along the horizontal left and right, the two cells are vertical and vertical. No other white mesh attacks can be found on the graph. The artillery attack range is not affected by the terrain.
Now, the generals plan how to deploy artillery troops to prevent accidental injuries (ensure that no two artillery troops can attack each other, that is, no artillery force is within the attack scope of other Artillery Forces.) the maximum number of Artillery Troops in our army can be placed in the whole map area.

Input
The first line contains two positive integers separated by spaces, representing N and m respectively;
In the next n rows, each row contains M consecutive characters ('P' or 'H') with no spaces in the middle. Represent the data of each row in the map in order. N <= 100; m <= 10.

Output
Only one row contains an integer k, indicating the maximum number of artillery troops that can be placed.

Sample Input
5 4
Phpp
Pphh
PPPP
Phpp
Phhp

Sample output
6

Ideas:
I heard that it was done by compressing DP in the classic State. Even if I heard that someone has used the search, I decided to try dp (but it is quite similar to searching ).
The solution is roughly like this. Assume that the map exists in the map array. For map [I] [J], if it is P, there can be two solutions: Release and not release, however, there are multiple Placement Solutions for all P prior to position I and J. We try to combine these two solutions with all the solutions before position I and J, pij has many other solutions.

That is, take status [I] = {solution set for the first p from the top left} (P in the IJ position). The dynamic equation can be expressed:
Status [I] = status [I-1] fill {x ε status [I-1] | valid (X & PI)} X & Pi is to put PI in the X scheme, valid is used to verify the validity of the solution.

The valid function can use the following constraints:
In the current scheme, there are no soldiers in the position of PI or they may be attacked up or down.
In the current scheme, there are no soldiers on the left side of PI and no soldiers on the right side, that is, there is no possibility of both sides being attacked.

It is not enough because it is beyond the scope required by the question in terms of space and time.

To solve the space problem, we can compress the row. I use a 4-digit representation to compress the state:
Encode (INT [] solution, int length ){
Int result = solution [0] * 1 + solution [1] * (4)... + solution [Radix-1] * (4 ^ (length-1 ))
}
Similar to decode.

For example, phpp can have the following States for the third P:
3000, 0030, 0000, 3003, 0003

When entering the motion rule of the next row, you need to subtract one from each non-zero number. Based on the above example, after passing through a row, you will get:
2000, 0020, 0000, 2002, 0002

There are several locations in this status, indicating that a soldier can attack in the vertical direction. The horizontal attack can directly determine whether the two on the left are 3. Because the DP order is from left to right, from top to bottom. Therefore, only two directions rather than four directions are required for each soldier's attack.

To avoid wasting a lot of space, according to the dynamic equation, we just need to keep the status [I-1] of the State set on the line, then update status [I] to status [I-1], continue to solve. So I take all the status sets of the bitset record status [I] (because the status can be compressed into an int value ), in addition, I use an index array to record the current optimal value of a certain State.
Take phpp For example: 3000 can be compressed to 3 (in turn, it can be compressed as long as decompression and compression are symmetric), its solution [3] = compression is 48, solution [48] = 195 compressed to 195, solution [] = 2. Maintain the status set of each row in the future. Remember to keep the maximum value of solution [X.
For update status [I], note that the bitset used for status [I] is status [I-1], do not use the bitset of status [I] being modified, you can use a bitset backup to save data.

Through the above processing, we can basically ensure that the space does not time out, but the time problem still exists. As the bitset of status [I] is constantly expanding, every update of status [I + 1] is becoming more and more troublesome. In order to control the bitset size, you need to cut some States that cannot produce the optimal solution, just like the scissors in the search.
The biggest number of soldiers in a single row is (INT) (m + 2)/3), so for a certain state, if the solution value + the maximum number of soldiers placed in a single row <current optimal solution, you can ignore this solution. Because our status records the status of a row, the maximum number of soldiers in the downlink cannot reach the optimal solution for a certain status in line I, then there is no need to keep the status to the downstream, so you can skip it.

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.