题目链接:https://www.luogu.org/problemnew/show/P1825

带有传送门的迷宫问题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2001;
char map[maxn][maxn];
int fx[4] = {0, 1, 0, -1};
int fy[4] = {1, 0, -1, 0};
int n, m, ex, ey, sx, sy;
struct map{
int x, y, t;
}q[4000001];
struct door{
int x1, y1, x2, y2;
}d[40];
void bfs()
{
int head = 1, tail = 2;
q[head].x = sx, q[head].y = sy, q[head].t = 0;
while(head != tail)
{
for(int i = 0; i < 4; i++)
{
int nowx = q[head].x + fx[i];
int nowy = q[head].y + fy[i];
if(nowx == ex && nowy == ey)
{
printf("%d", q[head].t + 1);
return ;
}
if(nowx > n || nowx <= 0 || nowy > m || nowy <= 0 || map[nowx][nowy] == '#') continue;
if(map[nowx][nowy] >= 'A' && map[nowx][nowy] <= 'Z')
{
if(nowx == d[map[nowx][nowy]-'A'].x1 && nowy == d[map[nowx][nowy]-'A'].y1)
{
q[tail].x = d[map[nowx][nowy]-'A'].x2;
q[tail].y = d[map[nowx][nowy]-'A'].y2;
q[tail].t = q[head].t + 1;
tail++;
}
if(nowx == d[map[nowx][nowy]-'A'].x2 && nowy == d[map[nowx][nowy]-'A'].y2)
{
q[tail].x = d[map[nowx][nowy]-'A'].x1;
q[tail].y = d[map[nowx][nowy]-'A'].y1;
q[tail].t = q[head].t + 1;
tail++;
}
}
else
{
map[nowx][nowy] = '#';
q[tail].x = nowx;
q[tail].y = nowy;
q[tail].t = q[head].t + 1;
tail++;
}
}
head++;
}
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
cin>>map[i][j];
if(map[i][j] == '@') sx = i, sy = j;
if(map[i][j] == '=') ex = i, ey = j;
if(map[i][j] <= 'Z' && map[i][j] >= 'A')
{
if(d[map[i][j]-'A'].x1 == 0 && d[map[i][j]-'A'].y1 == 0)
{d[map[i][j]-'A'].x1 = i, d[map[i][j]-'A'].y1 = j; continue;}
if(d[map[i][j]-'A'].x2 == 0 && d[map[i][j]-'A'].y2 == 0)
{d[map[i][j]-'A'].x2 = i, d[map[i][j]-'A'].y2 = j; continue;}
}
}
bfs();
return 0;
}

【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解的更多相关文章

  1. 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  2. 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  3. 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...

  4. 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 ...

  5. [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  6. 洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】

    P1825 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...

  7. 【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12 ...

  8. luogu P1879 [USACO06NOV]玉米田Corn Fields

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  9. 【Luogu】P1879玉米田(状压DP)

    题目链接 数据范围这么小,难度又这么大,一般就是状态压缩DP了. 对输入进行处理,二进制表示每一行的草地状况.如111表示这一行草地肥沃,压缩成7. 所以f[i][j]表示第i行状态为j时的方案数 状 ...

随机推荐

  1. Filter的常见应用

    1.字符编码过滤器 实现功能,在a.jsp中填写用户名提交到b.jsp,在b.jsp中读取参数名. a.jsp <body> <form action="encoding/ ...

  2. 谈谈我从工作中理解的CDN

    一.CDN定义 CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过 ...

  3. JS常见的几种数组去重方法

    总结一下JS中用到的数组去重的方法  方法一: 该方法利用对象的属性值不能相同: function arrDelLikeElement (array) { const result = []; con ...

  4. iis添加共享目录为虚拟目录

    注意物理路径处不能直接选择映射成的本地盘符!!!

  5. 阿里云更懂你的数据库,免费提供DBA服务

    阿里云更懂你的数据库,免费提供DBA服务   阿里云云数据库(RDS)管理控制台近期将全面升级为云数据库管家.云数据库管家的使命是提供便捷的操作.贴心的服务.专业的处理建议,帮助用户管理好云数据库. ...

  6. Java 多线程使用

    工作中遇到的问题,记录下解决的思路 问题: 对磁盘进行碎片化测试(比如说,磁盘空间是16G),从64K开始写文件,写满后删除一半,然后写32K 的数据,写满后删除一半...直到4K写满删除一般算是结束 ...

  7. According to TLD, tag fmt:formatDate must be empty, but is not 问题的解决

    在执行jsp格式化后报错,检查下代码,发现变成如下的样式: <fmt:formatDate value="${cur.sa_date}" pattern="yyyy ...

  8. Oracle基础之分析表

    analyze table tablename compute statistics; analyze index indexname compute statistics; (analyze 不会重 ...

  9. ArrayList 与 List 关系与代码示例 - Java

    关系 List 是 Java Interface, ArrayList 是 Java Class,它们都属于 java.util 包. Java List 是有序的集合(ordered collect ...

  10. mac和windows自动清理内存工具

    因为我比较懒,所以需要一款能自动清理电脑内存的工具,目的是设置内存最小值,然后自动清理. mac: drcleaner windows: MaxMem win10设置开机启动地址:C:\Program ...