Poj 1185 Artillery Position (Dynamic Planning)

Source: Internet
Author: User

Artillery positions
Time limit:2000 ms   Memory limit:65536 K
Total submissions:19152   Accepted:7417

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 4PHPPPPHHPPPPPHPPPHHP

Sample output

6

Source

Noi 01


Question:

The artillery has its own attack range. P indicates that the artillery can be deployed. The two artillery cannot attack each other and ask how many artillery can be placed at most.


Solution:

The state of the discretization artillery, which enumerates the valid state of the first layer, records the first two layers, then the maximum value of the third layer can be calculated, dynamic planning.


Solution code:

#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int maxn=70;int n,m,dp[110][maxn][maxn];char ch[110][20];vector <int> v;vector <int> cnt;void ini(){    for(int i=0;i<(1<<10);i++){        bool flag=true;        int cnt0=0;        for(int t=0;t<10;t++){            if( i&(1<<t) ){                cnt0++;                if( ( i&(1<<(t+1)) ) || ( i&(1<<(t+2)) ) ){                    flag=false;                    break;                }            }        }        if(flag){            cnt.push_back(cnt0);            v.push_back(i);        }    }}void input(){    memset(dp,0,sizeof(dp));    for(int i=1;i<=n;i++) scanf("%s",ch[i]);}bool conflict(int sum1,int sum2){    if(sum1&sum2) return true;    else return false;}bool isExist(int r,int sum){    for(int t=0;t<m;t++){        if( ( sum&(1<<t) ) && ch[r][t]=='H') return false;    }    return true;}void solve(){    int ans=0;    for(int i=1;i<=n;i++){        for(int y=0;y<v.size() && v[y]<(1<<m);y++){            if( !isExist(i,v[y]) ) continue;            for(int x=0;x<v.size() && v[x]<(1<<m);x++){                if( conflict(v[x],v[y]) ) continue;                for(int pre=0;pre<v.size() && v[pre]<(1<<m);pre++){                    if( conflict(v[y],v[pre]) ||  conflict(v[x],v[pre]) ) continue;                    dp[i][y][x]=max(dp[i][y][x],dp[i-1][x][pre]+cnt[y]);                    if(dp[i][y][x]>ans) ans=dp[i][y][x];                }            }        }    }    printf("%d\n",ans);}int main(){    ini();    while(scanf("%d%d",&n,&m)!=EOF){        input();        solve();    }    return 0;}




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.