当前位置: 首页 > news >正文

洛谷: P1825 [USACO11OPEN] Corn Maze S

原题链接:P1825 [USACO11OPEN] Corn Maze S - 洛谷

题目描述

This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides work in both directions: a cow can slide from the slide's start to the end instantly, or from the end to the start. If a cow steps on a space that hosts either end of a slide, she must use the slide.

The outside of the corn maze is entirely corn except for a single exit.

The maze can be represented by an N x M (2 <= N <= 300; 2 <= M <= 300) grid. Each grid element contains one of these items:

* Corn (corn grid elements are impassable)

* Grass (easy to pass through!)

* A slide endpoint (which will transport a cow to the other endpoint)

* The exit

A cow can only move from one space to the next if they are adjacent and neither contains corn. Each grassy space has four potential neighbors to which a cow can travel. It takes 1 unit of time to move from a grassy space to an adjacent space; it takes 0 units of time to move from one slide endpoint to the other.

Corn-filled spaces are denoted with an octothorpe (#). Grassy spaces are denoted with a period (.). Pairs of slide endpoints are denoted with the same uppercase letter (A-Z), and no two different slides have endpoints denoted with the same letter. The exit is denoted with the equals sign (=).

Bessie got lost. She knows where she is on the grid, and marked her current grassy space with the 'at' symbol (@). What is the minimum time she needs to move to the exit space?

输入格式

第一行:两个用空格隔开的整数 N 和 M。

第 2∼N+1 行:第 i+1 行描述了迷宫中的第 i 行的情况(共有M个字符,每个字符中间没有空格)。

输出格式

一个整数,表示起点到出口所需的最短时间。

隐藏翻译

题意翻译

奶牛们去一个 N×M 玉米迷宫,2≤N≤300,2≤M≤300。

迷宫里有一些传送装置,可以将奶牛从一点到另一点进行瞬间转移。这些装置可以双向使用。

如果一头奶牛处在这个装置的起点或者终点,这头奶牛就必须使用这个装置,奶牛在传送过后不会立刻进行第二次传送,即不会卡在传送装置的起点和终点之间来回传送。

玉米迷宫除了唯一的一个出口都被玉米包围。

迷宫中的每个元素都由以下项目中的一项组成:

  1. 玉米,# 表示,这些格子是不可以通过的。
  2. 草地,. 表示,可以简单的通过。
  3. 传送装置,每一对大写字母 A 到 Z 表示。
  4. 出口,= 表示。
  5. 起点, @ 表示

奶牛能在一格草地上可能存在的四个相邻的格子移动,花费 1 个单位时间。从装置的一个结点到另一个结点不花时间。

输入输出样例

输入 #1复制

5 6
###=##
#.W.##
#.####
#.@W##
######

输出 #1复制

3

        该题是一道bfs(广度优先搜索的题),因为广度优先搜索第一条到达终点的路径即为最短的路径。在该题中加入了一个很新颖的设定——传送门。用从A到Z的一对大写字母来表示传送门,可以从任意一端传送到另一端,并且传送的过程中是不消耗时间的。这里有一个大坑点。题目说了在进行一次传送过后不会“立刻”进行第二次传送。这个“立刻”很有说法。题目的意思是不能出现刚传过去就又传过来的这一种反复传送的情况。但是可以在传过去走一步之后在踏入传送门。所以,在标记状态时,要把起点的状态设置为已标记,中的状态不用标记,就可以符合题意。

        在记录传送门时,我用了一个结构体数组a,b;数组大小为26。数组下标0~25分别代表大写字母A~Z。分别存放传送门两端的坐标。可以将先遍历到的传送门的一个点当作起点,另一个即为终点。然后在输入用例时处理传送门的两端的坐标,先出现的传送门坐标放在数组a里,另一个则放在数组b里。在bfs中遍历时,如果遇到了传送门就进行特殊处理,只标记传送门的起点状态。

        总而言之,该题的主要难点就是对于传送门的处理,以及对题意的理解,做这种题还是要细心一点的。

具体代码如下:

#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct node{
    int x,y;
}a[26],b[26];//分别存放传送门两端的坐标
struct nn{
    int x,y,step;
};

int n,m;
int sx,sy,ex,ey;
int dict[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int arr[26];
char grid[310][310];
bool vis[310][310];

int bfs(){
    queue<nn> q;
    q.push({sx,sy,0});
    vis[sx][sy] = true;
    while (!q.empty()){
        nn now = q.front();
        q.pop();
        int x = now.x;
        int y = now.y;
        int step = now.step;
        for (int i = 0;i < 4;++i){
            int xx = x + dict[i][0];
            int yy = y + dict[i][1];
            if (xx < 0 || xx >= n || yy < 0 || yy >= m)
                continue;
            if (grid[xx][yy] == '#' || vis[xx][yy])
                continue;
            //到达终点就结束bfs
            if (xx == ex && yy == ey)
            {
                return step + 1;
            }
            //处理遇到传送门的情况
            if (grid[xx][yy] >= 'A' && grid[xx][yy] <= 'Z')
            {
                int num = grid[xx][yy] - 'A';
                //标记传送门起点的状态为已标记
                vis[xx][yy] = true;
                if (a[num].x == xx && a[num].y == yy)
                {
                    xx = b[num].x;
                    yy = b[num].y;
                }
                else
                {
                    xx = a[num].x;
                    yy = a[num].y;
                }
                q.push({xx,yy,step + 1});
                continue;
            }
            vis[xx][yy] = true;
            q.push({xx,yy,step + 1});
        }
    }
    return 0;
}
int main()
{
    cin>>n>>m;
    for (int i = 0;i < n;++i){
        for (int j = 0;j < m;++j){
            cin>>grid[i][j];
            //处理传送门两端的坐标
            if (grid[i][j] >= 'A' && grid[i][j] <= 'Z')
            {
                int num = grid[i][j] - 'A';
                arr[num]++;
                if (arr[num] == 1)
                {
                    a[num] = {i,j};
                }
                else
                {
                    b[num] = {i,j};
                }
            }
            //记录起点和终点的坐标
            if (grid[i][j] == '@')
            {
                sx = i;
                sy = j;
            }
            if (grid[i][j] == '=')
            {
                ex = i;
                ey = j;
            }
        }
    }
    int step = bfs();
    cout<<step;
    return 0;
}

相关文章:

  • 揭秘:父子组件之间的传递
  • Redis批量操作详解
  • Scala的基本语法(二)
  • Python第七章03:文件写入操作
  • JS判断对象是否为空的方法
  • 移动端六大语言速记:第4部分 - 数据结构
  • Linux环境下Ollama升级及Gemma3模型部署指南
  • 突发,国行 iPhone 17,支持 eSIM
  • C# 事件(Event)
  • 【Linux】Linux 系统启动流程详解
  • 深入理解 POSIX 线程 (pthread):从基础到高级应用
  • 3-栈、队列、数组
  • AIGC(生成式AI)试用 29 -- 用AI写读书笔记
  • 【 Vue 2 中的 Mixins 模式】
  • AI换脸工具--FaceFusion 整合包
  • 13 配置Hadoop集群-测试使用
  • harmony OS NEXT-沉浸式布局实现笔记
  • CSS层叠顺序
  • 算法竞赛备赛——【数据结构】并查集
  • Hexo + Fluid博客实现自定义图标
  • FileZilla做网站/青岛新闻最新消息
  • 厚街网站建设公司/百度网站大全
  • 手表价格网站/表白网站制作
  • 网站商城建设要多少钱/宁波seo推荐优化
  • 公司想建立一个网站吗/网站seo推广方案
  • 做网站哪家强/近期国际新闻