【POJ 3322】 Bloxorz I
【题目链接】
http://poj.org/problem?id=3322
【算法】
广度优先搜索
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 510int i,j,n,m; char mp[MAXN][MAXN];const int dx[4] = {0,0,-1,1}; const int dy[4] = {-1,1,0,0}; const int nx[3][4] = {{0,0,-2,1},{0,0,-1,1},{0,0,-1,2}}; const int ny[3][4] = {{-2,1,0,0},{-1,2,0,0},{-1,1,0,0}}; const int nxt[3][4] = {{1,1,2,2},{0,0,1,1},{2,2,0,0}};struct info {int x,y;int state; }; inline bool ok(int x,int y) {return x >= 1 && x <= n && y >= 1 && y <= m; } inline bool check(int x,int y,int state) {if (!ok(x,y)) return false;if (state == 0 && (mp[x][y] == '#' || mp[x][y] == 'E')) return false;if (state == 1 && (!ok(x,y+1) || mp[x][y] == '#' || mp[x][y+1] == '#')) return false;if (state == 2 && (!ok(x+1,y) || mp[x][y] == '#' || mp[x+1][y] == '#')) return false;return true; } inline void bfs() {int i,j,k,tx,ty,ts;info s,e,cur;queue< info > q;static int dist[MAXN][MAXN][3];while (!q.empty()) q.pop();for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){for (k = 0; k < 3; k++){dist[i][j][k] = -1;}}}for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){if (mp[i][j] == 'X'){s.x = i;s.y = j;s.state = 0;for (k = 0; k < 4; k++){tx = i + dx[k];ty = j + dy[k];if (ok(tx,ty) && mp[tx][ty] == 'X'){s.x = min(i,tx);s.y = min(j,ty);if (k < 2) s.state = 1;else s.state = 2;}}}if (mp[i][j] == 'O'){e.x = i;e.y = j;e.state = 0; }}} dist[s.x][s.y][s.state] = 0;q.push(s);while (!q.empty()){cur = q.front();q.pop();for (i = 0; i < 4; i++){tx = cur.x + nx[cur.state][i];ty = cur.y + ny[cur.state][i];ts = nxt[cur.state][i];if (check(tx,ty,ts) && dist[tx][ty][ts] == -1){q.push((info){tx,ty,ts});dist[tx][ty][ts] = dist[cur.x][cur.y][cur.state] + 1;if (tx == e.x && ty == e.y && ts == e.state){printf("%d\n",dist[tx][ty][ts]);return;}}}}printf("Impossible\n"); }int main() {while (scanf("%d%d",&n,&m) && n && m){getchar(); for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){mp[i][j] = getchar();} getchar();} bfs();}return 0;}
转载于:https://www.cnblogs.com/evenbao/p/9264571.html
【POJ 3322】 Bloxorz I相关推荐
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- BZOJ 2287 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...
- 【POJ 3026】Borg Maze
[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...
- 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)
2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...
- bzoj2287【POJ Challenge】消失之物 缺一01背包
bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...
- 【POJ - 1364】King(差分约束判无解)
题干: Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ` ...
- *【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)
题干: Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12409 Accepted: 3484 Ca ...
最新文章
- 软考总结——虚存管理
- Vue中数组赋值问题
- 【SSM框架系列】Spring - JdbcTemplate声明式事务
- 游戏开发模式一:组件模式(Component)
- [推荐] 世界上最健康的作息时间表
- linux下tar包安装sudo命令,ubuntu12.04LTS安装gv-412-Linux-x86.tar.gz方法
- java 接口文件夹_Java NIO.2 使用Path接口来监听文件、文件夹变化
- iCloud 照片如何转移至谷歌相册?
- 2021-11-14
- vue引用阿里云iconfont使用icon图标(elementUI图标太少)
- Android测试驱动开发实践2
- EXCEL工作表保护密码忘记,撤消工作表保护
- LINUX 下C实现线程池《转载》
- api 微信内置浏览器js_【微网站开发】之微信内置浏览器API使用
- tcp支持浏览器websocket协议
- Mac直接拔掉移动硬盘无法识别或识别要很久的解决方法
- Tableau权限设置
- web前端工程师都做什么工作
- TestNg常用enable、timeOut、exceptedException、groups、dependsOnGroups、dependsOnMethods、@Paramters、priority
- Excel中使用F-检验
热门文章
- 无痕模式后如何找到历史_新高考“3+1+2”模式下,物理与历史如何选择更好
- 人似秋鸿来有信,事如春梦了无痕
- 2018秋北京松松兄弟线下聚会干货分享
- creo打不开stp文件_为什么stp网站打不开 creo打不开stp文件
- 使用Excel中的公式计算日期
- 用C#编写HMI界面,用USB转485和PLC通讯,PLC用的是台达的DVP-12SE系列
- element中的横线,element的tab,下划线不显示的问题
- centos中startup.sh启动服务脚本
- jBPM4的PVM实现解析
- UPDATE或者DELETE忘加WHERE条件的恢复