【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 510 int i,j,n,m;
char mp[MAXN][MAXN]; const int dx[] = {,,-,};
const int dy[] = {-,,,};
const int nx[][] = {{,,-,},{,,-,},{,,-,}};
const int ny[][] = {{-,,,},{-,,,},{-,,,}};
const int nxt[][] = {{,,,},{,,,},{,,,}}; struct info
{
int x,y;
int state;
};
inline bool ok(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
}
inline bool check(int x,int y,int state)
{
if (!ok(x,y)) return false;
if (state == && (mp[x][y] == '#' || mp[x][y] == 'E')) return false;
if (state == && (!ok(x,y+) || mp[x][y] == '#' || mp[x][y+] == '#')) return false;
if (state == && (!ok(x+,y) || mp[x][y] == '#' || mp[x+][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][];
while (!q.empty()) q.pop();
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
for (k = ; k < ; k++)
{
dist[i][j][k] = -;
}
}
}
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
if (mp[i][j] == 'X')
{
s.x = i;
s.y = j;
s.state = ;
for (k = ; k < ; 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 < ) s.state = ;
else s.state = ;
}
}
}
if (mp[i][j] == 'O')
{
e.x = i;
e.y = j;
e.state = ;
}
}
}
dist[s.x][s.y][s.state] = ;
q.push(s);
while (!q.empty())
{
cur = q.front();
q.pop();
for (i = ; i < ; 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] == -)
{
q.push((info){tx,ty,ts});
dist[tx][ty][ts] = dist[cur.x][cur.y][cur.state] + ;
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 = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
mp[i][j] = getchar();
}
getchar();
}
bfs();
} return ; }
【POJ 3322】 Bloxorz I的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- IC验证概念总结
一份代码,在写完之后,一定要再经过一次或多次整理和打磨,才能算完成的:一份代码,一定要把其有效代码行,精简.锤炼到最少.最短.最有效,才能算完成的. 下面这些问题和回答是忘记是在哪里看到的了,参考 ...
- CSS——img
img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.
- Tcl之Lab1
Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...
- Discuz! G变量的使用方法
1,G变量的使用方法: 例如:$_G['style'][boardlogo] 风格变量篇 $_G['style'] => Array (官方模板区 cr180整理 $_G['style'][st ...
- [工具]iostat
本文主要分析了Linux的iostat命令的源码 iostat源码共563行,应该算是Linux系统命令代码比较少的了.源代码中主要涉及到如下几个Linux的内核文件: 1./proc/disksta ...
- Win8.1 Hyper-V 共享本机IP上网
公司的Win8.1自带了Hyper v,可是死活连接不到网络. 原因是公司只给每人分配一个局域网IP,而默认情况下Hyper-V的虚拟机会动态分配了一个没有经过MIS人员许可的IP…… 百度了N久解决 ...
- hdu_1018_Big Number_201308191556
Big NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale
https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...
- PDF在线预览-pdfjs使用
请参考我的开源: https://github.com/wuyechun2018/itools/blob/master/src/main/webapp/WEB-INF/views/pdf/index. ...
- 网络基础篇(一)--TCP/IP协议族
TCP/IP协议族是一个分层,多协议通信体系. 1 TCP/IP协议族体系结构 TCP/IP协议族自底而上分为四层: 数据链路层, 网络层, 传输层和应用层. 1.1 数据链路层 实现网卡接口的网络驱 ...