日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3322 Bloxorz I(BFS)

發布時間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3322 Bloxorz I(BFS) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

要把x所在地方的盒子翻滾到O處,最少需要的滾動次數。

思路:

BFS 求最短路徑,問題的關鍵是如何對盒子翻轉時,移動狀態的一個設計,開了一個三維數組。

?

#include <iostream> #include <algorithm> #include <deque> using namespace std;struct ST {int x, y; // x, y 代表右下角的坐標位置int step, state; // state = 0 豎立, state = 1 水平, state = 2 豎直ST() {}ST(int _x, int _y, int _step, int _state) : x(_x), y(_y), step(_step), state(_state) {} };const int dir[3][4][3] = {{{2,0,2},{-1,0,2},{0,-1,1},{0,2,1}},{{0,-2,0},{0,1,0},{-1,0,1},{1,0,1}}, {{-2,0,0},{1,0,0},{0,1,2},{0,-1,2}}};char grid[510][510]; bool vis[510][510][3]; int row, col;bool judge(int x, int y, int state) {if (0 < x && x <= row && 0 < y && y <= col && grid[x][y] != '#') {if (state == 0 && grid[x][y] != 'E') {return true;}if (state == 1 && 1 < y && grid[x][y-1] != '#') {return true;}if (state == 2 && 1 < x && grid[x-1][y] != '#') {return true;}}return false; }int bfs(const ST& src, const ST& dst) {deque<ST> Q;Q.push_back(src);vis[src.x][src.y][src.state] = true;while (!Q.empty()) {ST u = Q.front();Q.pop_front();if (u.x == dst.x && u.y == dst.y && u.state == dst.state)return u.step;for (int i = 0; i < 4; i++) {int x = u.x + dir[u.state][i][0];int y = u.y + dir[u.state][i][1];int state = dir[u.state][i][2];if (!vis[x][y][state] && judge(x, y, state)) {vis[x][y][state] = true;Q.push_back(ST(x, y, u.step + 1, state));}}}return -1; }int main() {while (scanf("%d%d", &row, &col) && row && col) {ST src, dst;bool cflag = false;for (int i = 1; i <= row; i++) {scanf("%s", &grid[i][1]);for (int j = 1; j <= col; j++) {if (grid[i][j] == 'O') {dst.x = i, dst.y = j, dst.state = 0;} else if (grid[i][j] == 'X') {if (!cflag) {cflag = true;src.x = i, src.y = j, src.state = 0;} else {if (src.x == i) {src.y = max(src.y, j);src.state = 1;} else if (src.y == j) {src.x = max(src.x, i);src.state = 2;}}}vis[i][j][0] = vis[i][j][1] = vis[i][j][2] = false;}}src.step = 0;int ans = bfs(src, dst);if (ans == -1) printf("Impossible\n");elseprintf("%d\n", ans);}return 0; }

轉載于:https://www.cnblogs.com/kedebug/archive/2013/03/25/2981221.html

總結

以上是生活随笔為你收集整理的POJ 3322 Bloxorz I(BFS)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。