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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU 1254 推箱子 BFS

發布時間:2025/5/22 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1254 推箱子 BFS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

囧,一開始沒有管人的情況,只看箱子,果然SB了。

#include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <bitset> #include <list> #include <cstdlib> #include <climits> #include <cmath> #include <ctime> #include <algorithm> #include <stack> #include <sstream> #include <numeric> #include <fstream> #include <functional>using namespace std;#define MP make_pair #define PB push_back typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef pair<int,int> pii; const int INF = INT_MAX / 3; const double eps = 1e-8; const LL LINF = 1e17; const double DINF = 1e60; const int maxn = 10; const int dx[] = {-1,1,0,0}; const int dy[] = {0,0,-1,1};struct Node {int x,y,pre;Node(int x,int y,int pre = -1): x(x), y(y), pre(pre) {} };int n,m,mp[maxn][maxn],sx,sy,ex,ey,tmp[maxn][maxn]; int dist[maxn][maxn][4],px,py; bool vis[maxn][maxn];bool bfs1(int x1,int y1,int x2,int y2) {if(tmp[x1][y1] != 0 || tmp[x2][y2] != 0) return false;queue<int> qx,qy;qx.push(x1); qy.push(y1);memset(vis,0,sizeof(vis));vis[x1][y1] = true;while(!qx.empty()) {int x = qx.front(), y = qy.front();qx.pop(); qy.pop();if(x == x2 && y == y2) return true;for(int i = 0;i < 4;i++) {int nx = x + dx[i], ny = y + dy[i];if(!vis[nx][ny] && tmp[nx][ny] == 0) {qx.push(nx); qy.push(ny); vis[nx][ny] = true;}}}return false; }int bfs() {queue<Node> box,man;box.push(Node(sx,sy,0));man.push(Node(px,py));for(int i = 0;i < 4;i++) dist[sx][sy][i] = -1;int x,y,nx,ny,xp,yp,nxp,nyp,pre;while(!box.empty()) {Node nowbox = box.front(), nowman = man.front();box.pop(); man.pop();x = nowbox.x; y = nowbox.y; pre = nowbox.pre;xp = nowman.x; yp = nowman.y;//printf("box: %d %d man: %d %d time: %d\n",x,y,xp,yp,dist[x][y][pre]);if(x == ex && y == ey) return dist[x][y][pre] + 1;for(int i = 0;i < 4;i++) {nx = x + dx[i]; ny = y + dy[i];nxp = x - dx[i]; nyp = y - dy[i];tmp[x][y] = 1;if((dist[nx][ny][i] == -1 || dist[nx][ny][i] >= dist[x][y][pre] + 1) && tmp[nx][ny] == 0&& bfs1(xp,yp,nxp,nyp)) {dist[nx][ny][i] = dist[x][y][pre] + 1;box.push(Node(nx,ny,i)); man.push(Node(x,y));}tmp[x][y] = 0;}}return -1; }int main() {int T; scanf("%d",&T);while(T--) {scanf("%d%d",&n,&m);memset(dist,-1,sizeof(dist));memset(tmp,0x3f,sizeof(tmp));for(int i = 1;i <= n;i++) {for(int j = 1;j <= m;j++) {scanf("%d",&tmp[i][j]);if(tmp[i][j] == 2) {sx = i; sy = j;}if(tmp[i][j] == 3) {ex = i; ey = j;}if(tmp[i][j] == 4) {px = i; py = j;}if(tmp[i][j] != 1) tmp[i][j] = 0;}}printf("%d\n",bfs());}return 0; }

  

轉載于:https://www.cnblogs.com/rolight/p/3936968.html

總結

以上是生活随笔為你收集整理的HDU 1254 推箱子 BFS的全部內容,希望文章能夠幫你解決所遇到的問題。

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