POJ-1185 Artillery positions (state compression)

Source: Internet
Author: User

The main topic: The Chinese topic is not much to say the careless

Problem Solving Ideas:
1. Each row has a maximum of 10 locations, and not the mountain is the plain, then you can use 1 for mountain, 0 for the plains, the state of each row is compressed

2. Then find out the status of each line of artillery, regardless of the impact of the other rows on the artillery and the mountain of the row, to enumerate all the states and record the number of artillery in each state.
In the above case, only need to consider whether the artillery will attack each other, it is necessary to determine whether his left first position on the artillery and the left second position whether there is artillery can be

3. Then DP, because there are two factors, one is the state of the previous line, and the other is the state of the last two lines, so set DP[ROW][I][J] to the first row, place the state as I, line row-1, and put the maximum number of artillery in case the state is J.
The transfer equation can be obtained in the first two rows without affecting the row placement of the
DP[ROW][I][J] = max (Dp[row][i][j], dp[row-1][j][k] + i The number of artillery that can be placed in the state)

Reference to the code of the Great God, written very detailed here to write the link content

#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;#define MAXN#define MAXM#define MAXSintR, C, CNT;Charstr[ the];intROW[MAXN], NUM[MAXN], STATU[MAXN];intDP[MAXN][MAXS][MAXS];voidInput () {memset(Row,0,sizeof(row)); for(inti =0; i < R; i++) {scanf('%s ', str); for(intj =0; J < C; J + +)if(Str[j] = =' H ') Row[i] + = (1<< j); }}voidInit_statu () {memset(Num,0,sizeof(num)); CNT =0; for(inti =0; I < (1<< C); i++) {if((I & (i <<1)) || (I & (i <<2)))Continue;intt = i; while(t) {num[cnt] + = (T &1); T >>=1;    } statu[cnt++] = i; }}voidINIT_DP () {memset(DP,0,sizeof(DP)); for(inti =0; I < CNT; i++) {if(Statu[i] & row[0])Continue; dp[0][i][0] = Num[i]; } for(inti =0; I < CNT; i++) {if(Statu[i] & row[1])Continue; for(intj =0; J < CNT; J + +) {if(Statu[j] & row[0])Continue;if(Statu[i] & Statu[j])Continue; dp[1][I][J] = max (dp[1][I][J], dp[0][j][0] + num[i]); }    }}intSolve () { for(intR =2; R < R; r++) { for(inti =0; I < CNT; i++) {if(Statu[i] & Row[r])Continue; for(intj =0; J < CNT; J + +) {if(Statu[j] & Row[r-1])Continue;if(Statu[i] & Statu[j])Continue; for(intK =0; K < CNT; k++) {if(Statu[k] & Statu[j])Continue;if(Statu[k] & Statu[i])Continue;if(Statu[k] & Row[r-2])Continue; DP[R][I][J] = max (Dp[r][i][j], dp[r-1][J][K] + num[i]); }            }        }    }intAns =0; for(inti =0; I < CNT; i++) for(intj =0; J < CNT; J + +) ans = max (ans, dp[r-1][I][J]);returnAns;}intMain () { while(scanf("%d%d", &r, &c)! = EOF) {input ();        Init_statu (); INIT_DP ();printf("%d\n", solve ()); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ-1185 Artillery positions (state compression)

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.