【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解
题目链接: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】 题解的更多相关文章
- 洛谷——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
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
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- [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 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...
- 【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解
题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12 ...
- luogu P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- 【Luogu】P1879玉米田(状压DP)
题目链接 数据范围这么小,难度又这么大,一般就是状态压缩DP了. 对输入进行处理,二进制表示每一行的草地状况.如111表示这一行草地肥沃,压缩成7. 所以f[i][j]表示第i行状态为j时的方案数 状 ...
随机推荐
- Heka 的 CMake 编译配置分析
CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件. CMakeLists.txt 的语法比较简单,由命令.注释和 ...
- JS埋点 小结
今天在看<大型网站性能监测.分析与优化>一书,提到性能监测方式,才知道有这个名词 “JS埋点”. 大概作用:通过在web页面中,添加js脚本,实现对页面性能监测(如加载时间.服务器响应时间 ...
- linux cut: invalid byte, character or field list Try 'cut --help' for more information.
1. 概述 centos执行简单shell 脚本 报错 cut: invalid byte, character or field listTry 'cut --help' for more info ...
- 我的gulp第一个程序
以前都是单枪匹马的干,从没用过模块化的打包工具,加入新的团队后,模块化开发学到不少,尤其是工具的使用.团队目前较多的使用gulp,也是最流行的一款前端打包工具.最近Team开始尝试用gulp,我也只是 ...
- Java jsp 自定义标签
1 自定义标签 1.1 引入 需求: 向浏览器输出当前客户的IP地址 (只能使用jsp标签) 1.2 第一个自定义标签开发步骤 1)编写一个普通的java类,继承SimpleTagSupport类,叫 ...
- JavaWeb请求-响应学习笔记
先来看一个流程图: 服务器处理请求的流程: (1)服务器每次收到请求时,都会为这个请求开辟一个新的线程. (2)服务器会把客户端的请求数据封装到request对象中,request就是请求数据的载 ...
- mysqlcppconn之ConnectOptionsMap的使用
由来 继上一篇文章, 发现之前写的一篇文章中断线重连部分是错误的, 也是现在翻阅了源码才知道 想要自动重连, 必须使用ConnectOptionsMap才可以 但由于官方代码没有做好导出部分的处理, ...
- JAVA基本数据类型、引用数据类型-参数传递详解
1:基本类型的参数传值 对于基本数据类型,修改这个值并不会影响作为参数传进来的那个变量,因为你修改的是方法的局部变量,是一个副本.实参的精度级别应等于或低于形参的精度级别,否则报错. class JB ...
- Linux制作deb
1.新建一个我们临时的工作目录mkdir deb 2.新建我们程序的目录mkdir hello 3.编写我们的程序 我们以我们最熟悉的helloworld程序做起,hello.c代码如下#includ ...
- 使用cocostudio 需要在Android.mk文件的配置
直接贴上Android.mk文件吧. 对了,是cocos2d3.0的,不知道2.x是否一样. LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LO ...