【题目链接】

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的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据

    俗话说:"好记性不如烂笔头".本人学习Hibernate也有一个星期了,对Hibernate也有一个初步的了解.下面对Hibernate显示数据做个笔记,使用租房系统的Hibern ...

  2. (转)Hibernate关联映射——对象的三种关系

    http://blog.csdn.net/yerenyuan_pku/article/details/70148618 Hibernate关联映射——对象的三种关系 Hibernate框架基于ORM设 ...

  3. mybatis 简单的入门实例

    第一步:添加mybaties的架包 第二步:配置mybaties的文件 <?xml version="1.0" encoding="UTF-8" ?> ...

  4. STL源码分析之内存池

    前言 上一节只分析了第二级配置器是由多个链表来存放相同内存大小, 当没有空间的时候就向内存池索取就行了, 却没有具体分析内存池是怎么保存空间的, 是不是内存池真的有用不完的内存, 本节我们就具体来分析 ...

  5. HDU 1465(错排公式)

    不容易系列之一 题意: 一个人要寄n个信封,结果装错了.信纸的编号为1到n,信封的编号为1到n,信纸的编号不能和信封的编号一样,全都不能一样. 思路:错排公式. D(n)表示n件信封装错的所有的情况. ...

  6. 原来PHP对象比数组用更少的内存

    一直以为php的数组更节省内存,从来没有测试过,今天因为要读取一个大配置文件作为pool.做了一次测试: 得出结论是 使用对象保存数据更好,花费的内存是数组array的1/4. 测试代码 class ...

  7. STM32串口IAP实验笔记

    STM32的IAP功能确实方便,以前对此如何实现有所了解,但是一直没去测试,这两天来练了下,可谓困难重重,搞了两天问题也一一解决,下面做些简要的笔记 IAP就是在线应用编程,方便程序升级,可以不用打开 ...

  8. [加强版] Codeforces 835D Palindromic characteristics (回文自动机、DP)

    题目链接: https://codeforces.com/contest/835/problem/D 题意: 一个回文串是\(1\)-回文的,如果一个回文串的左半部分和右半部分一样且都是\(k\)-回 ...

  9. Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法打开文件”“文件格式或文件扩展名无效”

    近段时间,陆续有两个同事先后出现同样的问题(在Excel多个版本都可能出现),问题描述: 当用鼠标右键在任意文件夹或电脑桌面“新建”→“ Microsoft Excel 工作表”,再用鼠标双击打开这个 ...

  10. 集群管理软件clustershell

    一.简介 1.安装方便.一条指令就能轻松安装. 2.配置方便.很多集群管理软件都需要在所有的服务器上都安装软件,而且还要进行很多的连接操作,clustershell就相当的方便了,仅仅需要所有机器能够 ...