HDU 5025 Saving Tang Monk(廣州網路賽D題)

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   sp   on   

HDU 5025 Saving Tang Monk

題目連結

思路:記憶化廣搜,vis[x][y][k][s]表示在x, y結點,有k把鑰匙了,蛇剩餘狀態為s的步數,先把圖預先處理出來,然後進行廣搜即可

代碼:

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int INF = 0x3f3f3f3f;const int N = 105;const int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int n, m, g[N][N], sn, vis[N][N][10][35];char str[N];struct State {int x, y, k, s;State() {}State(int x, int y, int k, int s) {this->x = x;this->y = y;this->k = k;this->s = s;}int get() {return vis[x][y][k][s];}} s, e;bool solve() {queue<State> Q;Q.push(s);memset(vis, INF, sizeof(vis));vis[s.x][s.y][0][0] = 0;while (!Q.empty()) {State u = Q.front();Q.pop();for (int i = 0; i < 4; i++) {State v = u;int ti = u.get();int x = u.x + d[i][0];int y = u.y + d[i][1];if (x < 0 || x >= n || y < 0 || y >= n) continue;if (g[x][y] == -10) continue;else if (g[x][y] < 0) {int nu = -g[x][y] - 1;v.x = x; v.y = y;if (v.s&(1<<nu))ti++;else {v.s |= (1<<nu);ti += 2;}} else {v.x = x; v.y = y;if (g[x][y] == v.k + 1)v.k = g[x][y];ti++;}if (vis[v.x][v.y][v.k][v.s] > ti) {vis[v.x][v.y][v.k][v.s] = ti;Q.push(v);}}}int ans = INF;for (int i = 0; i < 32; i++)ans = min(vis[e.x][e.y][e.k][i], ans);if (ans == INF) return false;printf("%d\n", ans);return true;}int main() {while (~scanf("%d%d", &n, &m) && n || m) {sn = 0;for (int i = 0; i < n; i++) {scanf("%s", str);for (int j = 0; j < n; j++) {if (str[j] == 'K') {g[i][j] = 0;s = State(i, j, 0, 0);}if (str[j] == 'T') {g[i][j] = 0;e = State(i, j, m, 0);}if (str[j] >= '1' && str[j] <= '9')g[i][j] = str[j] - '0';if (str[j] == '#')g[i][j] = -10;if (str[j] == '.')g[i][j] = 0;if (str[j] == 'S')g[i][j] = (--sn);}}if (!solve()) printf("impossible\n");}return 0;}


HDU 5025 Saving Tang Monk(廣州網路賽D題)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.