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 line has a maximum of only 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 where each line can put the artillery. Regardless of the impact of the other rows of artillery and the mountain in the row, the whole state is enumerated. and record the number of artillery in each state.
The artillery was decentralized in the above situation. It is only necessary to consider whether the same artillery will be able to attack each other, only to infer whether the first position on his left has artillery and a second position on the left.

3. Proceed with DP, due to two of the factors affecting it. One is the state of the previous line, and one is the state of the last two lines, so set DP[ROW][I][J] as the first row, put the state as I, the row-1 line, and put the maximum number of artillery that can be placed in the condition of 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)

The Code of the Great God, written very specifically 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;}

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.