{"id":82,"date":"2011-03-15T07:55:50","date_gmt":"2011-03-14T23:55:50","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=82"},"modified":"2012-03-03T19:36:25","modified_gmt":"2012-03-03T11:36:25","slug":"hdu-1689-eat-the-trees","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hdu-1689-eat-the-trees\/","title":{"rendered":"HDU 1689. Eat the Trees"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u591a\u6761\u56de\u8def\u95ee\u9898\uff0c\u7ed9\u5b9a\u4e00\u4e2a n \u00d7 m \u7684\u6709\u969c\u788d\u5730\u56fe\uff0c\u6c42\u7528\u82e5\u5e72\u6761\u56de\u8def\u8986\u76d6\u6240\u6709\u683c\u5b50\u7684\u6709\u591a\u5c11\u79cd\u65b9\u6848\u3002<br \/>\n\uff081 <= n, m <= 11 ...\uff09\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u8bbe\u8ba1\u72b6\u6001\uff0c\u8bd5\u8bd5\u770b\u80fd\u4e0d\u80fd\u9010\u884c\u8f6c\u79fb\uff1ff[i][s] \u8868\u793a\u524d i \u5c42\uff0c\u7b2c i \u5c42\u72b6\u6001\u4e3a s \u7684\u65b9\u6848\u6570\uff0cs \u7684\u6bcf\u4e00\u4f4d\u8868\u793a\u6b64\u5904\u662f\u5426\u6709\u63d2\u5934\uff0c\u4e14\u63d2\u5934\u4e00\u5b9a\u662f\u6210\u5bf9\u51fa\u73b0\uff0c\u662f\u5076\u6570\uff0c\u63a5\u4e0b\u6765\u5206\u6790\u72b6\u6001\u8f6c\u79fb\uff0c\u5148\u8003\u8651\u6ca1\u6709\u969c\u788d\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u4ee5 m = 4 \u7684\u4e00\u4e2a\u60c5\u51b5\u505a\u4f8b\u5b50\u3002<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u4f4d\u7f6e\u662f\u6709\u63d2\u5934\u7684\u3002\n<ul>\n<li>\u4e0d\u52a8\u3002<\/li>\n<li>\u5f00\u59cb\u5411\u53f3\u626b\u63cf\uff0c\u5982\u679c\u53f3\u4fa7\u7684\u4f4d\u7f6e\u662f\u4e00\u7ec4\u96f6\u96f6\u96f6\u96f6\uff0c\u90a3\u4e48\u63d2\u5934\u53f3\u79fb\u30020100\uff0c0010\uff0c0001 \u8fd9\u6837\u3002\u3002<\/li>\n<li>\u649e\u5899\u7684\u65f6\u4faf\u9047\u5230\u4e00\u4e2a\u540c\u4f34\uff0c\u90a3\u4e48\u540c\u5b83\u5408\u4f53\u3002\u4f8b\u5982 1001\uff0c\u5408\u4f53\u4ee5\u540e\u5f62\u6210 0000\u3002<\/li>\n<\/ul>\n<\/li>\n<p><bk><bk><\/p>\n<li>\u8fd9\u4e2a\u4f4d\u7f6e\u6ca1\u6709\u63d2\u5934\u3002\n<ul>\n<li>\u5411\u53f3\u626b\u63cf\uff0c\u5982\u679c\u53f3\u4fa7\u7684\u51e0\u4e2a\u4f4d\u7f6e\u4e5f\u4e00\u76f4\u662f\u4e00\u7ec4\u96f6\u96f6\u96f6\u96f6\uff0c\u90a3\u4e48\u5fc5\u987b\uff08\u4ee5\u786e\u4fdd\u5b8c\u5168\u8986\u76d6\uff09\u751f\u6210\u4e00\u7ec4\u65b0\u63d2\u5934\u30021100\uff0c1010\uff0c1001\u3002<\/li>\n<li>\u649e\u5899\u7684\u65f6\u4faf\u9047\u5230\u4e00\u4e2a\u63d2\u5934\uff0c\u90a3\u4e48\u9700\u8981\u628a\u8fd9\u4e2a\u63d2\u5934\u5411\u5de6\u5f15\u5411\u6b64\u5904\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><bk><br \/>\n\u603b\u7684\u6765\u8bf4\u4e00\u5171\u4e5f\u53ea\u6709\uff1a\u751f\u6210\u4e00\u7ec4\u65b0\u63d2\u5934\u3001\u63d2\u5934\u5411\u4e24\u8fb9\u79fb\u52a8\u6216\u8005\u4e0d\u53d8\u3001\u5408\u5e76\u4e24\u4e2a\u63d2\u5934\u8fd9\u4e09\u79cd\u60c5\u51b5\u800c\u5df2\u3002\u8fd9\u4e4b\u540e\u5728\u52a0\u4e0a\u5224\u65ad\u969c\u788d\u7684\u90e8\u5206\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<pre lang=\"cpp\" file=\"A.cpp\">\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <cstring>\r\nusing namespace std;\r\n\r\nconst int hh = 11, ss = 1 << hh;\r\nlong long f[2][ss], d;\r\nint L[ss], h, w, up;\r\nint s, o, p, q;\r\n\r\n\r\n#define _Pl (s &#038; (1 << l))\r\n#define _Pr (s &#038; (1 << r))\r\n\/\/ Check Plug.\r\n\r\n#define _Ol (o &#038; (1 << l))\r\n#define _Or (o &#038; (1 << r))\r\n\/\/ Check Obstacle...\r\n\r\n#define _Sl s ^= 1 << l\r\n#define _Sr s ^= 1 << r\r\n\/\/ Reset Plug ..\r\n\r\ninline int countBits(int x){\r\n    x = (x &#038; 0x00005555) + ((x &#038; 0x0000aaaa) >> 1);\r\n    x = (x & 0x00003333) + ((x & 0x0000cccc) >> 2);\r\n    x = (x & 0x00000f0f) + ((x & 0x0000f0f0) >> 4);\r\n    x = (x & 0x000000ff) + ((x & 0x0000ff00) >> 8);\r\n    return x;\r\n}\r\n\r\n\r\ninline bool isLegal(int x){\r\n    return (countBits(x) & 1) == 0;\r\n}\r\n\r\nvoid dfs(int l){\r\n\tif (l == w){\r\n\t\tf[p][s] += d;\r\n\t}\r\n\telse {\t\t\r\n\t\tint r = l + 1;\r\n\t\t\r\n\t\tif (_Ol){\r\n\t\t\tdfs(r);\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\tif (_Pl){\r\n\t\t\t\/\/ \u4f4d\u7f6e\u4e0d\u53d8..\r\n\t\t\tdfs(r);\r\n\t\t\t\r\n\t\t\t\/\/ \u63d2\u5934\u53f3\u79fb ..\r\n\t\t\t_Sl;\r\n\t\t\twhile (!_Pr && !_Or){\r\n\t\t\t\t_Sr; dfs(r+1); _Sr;\r\n\t\t\t\tr++;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ \u63d2\u5934\u5408\u5e76 ...\r\n\t\t\tif (_Pr){\r\n\t\t\t\t_Sr; dfs(r+1); _Sr;\r\n\t\t\t}\r\n\t\t\t_Sl;\r\n\t\t}\r\n\t\telse {\r\n\t\t\t_Sl;\r\n\t\t\t\/\/ \u751f\u6210\u4e00\u7ec4\u65b0\u63d2\u5934. ...\r\n\t\t\twhile (!_Pr && !_Or){\r\n\t\t\t\t_Sr; dfs(r+1); _Sr;\r\n\t\t\t\tr++;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ \u63d2\u5934\u5de6\u79fb ..\r\n\t\t\tif (_Pr){\r\n\t\t\t\t_Sr; dfs(r+1); _Sr;\r\n\t\t\t}\r\n\t\t\t_Sl;\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\nvoid solve(){\r\n\tf[p = 0][s = 0] = 1;\r\n\t\r\n\tint t;\r\n\tfor (int i = 0; i < h; i ++){\r\n\t\tq = p, p = 1 - p, o = 1;\r\n\t\tmemset(f[p], 0, sizeof(f[p]));\r\n\t\t\r\n\t\tfor (int j = 0; j < w; j++)\r\n\t\t\tscanf(\"%d\", &#038;t), o = (o << 1)  + (t == 0); \r\n\t\t\r\n\t\tfor (int ii = 0; ii < up; ii ++){\r\n\t\t\ts = L[ii];\r\n\t\t\tif (f[q][s] &#038;&#038;  !(s &#038; o))\r\n\t\t\t\td = f[q][s], dfs(0);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nvoid init(){\r\n    scanf(\"%d%d\", &#038;h, &#038;w); \r\n    \r\n\tint t = 1 << w; up = 0;\r\n    for (int i = 0; i < t; i++)\r\n        if (isLegal(i)) L[up++] = i, \r\n\tmemset(f, 0, sizeof(f));\r\n}\r\n\r\n\r\nint main(){        \r\n    freopen(\"in.txt\", \"r\", stdin);\r\n    int T; cin >> T;\r\n    for (int i=1;i<=T;i++){\r\n        init();  solve();\r\n        printf(\"Case %d: There are %lld ways to eat the trees.\\n\", i, f[p][0]);\r\n        \/\/printf(\"Case %d: There are %I64d ways to eat the trees.\\n\", i, f[p][0]);\r\n    }\r\n}\r\n<\/pre>\n<pre lang=\"cpp\" file=\"B.cpp\">\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <cstring>\r\nusing namespace std;\r\n\r\nconst int hh = 11, ss = 1 << hh, MaxH = ss \/ 2;\r\nbool O[hh+1][hh+1], lt, up; int h, w, l, s;\r\nlong long ans, d;\r\n\r\n\r\nstruct hashTable {\r\n\tint state[MaxH], head[MaxH], next[MaxH], sz;\r\n\tlong long key[MaxH];\r\n\t\r\n\tinline void clear() {\r\n\t\tsz = 0;\r\n\t\tmemset(head, -1, sizeof(head));\r\n\t}\r\n\tinline void insert(int s) {\r\n\t\tint x = s % MaxH;\r\n\t\tfor ( int it = head[x] ; it != -1 ; it = next[it] ) {\r\n\t\t\tif ( state[it] == s ) {\r\n\t\t\t\tkey[it] += d;\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t}\r\n\t\tstate[sz] = s, key[sz] = d;\r\n\t\tnext[sz] = head[x];\r\n\t\thead[x] = sz++;\r\n\t}\r\n\tinline long long search(int s){\r\n\t\tint x = s % MaxH;\r\n\t\tfor ( int it = head[x] ; it != -1 ; it = next[it])\r\n\t\t\tif ( state[it] == s ) return key[it];\r\n\t\treturn 0;\r\n\t}\r\n} H[2] , *src , *des;\r\n\r\n\r\n\r\nvoid patch(){\r\n\tfor (int i=0;i<des->sz;i++)\r\n\t\tcout << des->state[i] << \",\" << des->key[i] << \" \";\r\n\tcout << endl;\r\n}\r\n\r\n\r\nvoid solve(){\r\n\tsrc = H, des = src + 1, d = 1;\r\n\tdes->clear(), des->insert(0);\r\n\t\r\n\tfor (int i = 0; i < h; i ++){\r\n\t\t\r\n\t\t\r\n\t\tfor (int j = 0; j < src->sz; j ++)\r\n\t\t\tdes->state[j] <<= 1;\r\n\t\t\r\n\t\tfor (int j = 0; j < w; j ++){\r\n\t\t\t\r\n\t\t\tif (O[i][j]) continue;\r\n\t\t\t\r\n\t\t\tswap(src, des), des->clear();\t\t\t\r\n\t\t\t\r\n\t\t\tfor (int k = 0; k < src->sz; k ++){\r\n\t\t\t\ts = src->state[k], d = src->key[k];\r\n\t\t\t\tlt = s & (1 << j), up = s &#038; (1 << (j+1));\r\n\t\t\t\t\r\n\t\t\t\t\/\/if (lt != up) des->insert(s, d);\r\n\t\t\t\t\/\/des->insert(s ^ (3 << j), d);\r\n\t\t\t\t\r\n\t\t\t\tif (!lt &#038;&#038; !up){\r\n\t\t\t\t\tif (!O[i+1][j] &#038;&#038; !O[i][j+1]) des->insert(s ^ (3 << j));\r\n\t\t\t\t}\r\n\t\t\t\telse if (lt &#038;&#038; up){\r\n\t\t\t\t\tdes->insert(s ^ (3 << j));\r\n\t\t\t\t}\r\n\t\t\t\telse {\r\n\t\t\t\t\tif (lt){\r\n\t\t\t\t\t\tif (!O[i+1][j]) des->insert(s);\r\n\t\t\t\t\t\tif (!O[i][j+1]) des->insert(s ^ (3 << j));\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\tif (!O[i][j+1]) des->insert(s);\r\n\t\t\t\t\t\tif (!O[i+1][j]) des->insert(s ^ (3 << j));\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/patch();\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t}\r\n\t\r\n\tans = des->search(0);\r\n}\r\n\r\n\r\nvoid init(){\r\n    scanf(\"%d%d\", &h, &w); \r\n\tl = (1 << (w+1)) - 1;\r\n\t\r\n\tint t;\r\n\tfor (int i=0;i<h;i++){\r\n\t\tfor (int j=0;j<w;j++)\r\n\t\t\tscanf(\"%d\", &#038;t), O[i][j] = (t == 0);\r\n\t}\r\n\tfor (int i=0;i<h;i++)\r\n\t\tO[h][i] = O[i][w] = true;\r\n}\r\n\r\n\r\nint main(){        \r\n    \/\/freopen(\"in.txt\", \"r\", stdin);\r\n    int T; cin >> T;\r\n    for (int i=1;i<=T;i++){\r\n        init();  solve();\r\n        \/\/printf(\"Case %d: There are %lld ways to eat the trees.\\n\", i, ans);\r\n        printf(\"Case %d: There are %I64d ways to eat the trees.\\n\", i, ans);\r\n    }\r\n}\r\n<\/pre>\n<p><bk><bk><bk><bk><br \/>\n<bk><bk><bk><bk><\/p>\n<h3>Further discussion :<\/h3>\n<p>\u7b2c 90 \u884c\u9644\u8fd1\uff0c\u969c\u788d\u4e00\u5f00\u59cb\u7684\u65f6\u4faf\u8d4b\u503c1\uff0c\u8fd9\u6837\u5f80\u540e\u5de6\u79fb\u7684\u8fc7\u7a0b\u53ef\u4ee5\u5f62\u6210\u4e00\u5835\u5899\uff0c\u8fd9\u6837 DFS \u91cc\u9762\u53ef\u4ee5\u7701\u7565\u8bb8\u591a\u5224\u65ad r < w \u53e5\u5b50\u3002\n15 ms \u8fd8\u53ef\u4ee5\u7684\u6837\u5b50... \u55ef\u55ef\u3002\n\n\n\n\n<h3>External link :<\/h3>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=1693\">http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=1693<\/a><br \/>\n<a href=\"http:\/\/blog.huang-wei.com\/2008\/08\/24\/hdu-1693-eat-the-trees\/\">http:\/\/blog.huang-wei.com\/2008\/08\/24\/hdu-1693-eat-the-trees\/<\/a><br \/>\n<a href=\"http:\/\/hi.baidu.com\/fqq11679\/blog\/item\/423bcd4a3d956bf983025c6d.html\">http:\/\/hi.baidu.com\/fqq11679\/blog\/item\/423bcd4a3d956bf983025c6d.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u591a\u6761\u56de\u8def\u95ee\u9898\uff0c\u7ed9\u5b9a\u4e00\u4e2a n \u00d7 m \u7684\u6709\u969c\u788d\u5730\u56fe\uff0c\u6c42\u7528\u82e5\u5e72\u6761\u56de\u8def\u8986\u76d6\u6240\u6709\u683c\u5b50\u7684\u6709\u591a\u5c11\u79cd\u65b9\u6848\u3002 \uff081<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1k","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}