zoukankan
html css js c++ java
HDU1254 推箱子 BFS+优先队列
/* 箱子和人共同组成一个状态,用node来记录状态 使用优先队列,是因为只有箱子移动,记数才+1,并不是每次都+1, 从队列中选择记数最小的,进行下一步搜索 只使用优先队列不能保证结果是最小。 因为两个记数相同的状态,下一步的记数不一定相同 使用flag避免重复计算 */ #include <iostream> #include <queue> using namespace std; struct node { int ceil; int people_x,people_y; int box_x,box_y; bool operator<(const node& A)const { return ceil > A.ceil; } }; int map[9][9]; priority_queue<node> Q; int dir[4][2] = {{1, 0},{-1, 0},{0, 1},{0, -1}}; bool flag[9][9][9][9]; int main() { int t, m, n, i, j, px,py,bx,by, ans; node p, q; cin>>t; while(t--) { cin>>m>>n; memset(map, 0, sizeof(map)); memset(flag, 0, sizeof(flag)); ans = -1; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { cin>>map[i][j]; if(map[i][j] == 4) { p.people_x = i; p.people_y = j; } else if(map[i][j] == 2) { p.box_x = i; p.box_y = j; } } } while(!Q.empty())Q.pop(); p.ceil = 0;Q.push(p); flag[p.people_x][p.people_y][p.box_x][p.box_y] = 1; ans = -1; while(!Q.empty()) { p = Q.top();Q.pop(); px = p.people_x; py = p.people_y; bx = p.box_x; by = p.box_y; if(map[bx][by] == 3) { if(ans == -1 || ans > p.ceil) ans = p.ceil; } for(i = 0; i < 4; i++) { //越界 if((px+dir[i][0])<0 || (px+dir[i][0]) >= m || (py+dir[i][1]) < 0 || (py+dir[i][1]) >= n) continue; //墙 else if(map[px+dir[i][0]][py+dir[i][1]] == 1) continue; //不是箱子 else if(px+dir[i][0] != bx || py+dir[i][1] != by) { q = p; q.people_x += dir[i][0]; q.people_y += dir[i][1]; if(flag[q.people_x][q.people_y][q.box_x][q.box_y] == 0) { flag[q.people_x][q.people_y][q.box_x][q.box_y] = 1; Q.push(q); } } //是箱子 else if(px+dir[i][0] == bx && py+dir[i][1] == by) { //越界 if((px+2*dir[i][0])<0 || (px+2*dir[i][0]) >= m || (py+2*dir[i][1]) < 0 || (py+2*dir[i][1]) >= n) continue; //墙 else if(map[px+2*dir[i][0]][py+2*dir[i][1]] == 1) continue; //推箱子 q.box_x = px+2*dir[i][0]; q.box_y = py+2*dir[i][1]; q.people_x = px+dir[i][0];q.people_y = py+dir[i][1]; q.ceil = p.ceil + 1; if(flag[q.people_x][q.people_y][q.box_x][q.box_y] == 0) { flag[q.people_x][q.people_y][q.box_x][q.box_y] = 1; Q.push(q); } } } } cout<<ans<<endl; } return 0; }
查看全文
相关阅读:
5555
3333
4444
试验2
Android之TextView灵活使用(转载)
Android之使用Android-query框架进行开发(一)(转载)
Android 之Html的解析(使用jsoup)
Android之动画的学习(转载)
Android之官方下拉刷新控件SwipeRefreshLayout
Android之sqlite的使用 (转载)
原文地址:https://www.cnblogs.com/windmissing/p/2559884.html
最新文章
JavaScript 执行机制
(a == 3 && a == 4 && a == 5) 返回 true 的问题
JavaScript 精度问题以及JavaScript 浮点数陷阱及解决方案
浅谈 JS ES6函数式编程
js call()和apply()方法的区别和用法详解
Js截取字符串函数 和 JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
JS 数组reduce()方法详解及高级技巧
手写webpack----O(∩_∩)O
ElementUI el-time-picker-只显示小时、分钟,分并添加范围校验
创新,创新性的人,创新性的研究——蒲慕明所长在神经所2010年会上的讲话
热门文章
如何选择研究课题——蒲慕明所长在神经所2008年会上的讲话
论科研不端行为——蒲慕明所长在神经所2007年会上的讲话
论科研创新——蒲慕明所长在神经所2006年会上的讲话
论研究生教育——蒲慕明所长在神经所2005年会上的讲话
聚电解质微凝胶的泊松-玻尔兹曼-弗洛里理论
混合溶剂中的高分子凝胶中的渗透压的一般计算
兰州交通大学开除患癌教师符合理性
穿过地心要多久?
什么是世界一流?什么是科学文化?——蒲慕明所长在神经所2003年所年会上的讲话
6666
Copyright © 2011-2022 走看看