{"id":131,"date":"2010-08-07T08:38:03","date_gmt":"2010-08-07T00:38:03","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=131"},"modified":"2012-03-03T19:36:08","modified_gmt":"2012-03-03T11:36:08","slug":"summer-contest-ix-%e6%90%9c%e7%b4%a2%e7%bb%83%e4%b9%a0-round-b","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/summer-contest-ix-%e6%90%9c%e7%b4%a2%e7%bb%83%e4%b9%a0-round-b\/","title":{"rendered":"Summer Contest IX. \u641c\u7d22\u7ec3\u4e60 Round B"},"content":{"rendered":"<h3>Background :<\/h3>\n<p>\u53ea\u505a\u51fa\u4e00\u9053Problems C\uff0cAstar (\u4e2d\u95f4\u51fa\u73b0\u70b9\u6545\u969c\u3002\u3002\u5bfc\u81f4\u4ea4\u4e86\u8fd9\u4e48\u591a\u6b21 \u256f\ufe4f\u2570)<br \/>\n\u8bfb\u9898\uff0c\u524d\u4e24\u9053\u90fd\u662f\u6211\u505a\u8fc7\u4f46\u505a\u4e0d\u4e0a\u6765\u7684\u3002\u3002\u3002\uff08\u7b2c\u4e00\u9898\u662f\u53bb\u5e74NOIP\u6302\u6389\u7684\u6570\u72ec\u9898\uff0c\u7b2c\u4e8c\u9898\u662f\u6211USACO\u5361\u4f4f\u7684\u9898\u3002\u3002\u3002\uff09<br \/>\n\u4e8e\u662f\u5fc3\u60f3\uff0c\u8fd9\u4e0b\u53ea\u6709\u6b7b\u78d5C\u4e86\u3002\u3002\u3002\u4e8e\u662f\u5f00\u59cb\u6b7b\u78d5C\u3002\u3002\u3002<\/p>\n<p>\u8003\u8651\u5230\u53ef\u80fd\u6709\u4eba\u4f1a\u9700\u8981\uff0c\u4ee3\u7801\u8d34\u8fd9\u91cc\u3002\u3002\u3002<br \/>\n<!--more--><\/p>\n<pre lang=\"cpp\" file=\"C.cpp\">\r\n#include <iostream>\r\n#include <cstring>\r\n#include <queue>\r\n\r\nusing namespace std;\r\nconst int dx[4] = {0,0,1,-1};\r\nconst int dy[4] = {1,-1,0,0};\r\n\r\nstruct state{\r\n    int s, l;\r\n    int g, h, f;\r\n    friend bool operator <(state a, state b){\r\n        return a.f>b.f;\r\n    }\r\n};\r\nstate u, v;\r\n\r\n\r\nbool hash[1679617];\r\npriority_queue<state> Q;\r\nchar map[8][8]; int hh[7][7];\r\nint px[4], py[4], ux, uy, vx, vy;\r\nint x0, y0; int ans;\r\nint n, m, h0;\r\n\r\n\r\nint qx[8*8], qy[8*8];\r\nint head, tail;\r\n\r\n\r\nint encode(){\r\n    int s = 0;\r\n    for (int i=0;i<m;i++)\r\n        s = (s * 36) + (px[i]-1) * 6 + (py[i]-1);\r\n    return s;\r\n}\r\nvoid decode(int s){\r\n    for (int i=m-1;i>=0;i--){\r\n        py[i] = (s%6) + 1; s \/= 6;\r\n        px[i] = (s%6) + 1; s \/= 6;\r\n    }\r\n}\r\n\r\n\r\n\r\nvoid init(){\r\n    cin >> n >> m;\r\n\r\n    memset(map, 'X', sizeof(map));\r\n    for (int i=1;i<=n;i++)\r\n        for (int j=1;j<=n;j++){\r\n            cin >> map[i][j];\r\n            if (map[i][j]=='O') x0 = i, y0 = j;\r\n            else if (map[i][j]!='.'&&map[i][j]!='X') px[int(map[i][j])-49] = i, py[int(map[i][j])-49] = j ,map[i][j]='.';\r\n        }\r\n\r\n    v.s = encode();\r\n    memset(hh, -1, sizeof(hh));\r\n    qx[0] = x0; qy[0] = y0;\r\n\r\n    head = 0; tail = 1;  hh[x0][y0] = 0; map[x0][y0] = '.';\r\n\r\n    int x, y, xx, yy;\r\n    while (head<tail){\r\n        x = qx[head]; y = qy[head];\r\n        for (int i=0;i<4;i++){\r\n            xx = x + dx[i]; yy = y + dy[i];\r\n            if (map[xx][yy]=='X'||hh[xx][yy]!=-1) continue;\r\n            hh[xx][yy] = hh[x][y] + 1; qx[tail] = xx; qy[tail] = yy;\r\n            tail++;\r\n        }\r\n        head++;\r\n    }\r\n\r\n    v.h = 0;\r\n    for (int i=0;i<m;i++)\r\n        v.h += hh[px[i]][py language=\"[i\"][\/py]];\r\n    v.g = 0; v.f = v.g + v.h; v.l = 0;\r\n    memset(hash, false, sizeof(hash));\r\n    while (!Q.empty()) Q.pop();\r\n    Q.push(v);\r\n\r\n}\r\n\r\n\r\n\r\n\r\nvoid patch(){\r\n    cout << endl;\r\n    for (int i=1;i<=n;i++){\r\n        for (int j=1;j<=n;j++){\r\n            if (i==x0&#038;&#038;j==y0) { cout << \"O\"; goto a;}\r\n            for (int k=0;k<m;k++)\r\n                if (i==px[k]&#038;&#038;j==py[k]) {cout << k+1; goto a;}\r\n            cout << map[i][j];\r\n            a:;\r\n        }\r\n        cout << endl;\r\n    }\r\n    cout << endl;\r\n    int t; cin >> t;\r\n}\r\n\r\n\r\n\r\n\r\nbool crash(){\r\n    if (map[vx][vy]=='X') return true;\r\n    for (int i=u.l;i<m;i++)\r\n        if (vx==px[i]&#038;&#038;vy==py[i]) return true;\r\n    return false;\r\n}\r\n\r\n\r\nvoid solve(){\r\n    ans = 0;\r\n    while (true){\r\n        u = Q.top(); Q.pop(); if (u.l==m) {ans = u.g; break;}\r\n        if (hash[u.s]==true) continue; hash[u.s] = true;\r\n        decode(u.s);  \/\/patch();\r\n\r\n        int i, j;\r\n        for (i=u.l;i<m;i++){\r\n            ux = px[i]; uy = py[i];\r\n            for (j=0;j<4;j++){\r\n                vx = ux + dx[j]; vy = uy + dy[j];\r\n                if (crash()) continue;\r\n                px[i] = vx; py[i] = vy; v.s = encode(); v.l = u.l;\r\n                if (vx == x0 &#038;&#038; vy == y0 &#038;&#038; i == u.l) v.l++;\r\n                v.g = u.g + 1; v.h = u.h - hh[ux][uy] + hh[vx][vy];\r\n                v.f = v.g + v.h;\r\n                Q.push(v);\r\n            }\r\n            px[i] = ux; py[i] = uy;\r\n        }\r\n    }\r\n}\r\n\r\n\r\nint main (){\r\n\r\n    int T; cin >> T;\r\n    for (int i=0;i<T;i++){\r\n        init(); solve(); \/\/ patch();\r\n        cout << ans << endl;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Background : \u53ea\u505a\u51fa\u4e00\u9053Problems C\uff0cAstar (\u4e2d\u95f4\u51fa\u73b0\u70b9\u6545\u969c\u3002\u3002\u5bfc\u81f4\u4ea4\u4e86\u8fd9\u4e48\u591a\u6b21 \u256f\ufe4f\u2570) \u8bfb\u9898\uff0c\u524d\u4e24\u9053\u90fd\u662f\u6211\u505a\u8fc7\u4f46\u505a\u4e0d\u4e0a\u6765\u7684\u3002\u3002\u3002\uff08\u7b2c\u4e00\u9898\u662f\u53bb\u5e74NOIP\u6302\u6389\u7684\u6570\u72ec\u9898\uff0c\u7b2c\u4e8c\u9898\u662f\u6211USACO\u5361\u4f4f\u7684\u9898\u3002\u3002\u3002\uff09 \u4e8e\u662f\u5fc3\u60f3\uff0c\u8fd9\u4e0b\u53ea\u6709\u6b7b\u78d5C\u4e86\u3002\u3002\u3002\u4e8e\u662f\u5f00\u59cb\u6b7b\u78d5C\u3002\u3002\u3002 \u8003\u8651\u5230\u53ef\u80fd\u6709\u4eba\u4f1a\u9700\u8981\uff0c\u4ee3\u7801\u8d34\u8fd9\u91cc\u3002\u3002\u3002<\/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-131","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-27","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/131","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=131"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}