【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时的方案数 状 ...
随机推荐
- 初进JAVA职场面试小技巧:一个老学长的吐血之作!
看着一批批小白的遭遇,有些无奈,又跟我年轻时有些类似.今天正好有点时间,给你几个建议. 1.在结业之前一定要把自己参与过的项目仔细审视一下,一点要特别熟悉项目的流程功能,另外也要重视自己做过的模块,看 ...
- Java 异常的处理方式--throws和try catch
异常的第一种处理方式throws. 看以下例子: import java.io.*;public class ExceptionTest04{ public static void main(Stri ...
- PHP开发框架CodeIgniter
CodeIgniter 是一套给 PHP 网站开发者使用的应用程序开发框架和工具包. 通过下面四步来安装 CodeIgniter: 解压缩安装包: 将 CodeIgniter 文件夹及里面的文件上传到 ...
- CSS之after与before的content 和 attr 配合使用
content 和 attr 配合使用 如果你不想把content内容在CSS里写死,那你可以使用attr表达式来从页面元素中动态的获取内容: /* <div data-line="1 ...
- jQuery filter() , end()
1. jquery filter(condition) : 过滤指定对象中符合条件的元素: 2. jquery end() : 回到原来的操作对象 3. example : <body& ...
- mysql索引是什么?索引结构和使用详解
索引是什么 mysql索引: 是一种帮助mysql高效的获取数据的数据结构,这些数据结构以某种方式引用数据,这种结构就是索引.可简单理解为排好序的快速查找数据结构.如果要查“mysql”这个单词,我们 ...
- 开启VS2017之旅
- Python爬虫教程-34-分布式爬虫介绍
Python爬虫教程-34-分布式爬虫介绍 分布式爬虫在实际应用中还算是多的,本篇简单介绍一下分布式爬虫 什么是分布式爬虫 分布式爬虫就是多台计算机上都安装爬虫程序,重点是联合采集.单机爬虫就是只在一 ...
- 关于建立Android工程R文件丢失的问题
今天开始学习Android了,好久没打开eclipse,建立Android工程老是报错,于是手残的把appcompat-v7给删了,然后建立工程以后重新出来的appcompat-v7有个小叉号,百度了 ...
- svn up (svn update) 状态缩写含义
A:add,新增 C:conflict,冲突 D:delete,删除 M:modify,本地已经修改 G:modify and merGed,本地文件修改并且和服务器的进行合并 U:upda ...