当前位置:网站首页>P1825 [USACO11OPEN]Corn Maze S
P1825 [USACO11OPEN]Corn Maze S
2022-07-26 08:32:00 【Tiredd】
题意:
奶牛们去一个 N\times MN×M 玉米迷宫,2 \leq N \leq 300,2 \leq M \leq3002≤N≤300,2≤M≤300。
迷宫里有一些传送装置,可以将奶牛从一点到另一点进行瞬间转移。这些装置可以双向使用。
如果一头奶牛处在这个装置的起点或者终点,这头奶牛就必须使用这个装置。
玉米迷宫除了唯一的一个出口都被玉米包围。
迷宫中的每个元素都由以下项目中的一项组成:
- 玉米,
#表示,这些格子是不可以通过的。 - 草地,
.表示,可以简单的通过。 - 传送装置,每一对大写字母 \tt{A}A 到 \tt{Z}Z 表示。
- 出口,
=表示。 - 起点,
@表示
奶牛能在一格草地上可能存在的四个相邻的格子移动,花费 11 个单位时间。从装置的一个结点到另一个结点不花时间。
输入:
5 6 ###=## #.W.## #.#### #[email protected]## ######
输出:
3
思路:根据题意可以用bfs解决,需要特别注意的是到达传送点时需要特殊处理,传送点之间不需要标记,因为传送点可能需要用多次。例如样例:
3 5
A#@A=
.#.#.
.....此时的走法是1,3 ->1,4->1,1->2,1->1,1->1,4->1,5。因为传送不需要时间,所以一共需要4单位的时间,此时传送门被重复使用了两次。所以传送门不需要标记。那么怎么放置在传送门之间来回走导致死循环呢?这个问题不需要担心,因为我到达传送门,那么我会标记传送门周围的点为true。然后我的dx和dy数组只有走上下左右4个方向的状态,并没有直接传送的状态。所以传送门之间并不会直接传送,而是需要你走出传送门然后走进传送门才能进行传送,所以并不会出现死循环。
那么这样我们就可以愉快的AC了,code如下:
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 500;
int n, m, Sx, Sy, Ex, Ey, dist[N][N];
char g[N][N];
bool vis[N][N];
PII _flash(int x, int y)
{
PII t;
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ )
if(g[i][j] == g[x][y])
{
if(i == x && j == y)
continue;
t.first = i, t.second = j;
return t;
}
}
int bfs()
{
queue<PII> q;
int head = 0, tail = 0;
q.push({Sx, Sy}), vis[Sx][Sy] = true;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0; i < 4; i ++ )
{
int tx = t.first + dx[i], ty = t.second + dy[i];
if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
if(g[tx][ty] == '#' || vis[tx][ty]) continue;
vis[tx][ty] = true, dist[tx][ty] = dist[t.first][t.second] + 1;
if(g[tx][ty] >= 'A' && g[tx][ty] <= 'Z')
{
PII T = _flash(tx, ty);
q.push({T.first, T.second}), dist[T.first][T.second] = dist[tx][ty];
}
else q.push({tx, ty}); //**当到达传送门时,不能将传送门入队,因为如果将传送门入队,那么队列到达传送门的状态时
} //会选择不走传送门的方法,而题目要求到达传送门必须使用,所以要判断来入队。
}
return dist[Ex][Ey];
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ )
{
cin >> g[i][j];
if(g[i][j] == '@')
Sx = i, Sy = j;
if(g[i][j] == '=')
Ex = i, Ey = j;
}
cout << bfs() << endl;
}
/*
3 5
A#@A=
.#.#.
.....
*/边栏推荐
猜你喜欢

Bee guitar score high octave and low octave

Write common API tools swagger and redoc

为什么要在时钟输出上预留电容的工位?

Add in the registry right click to open in vscode

Take out brother is the biggest support in this society

Daily Note (11) -- word formula input arbitrary matrix

vscode 实用快捷键

1、 Redis data structure

Spark scheduling analysis
![[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)](/img/25/475c91d7e673fda3930e5a69be0f28.png)
[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)
随机推荐
On some concepts involved in journal papers compilation + journal query methods
Day 3 homework
shell编程
QT note 2
mysql函数汇总之日期和时间函数
日常一记(11)--word公式输入任意矩阵
【C语言】程序员筑基功法——《函数栈帧的创建与销毁》
Write common API tools swagger and redoc
Redis进阶
Flutter custom player progress bar
Storage of drawings (refined version)
第四天作业
The second lesson is the construction of development environment
【EndNote】文献类型与文献类型缩写汇编
2022年全国职业院校技能大赛“网络安全”竞赛试题文件上传渗透测试答案Flag
Flutter text is left aligned with no blank space in the middle
Date and time function of MySQL function summary
Shell第二天作业
import error: ‘Icon‘ is not exported from ‘antd‘. Import icon error
基于C语言的内存管理-动态分区分配方式模拟