【题目链接】

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. ABP生成错误:必须添加对程序集“netstandard”的引用

    当前使用ABP版本为:4.6.0 升级vs2017到15.4版本,升级framework到4.7版本 如果Core版本请升级到net core 2

  2. 微信小程序video监测播放进度

    video组件提供的进度相关的监测只有 bindtimeupdate ,官方说明这个函数250ms触发一次,在开发者工具上基本符合,但在真机上每隔1秒触发一次.达不到我们要求的精度.对比下音频,wx. ...

  3. Mock随机生成数据模拟后台接口

    <html> <head> <title>测试</title> <script src="http://code.jquery.com/ ...

  4. centos 设置 ip地址

    linux设置ip,主要是修改/etc/sysconfig/network-scripts/ifcfg-** 里面的网卡配置文件,然后命令 service network restart 生效 自动获 ...

  5. mysql高可用架构mha之master_ip_failover脚本

    脚本如下:           #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ...

  6. mitmproxy安装与使用

    mitmproxy安装与使用 (抓包,中间人代理工具.支持SSL) 在开发微信公端的时候开发调试只能用浏览器自带开发工具,本来移动端可以用用fiddler.wireshark等工具来抓包,但是自从改用 ...

  7. sysbench使用指南

    sysbench 安装.使用和测试 摘要: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQ ...

  8. codevs2833 奇怪的梦境

    2833 奇怪的梦境  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...

  9. idea和eclipse互相导入方法

    1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...

  10. ubuntu_linux /boot/grub/grub.conf

    ==========================================UBUNTU  /boot/grub/grub.conf文件============================ ...