Zoj 2634 Collecting Stones

Source: Internet
Author: User

An 8x8 lattice subgraph, each of which has no more than 2000001 stones, goes from () to (). The passing lattice can no longer go and can only go up, down, right, in the upper right, lower right, and five directions, I asked if I could just collect M stones when I went to (8, 8. topic idea: no idea about dp. taking the brute-force and in-depth search route is definitely not feasible. try pruning. pruning 1: The current sum is greater than m and jumps out, taking other steps. I think so. pruning 2: All and smaller than m after the column where the current accumulate and + are located, jump out. pruning 2 is very important! Why? I don't know... code: [cpp] # include <stdlib. h> # include <string. h> # include <stdio. h> # include <ctype. h> # include <math. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <string> # include <iostream> # include <algorithm> using namespace std; # define ll long # define ls rt <1 # define rs ls | 1 # define lson l, mid, ls # define rson mid + 1, r, rs # define middle (l + r)> 1 # define eps (1e-8) # define clr_all (x, c) memset (x, c, sizeof (x) # define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1) # define MOD 1000000007 # define INF 0x3f3f3f3f # define PI (acos (-1.0 )) # define _ mod (x, y) (x)> 0? (X) % (y) :( (x) % (y) + (y) % (y) # define _ abs (x) <0? (-(X) :( x) # define getmin (x, y) (x = (x) <0 | (y) <(x ))? (Y) :( x) # define getmax (x, y) (x = (y)> (x ))? (Y) :( x) template <class T> void _ swap (T & x, T & y) {T = x; x = y; y = t ;} template <class T> T _ max (T x, T y) {return x> y? X: y;} template <class T> T _ min (T x, T y) {return x <y? X: y;} int TS, cas = 1; const int M = 1000 + 5; bool flag; int m, vis [11] [11], a [11] [11], dp [11]; int dir [5] [2] = }, {-}, {}; int tot; void dfs (int x, int y) {if (tot> m) return; if (x = y & x = 8) {if (tot = m) flag = true; return ;}for (int I = 0; I <5; I ++) {int xx = x + dir [I] [0], yy = y + dir [I] [1]; if (xx <1 | yy <1 | xx> 8 | yy> 8) continue; if (vis [xx] [yy] | tot + dp [y] <m) continue; vis [xx] [yy] = 1; tot + = a [x X] [yy]; dfs (xx, yy); if (flag) return; tot-= a [xx] [yy]; vis [xx] [yy] = 0 ;}} void run () {int I, j; scanf ("% d", & m); for (I = 1; I <= 8; I ++) for (j = 1; j <= 8; j ++) scanf ("% d ", & a [I] [j]); clr_all (dp, 0); for (I = 1; I <= 8; I ++) for (j = 1; j <= 8; j ++) dp [j] + = a [I] [j]; for (I = 7; I> 0; I --) dp [I] + = dp [I + 1]; clr_all (vis, 0); tot = a [1] [1], vis [1] [1] = 1; flag = 0, dfs (1, 1); printf ("% s \ n", flag? "Yes": "No");} void preSof () {} int main () {// freopen ("input.txt", "r", stdin ); // freopen ("output.txt", "w", stdout); preSof (); // run (); // while (~ Scanf ("% d", & n, & q) run (); for (scanf ("% d", & TS); cas <= TS; cas ++) run (); return 0 ;}

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.