Codeforces 111C Petya and Spiders

Source: Internet
Author: User

An n * m chessboard. In the initial state, each grid has a spider. The spider can go up, down, and up or down, or stop in the same place. Ask, take a step, the maximum number of vacancies that can be generated on the board. Considering that n * m is small, there is a one-dimensional space <= 6, at this time, we should think that we can use status compression DP to solve dp [I] [j] [k] to indicate the first I rows, and the status of row I is j, the maximum number of vacancies that can be generated when the status of row I + 1 is k (excluding row I + 1, therefore, it is much easier to determine whether certain statuses can be transferred, you only need to determine whether the middle row can be restored to the initial state (that is, whether the current state can be changed from the initial state) refer to the [cpp] # include <cstdio> # include <cstring> # include <set> # include <string> # include <iostream> # include <cmath> # include <vector> # include <map> # include <Stack> # include <time. h >#include <queue> # include <cstdlib> # include <algorithm> using namespace std; # define lowbit (x) & (-(x ))) # define sqr (x) * (x) # define PB push_back # define MP make_pair # define foreach (it, x) for (typeof (x. begin () it = x. begin (); it! = X. end (); it ++) typedef unsigned long ULL; typedef long lld; typedef vector <int> VI; typedef vector <string> VS; typedef pair <int, int> PII; # define rep (I, n) for (int I = 0; I <n; I ++) # define For (I, a, B) for (int I = a; I <= B; I ++) # define CL (x) memset (x, 0, sizeof (x) # define CLX (x, y) memset (x, y, sizeof (x) template <class T> T two (T x) {return 1 <x ;} template <class T> void Min (T & x, T y) {if (y <x) x = y;} template <class T> void Max (T & x, T y) {if (y> x) x = y;} int dp [45] [1 <6] [1 <6], n, m; int full; inline int get (int x) {int cnt = 0; rep (I, m) if (x> I & 1) cnt ++; return m-cnt ;} bool check (int a, int B, int c) {int s = B | (B <1) | (B> 1) | a | c; return (s & (full-1) = (full-1);} int State [1 <6]; int main () {scanf ("% d", & n, & m); if (n <m) swap (n, m); www.2cto.com full = two (m ); rep (I, n + 1) rep (j, full) rep (k, full) dp [I] [j] [k] =-111111; rep (I, full) dp [0] [0] [I] = 0, State [I] = get (I); rep (I, n) rep (j, full) rep (k, full) rep (l, full) if (check (j, k, l) Max (dp [I + 1] [k] [l], dp [I] [j] [k] + State [k]); int ans = 0; rep (I, full) Max (ans, dp [n] [I] [0]); cout <ans <endl ;}

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.