【BZOJ】3299: [USACO2011 Open]Corn Maze玉米迷宫(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=3299
映射一下传送门即可。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1105, oo=~0u>>2, Q=N*N, dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
int mp[N][N], n, m, d[N][N], X, Y, XX, YY, front, tail;
struct dat { int x, y; }q[Q];
struct HS { int x[2], y[2]; }hs[51]; void bfs() {
q[tail].x=X, q[tail++].y=Y;
for1(i, 1, n) for1(j, 1, m) d[i][j]=oo;
d[X][Y]=0;
while(front!=tail) {
dat &t=q[front++]; if(front==Q) front=0;
int x=t.x, y=t.y;
if(XX==x && YY==y) continue;
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || mp[fx][fy]==1) continue;
if(mp[fx][fy]>=4) {
int h=mp[fx][fy], pos=(hs[h].x[0]==fx && hs[h].y[0]==fy);
fx=hs[h].x[pos], fy=hs[h].y[pos];
}
if(d[fx][fy]>d[x][y]+1) {
dat &t2=q[tail++]; if(tail==Q) tail=0;
t2.x=fx, t2.y=fy; d[fx][fy]=d[x][y]+1;
}
}
}
} int main() {
read(n); read(m);
for1(i, 1, 50) CC(hs[i].x, 0), CC(hs[i].y, 0);
for1(i, 1, n) for1(j, 1, m) {
char ch=getchar(); while(ch=='\n' || ch=='\0') ch=getchar();
if(ch=='#') mp[i][j]=1;
else if(ch=='=') mp[i][j]=2, XX=i, YY=j;
else if(ch=='@') mp[i][j]=3, X=i, Y=j;
else if(ch>='A'&&ch<='Z') {
int t=4+ch-'A', pos=(hs[t].x[0]!=0);
hs[t].x[pos]=i; hs[t].y[pos]=j;
mp[i][j]=t;
}
}
bfs();
print(d[XX][YY]);
return 0;
}
Description
今年秋天,约翰带着奶牛们去玩玉米迷宫。迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行。
迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是出口。
在这个迷宫里,有一些神奇的传送点6每个传送点由一对点组成,一旦 走入传送点的某个结点,
机器就会强制把你送到传送点的另一头去。所有的传送点都是双向 的,如果你定到了另一头,机器也会把你送回来。
奶牛在一个单位的时间内只能向相邻的四个方向移动一格,不过传送机传送是瞬间完成 的。
现在W西在迷宫里迷路了,她只知道目前的位罝在哪里,请你帮助她用最短的时间走出 迷宫吧。
Input
第一行:两个用空格分开的整数:N和M,2
第二行到N+1行:第i+1行有M个连续的字符,描述了迷宫第i行的信息。其中"#"代 表不能通行的玉米地,
"."代表可以通行的草地,"@"代表贝西的起始位罝,"="代表迷宫出口,
大写字母“A”到“Z”总是成对出现的,代表一对传送点
Output
第一行:一个整数,表示贝西走出迷宫的最短时间,保证逃离迷宮的路线一定存在
Sample Input
###=##
#.W.##
#.####
#.@W##
######
Sample Output
HINT
从起点向右走,通过w传送,再从另一端 走出迷宫
Source
【BZOJ】3299: [USACO2011 Open]Corn Maze玉米迷宫(bfs)的更多相关文章
- BZOJ 3299: [USACO2011 Open]Corn Maze玉米迷宫(BFS)
水题一道却交了4次QAQ,真是蒟蒻QAQ CODE: #include<cstdio>#include<iostream>#include<cstring>#inc ...
- 3299: [USACO2011 Open]Corn Maze玉米迷宫
3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 Solved: 59[ ...
- bzoj3299 [USACO2011 Open]Corn Maze玉米迷宫
Description 今年秋天,约翰带着奶牛们去玩玉米迷宫.迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行. 迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是 ...
- 【bzoj 3299】 [USACO2011 Open]Corn Maze玉米迷宫(最短路)
就一个最短路,并且边长都是1,所以每个点只搜一次. /************************************************************** Problem: 3 ...
- P1825 [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze
https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...
- 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
随机推荐
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)如何在TwinCAT Scope中做变量监控
为了更好的监控变量,可以打开ScopeView即变量监控器 添加一个Scope View,然后右击添加一个Channel 我们在之前登录的时候可以选择Run-Time的端口(默认是801) ...
- ASP.NET MVC扩展之HtmlHelper辅助方法
什么是HtmlHelper辅助方法? 其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class Fo ...
- 建站笔记1:centos6.5下安装mysql
近期买了个域名,想要玩玩自己建站点:接下来遇到的问题都会一次记录下来.以备自己以后复习查看: 首先建站方案选择: wordPress +centos6.5 +mysql; server买的:搬瓦工最低 ...
- iOS项目开发实战——使用CoreLocation获取当前位置信息
随着基于位置服务LBS和移动互联网的兴起,你的位置是越来越重要的一个信息.位置服务已经是当前的热门应用如微信.陌陌等社交应用的杀手锏.而在iOS开发中,苹果已经给我们提供了一个位置接口.CoreLoc ...
- Introducing MVC
PS:这本书感觉不怎么样,这么多低频词就倒人胃口... Suppose you'v recently launched a new web site, only to find that it's s ...
- 带 IK 分词器的 Luke 和 搜索应用服务器solr
首先在网上查了一下: Solr Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索 ...
- springmvc管理资源开放
关于web.xml的url映射的小知识:<url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀 ...
- centos7防火墙的关闭
从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig. #systemctl list-unit-files|grep firewalld.servic ...
- Handler源代码解析-有关Handler那些事
Handler被成为异步处理大师.相信大家都会用,面试中也常常会问到Handler的底层原理.今天就来看一看Handler的机制. Android的消息处理有四个核心类:Handler.Looper. ...
- Paper Reading 1 - Playing Atari with Deep Reinforcement Learning
来源:NIPS 2013 作者:DeepMind 理解基础: 增强学习基本知识 深度学习 特别是卷积神经网络的基本知识 创新点:第一个将深度学习模型与增强学习结合在一起从而成功地直接从高维的输入学习控 ...