POJ1185 Artillery Positions

Source: Internet
Author: User

The main topic: In a n*m area, p is plain, h is mountainous. The plains can be put into artillery units, and the mountains cannot. Each artillery unit may be injured in a companion or in the same " -2,+2" range of friendlies. Under the premise of guaranteeing no injury, ask the area to be able to decorate the maximum number of artillery. N<=100,m<10

Analysis: processing by row. The state of each row can be processed with DFS and saved in the array.

F[i][j][k]=∑ (F[[i-1][k][p])

F[I][J][K] Indicates the maximum number of artillery that can be arranged when the state of line I is J and the previous row has a state of K.

J and K can not conflict, J and P also can not conflict, K and P can not conflict. f The initial value is 0.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <cstring>6 using namespacestd;7 #defineMAXN 1058 intF[maxn][maxn][maxn],n,m,state[maxn][maxn],row,cnt,cnts[maxn][maxn],nums[maxn],num;9 CharARR[MAXN][MAXN];Ten voidDfsintJintStaintCNT) One { A   if(j>m) -   { -state[row][num++]=STA; thecnts[row][num-1]=CNT; -nums[row]=num; -       return; -   } +     if((sta&3)==0&&arr[row][j]=='P') -DFS (j+1, (sta<<1)+1, cnt+1); +DFS (j+1, (sta<<1), CNT); A } at intMain () - { -scanf"%d%d",&n,&m); -   for(intI=1; i<=n;i++) -{scanf ("%s", arr[i]+1); -row=i; innum=0; -Dfs1,0,0); to   } +Memset (F,0,sizeoff); -    for(intI=0; i<nums[1];i++) thef[1][i][0]=cnts[1][i]; *nums[0]=1; $   for(intI=2; i<=n;i++)Panax Notoginseng     for(intj=0; j<nums[i];j++) -      for(intk=0; k<nums[i-1];k++) the         if((state[i][j]&state[i-1][k]) = =0) +         { A           for(intq=0; q<nums[i-2];q++) the         { +             if((state[i-1][k]&state[i-2][Q]) = =0&& (state[i][j]&state[i-2][Q]) = =0) -             if(f[i][j][k]<f[i-1][k][q]+Cnts[i][j]) $f[i][j][k]=f[i-1][k][q]+Cnts[i][j]; $         } -     } -     Long Longans=0; the      for(intI=0; i<nums[n];i++) -       for(intj=0; j<nums[n-1];j++)Wuyi       if(ans<F[n][i][j]) theans=F[n][i][j]; -printf"%i64d\n", ans);
View Code

POJ1185 Artillery Positions

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.