【题目链接】

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. Ajax——php基础知识(一)

    AMP环境 AMP(Apache.MySQL.PHP)是三个独立的软件,但是对于初学者而言分别安装以及配置需要掌握一定的软件知识,所以就有了很多AMP集成环境帮助我们简化安装 ——WAMP WAMP安 ...

  2. jdbc 使用谨记

    jdbc是java操作数据库的杀手锏.所有java程序员,对jdbc应该都不陌生. 但是,应该你也曾经被其折磨的抓耳挠腮,咬牙切齿吧,也许正因为这样你才对其记忆犹新,刻骨铭心. 这里有一些使用jdbc ...

  3. python发送文本邮件

    #!/usr/bin/env python #coding=utf-8 #Author: Ca0Gu0 import time import smtplib from email.mime.text ...

  4. (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)

    http://blog.csdn.net/u012453843/article/details/70184155 上节课我们一起学习了怎样解决KindEditor富文本编辑器上传图片的浏览器兼容性问题 ...

  5. js 不能用关键字 delete 做函数名

    把delete更改为mydelete正常.

  6. day04-交互、格式化输出及基本运算符

    目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...

  7. Django中 基于form的注册,基于ajax的登录

    1 form.py中写register的的form组件 from django import forms class Register(forms.Form): # 注册的form username ...

  8. javascript模块化编程(一)(http://www.ruanyifeng.com/blog/2012/10/javascript_module.html)

    Javascript模块化编程(一):模块的写法   随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个 ...

  9. LNMP动态网站架构及web应用部署,搭建discuz论坛

    1)部署Nginx 实验tar安装包可找本人拿记得点+关注,感谢亲们支持,评论拿包 systemctl stop firewalld iptables -F setenforce 0 1)安装支持软件 ...

  10. 笔记,js对象浅析

    学习笔记, 来源:http://www.cnblogs.com/zuiyirenjian/p/3535126.html 作者:醉意人间  此外,关于自运行函数可参考  http://benalman. ...