【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;
}

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9642111.html

时间: 2024-10-12 14:56:16

【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 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

[USACO11OPEN]玉米田迷宫Corn Maze

题目描述 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 w

[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)

第一次做MLE了…第二次WA了5个点,其实就是一个判断错了…QAQ总的来说…是个水题/板子题(逃 #include<bits/stdc++.h> using namespace std; #define For(i,l,r) for(register int i=l; i<r; i++) int n,m; int d[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 }; bool bj,vis[301][301]; char ch[301][301]; struct

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

题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12行12列,最多1<<12. 这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1. 加法原理转移. $ j&k==0 $ $ dp[i][j] += dp[i-1][k] $ #include <cstdio> #include <cstring> #inc

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

题目链接 数据范围这么小,难度又这么大,一般就是状态压缩DP了. 对输入进行处理,二进制表示每一行的草地状况.如111表示这一行草地肥沃,压缩成7. 所以f[i][j]表示第i行状态为j时的方案数 状态j指的是一个二进制集合,有牛在吃草的位置是1,不再吃草的位置是0 f[i][j]=Sum(f[i-1][k]) 限制:(1) j必须是草地状况的子集.很好理解,如果有牛在贫瘠草地上吃草--会被投诉到动物保护协会的 (2) j 的相邻两个位置不能都是1. 代码表示为!(j&(j<<1) (

3299: [USACO2011 Open]Corn Maze玉米迷宫

3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[Submit][Status][Discuss] Description 今年秋天,约翰带着奶牛们去玩玉米迷宫.迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行. 迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是出口. 在这个迷宫里,有一些神奇的传送点6每个传送点

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the

P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 这道题跟互不侵犯差不多,但是数据范围更大了 对于状态压缩dp,提前枚举出转移是一种很好的优化 然后对于转移也需要仔仔细细的分析,如同期望dp #include<cstdio> #include<algorithm> #include<iostream> const int mod=1e8; int map[20]; long long f[2][1<<12]; int b[400],tot;

玉米田(状压DP)

题目:P1879 [USACO06NOV]玉米田Corn Fields 参考:状态压缩动态规划 状压DP 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. John想知道,如果不考虑草地的总块数