POJ 1185 Artillery Positions

Source: Internet
Author: User

Test instructions: Chinese question ...

Solution: Pressure DP. The state of a row is represented by a binary number, 1 means a person, 0 means no one, and a dp[i][j][k] indicates that the number of people with the first row state is J, and the I-1 row state is K, then there is a state transition equation when the state enumeration state JKL represents the first line I, the I-1 row, the I-2 row state respectively, When JKL is compatible with each other, dp[i][j][k] = max (Dp[i][j][k], dp[i-1][k][l] + num[j]), Num[i] represents the number of people in state I, there are 210 states, so the complexity is nx210x210x 210, is not acceptable, but because the distance between people in each line is at least 2, so the legal state only 60, can be deep search, so the complexity becomes nx60x60x60. Status I and J compatible conditions are i&j equals 0, that is, no two soldiers stand on the same column, you can compress the state of the map in the same way.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #include <iomanip> #define LL Long Long#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1using namespace Std;char maze[110][15];int state[100];int tot = 0;int num[60];void dfs (int now, int pos, int ni) {num[    Tot] = ni;    state[tot++] = now;        for (int i = pos + 3; i < ten; i++) {int tmp = 1 << i;    DFS (now | tmp, I, ni + 1);    }}int dp[105][60][60];int M[105];int Main () {DFS (0,-3, 0);    int n, m;        while (~SCANF ("%d%d", &n, &m)) {memset (m, 0, sizeof m);            for (int i = 1; i < n + 1; i++) {scanf ("%s", Maze[i]);               for (int j = 0; J <; J + +) { M[i] <<= 1;            if (maze[i][j]! = ' P ') ++m[i];        }} memset (DP, 0, sizeof DP);  for (int i = 1, i < n + 1; i++) {for (int j = 0; J < + j + +) {for (int k = 0; K < 60; k++) {for (int l = 0; l <; l++) {if (STA                        TE[L] & M[i]) continue;                        if (State[j] & State[k]) continue;                        if (State[j] & State[l]) continue;                        if (State[k] & State[l]) continue;                    Dp[i][l][k] = max (Dp[i][l][k], dp[i-1][k][j] + num[l]);        }}}} int ans = 0;        for (int i = 0; i <; i++) for (int j = 0; J <; J + +) ans = max (dp[n][i][j], ans);    printf ("%d\n", ans); } return 0;}

  

POJ 1185 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.