CodeForces 197D Infinite Maze (BFS+状态压缩)

Ubuntu24.04LTS上Ollama GPU加速配置指南:从驱动安装到多卡均衡负载 本文详细介绍了在Ubuntu24.04LTS系统上配置Ollama GPU加速的完整指南,包括NVIDIA驱动安装、CUDA Toolkit配置、Ollama在线与离线安装方法,以及多GPU负载均衡优化策略。通过实战案例和性能调优建议,帮助开发者充分利用硬件资源提升大型语言模型的运行效率。 阅读详情

题目链接:http://codeforces.com/problemset/problem/197/D

D. Infinite Maze
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (x, y) is a wall if and only if cell is a wall.

In this problem is a remainder of dividing number a by number b.

The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y), provided that the cell he goes to is not a wall.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy used to cyclically tile the plane.

Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point.

The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.

Output

Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).

Examples
Input
5 4
##.#
##S#
#..#
#.##
#..#
Output
Yes
Input
5 4
##.#
##S#
#..#
..#.
#.##
Output
No
Note

In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.

In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.


1 题目分析

将题目分析得到,我们需要解决的是,遍历整个图的所有节点,然后求出最小其权值的最小值。

2 解题思路

 由于整张图中的所有点默认标号为1-16,所以我们可以采用深度优先遍历的方式来遍历整张图,然后记录下用时最短的那个时间即可。

3 存在的问题

 题目中所给最大的编号是16,遍历整张图需要进行16!次操作,时间太长,绝对超时。

4 解决方案

  利用深度优先的方式,在其中进行剪枝操作。

  剪枝分为两种:1 可行性剪枝 2 最优性剪枝。在这里我们只用到后者。

剪枝1 如果在进行深度优先时已经花费的时间大于了已有的最优时间,那么不进行接下来的操作。

剪枝2 记录从出发点1-某个点i的状态,当两个状态相同时,如果前者花费的时间少,那么后者不需要再进行操作。

比如 1-4

状态1 :1 2 3 4  花费时间 :5

状态2 :1 3 2 4  花费时间 :8

由于比较时,前者是已经走完的,那么当后者花费时间比较大的时候,我们直接采取前者的路线即可,不需要再进行自己的操作。

5 难点

如何进行状态的存储?

利用二维数组State[i][j];i表示现在走到的岛屿编号,j代表已经走过的路线。

也许你会问j怎么代表路线呢?

我们利用二进制来存储已经走过的路线,走过就将对应位置设为1,比如:

f[2][5]表示现在在第二个岛屿,之前走过了两个岛屿,因为5二进制表示为101;



BFS+状态压缩AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=20;
int f[maxn][1<<maxn],map[maxn][maxn];
int n,m,ans,vis[maxn];
void dfs(int now,int condition)
{
    if(f[now][condition]>ans)
        return;
    if((condition|(1<<(n-1)))==(1<<n)-1){
        if(f[now][condition]+map[now][n]<ans)
        {
            ans=f[now][condition]+map[now][n];
        }
    }
    for(int i=2;i<=n-1;i++)
        if(vis[i]==0)
        {
            int state=condition|(1<<(i-1));
            if(now==1)
            {
                vis[i]=1;
                f[i][state]=map[1][i];
                dfs(i,state);
                vis[i]=0;
            }
            else if(f[i][state]>f[now][condition]+map[now][i])
            {
                vis[i]=1;
                f[i][state]=f[now][condition]+map[now][i];
                dfs(i,state);
                vis[i]=0;
            }
        }
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n;i++)
        for(int j=1;j<=n;j++)
           scanf("%d",&map[i][j]);
    memset(f,127/3,sizeof(f));
    //cout << f[1][1] << endl;
    ans=1000000000;
    f[1][1]=0;
    dfs(1,1);
    cout << ans << endl;
}

纯状态压缩AC代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<limits>
using namespace std;
const int maxn=20;
int dp[1<<maxn][maxn],map[maxn][maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
          for(int j=0;j<n;j++) scanf("%d",&map[i][j]);
        for(int i=0;i<n-2;i++)
            dp[(1<<i)][i]=map[0][i+1];
        int ans=n-2;
        int flag=0;
        for(int k=0;k<(1<<ans);k++)
        {
            for(int i=0;i<n;i++)
                if(k==(1<<i))
                    flag=1;
            if(flag==1){
                flag=0;
                continue;
            }
            for(int i=0;i<ans;i++) dp[k][i]=100000000;
            for(int i=0;i<ans;i++)
            {
                if(k&(1<<i)){
                    for(int j=0;j<n;j++){
                        if(i==j) continue;
                        if(k&(1<<j)) dp[k][i]=min(dp[k][i],dp[k-(1<<i)][j]+map[j+1][i+1]);
                    }
                }
            }
        }
        int sum=numeric_limits<int>::max();
        int t=(1<<ans)-1;
        for(int i=0;i<ans;i++)
            sum=min(sum,dp[t][i]+map[i+1][ans+1]);
        printf("%d\n",sum);
    }
    return 0;
}


【存储原理】IO超时处理 块层block-scsi层之间 阅读详情

相关推荐

S7-1200 Modbus TCP客户端配置实战:从基础设置到数据交互详解

本文详细介绍了S7-1200 Modbus TCP客户端的配置方法,从基础设置到数据交互的完整流程。涵盖硬件准备、TIA Portal配置、MB_CLIENT指令参数详解、数据缓冲区管理及实战测试,帮助工程师快速掌握西门子PLC与Modbus TCP通信的关键技术。

fire9的博客 310

Codeforces #454 div1 C party(状态压缩bfs)

题意: 给你N个点的一幅图,初始图中有M条边,每次操作可以使得一个点连接的所有点变成一个团,问你最少多少次操作可以使得整个图变成一个团. 解法: 因为N很小 所以我们可以二进制压缩来表示一个点与其他点之间的关系。二进制的第i位代表标号位i+1的人。例如标号为1的人认识标号为3与5还有7的人,标号为1的二进制压缩结果就为000000000000000...

TT4191105的博客 221

Lumerical-定向耦合器1

概述:本文章使用FDE求解器对定向耦合器进行了数值仿真,讨论了耦合器的间距相关性,耦合长度相关性,波长相关性。讨论了脊形波导和条形波导两种情况。

m0_61776728的博客 4611

codeforces 152E bfs+状态压缩

/*题意:在一些n*m的网格中 每个格子里面都有一些不同数量的花 问现在要使得一些给定的重要位置联通 你需要破坏至少多少花来使得道路联通*/ /* 我们采用暴力的思维 把重要位置当成状态来处理 然后枚举出发的位置点 取最优结果就ok啦*/ #include #include #include #include #include #include #include #include #includ

yellowjianokok的博客 512

STM32F103中GPIO_Remap1_CAN1与GPIO_Remap2_CAN1的配置差异与应用场景解析

本文深入解析了STM32F103中GPIO_Remap1_CAN1与GPIO_Remap2_CAN1的配置差异与应用场景。通过剖析AFIO_MAPR寄存器,明确了两者在引脚映射(PB8/PB9 vs PD0/PD1)和适用封装上的区别,并提供了详细的配置步骤、常见陷阱排查及基于项目需求的选型指南,帮助开发者高效完成CAN引脚重映射。

u6v7w8x的博客 394

别再手动建模了!用Simscape Multibody Link一键导入SolidWorks装配体到Simulink

本文介绍了如何利用Simscape Multibody Link插件将SolidWorks装配体一键导入Simulink,实现CAD到仿真的无缝衔接。该技术显著提升机械系统设计与控制效率,特别适用于工业机器人、车辆悬架等复杂机构的动力学分析。文章详细解析了插件安装、模型导出优化及仿真验证的全流程,帮助工程师摆脱手动建模的低效工作模式。

weixin_27256197的博客 457

CodeForces 196B Infinite Maze

Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We've got a rectangularn × m-cell maze. Each ce...

dixiangee374493的博客 259

RK3568/RK3588 + YT 9215交换机芯片,MAC TO MAC 调试记录

原来的方案是rk3568 gmac 直接接phy,phy 接 switch 芯片,只是把交换芯片当交换用,驱动方面基本不用开发,但是要做vlan 那么必须涉及交换芯片的开发。选择裕太微有两个方面的原因:1.国产化替代2.可获得原厂技术支持3.目前已经完成 两个gmac 口交换芯片的配置,实现vlan,内部交换,两个光口可正常通信。

Nyiragongo的博客 1万+

CODEFORCES 197D Infinite Maze

题目:http://codeforces.com/contest/197/problem/D 题意:在迷宫找一条可以循环的路,上下可以连接起来 分析:像一种贪吃蛇游戏,贪吃蛇可以从下面出去,从上面出来。。。找一条使得贪吃蛇不死亡的走法。若找得到输出Yes,否输出No #include using namespace std; int n,m,S[2]; int num[1505][15

论菜鸟的自我修养。。。 1047

【RT-DETR涨点改进】小目标检测、特征融合改进篇 | TGRS 2024 | RT-DETR引入CSFM上下文选择融合模块,适合多模态检测、红外小目标检测、小目标检测、遥感目标检测等任务有效涨点

本文提出了一种改进RT-DETR目标检测模型的方法,通过引入上下文选择融合模块(CSFM)显著提升复杂背景下小目标和低对比度目标的检测精度。CSFM模块采用多尺度特征融合和动态特征选择机制,有效增强模型的全局上下文感知能力,同时保留细节特征。该模块通过通道减少机制和空间信息调节,实现从粗到细的特征聚合,减少误报并提高轮廓检测精度。实验表明,改进后的RT-DETR模型在多个基准测试中表现优异,计算效率与检测精度均优于现有方法。文章还详细介绍了模块配置和yaml文件修改方法,为模型部署提供指导。

Ai缝合怪、助力小伙伴高效发论文 1723

SAP Joule for Consultants 与 Joule for Developers 全面解读

SAP推出面向不同用户群体的Joule AI助手:开发者版(Joule for Developers)专注于编码提效,嵌入开发工具链辅助生成、重构和测试代码;顾问版(Joule for Consultants)聚焦业务场景,集成SAP云应用知识库,为咨询项目提供方案设计、差距分析和实施指引。两者共享AI底座但功能侧重点不同:开发者版优化技术开发流程,顾问版加速项目交付周期。典型应用场景包括制造业云转型方案制定和ABAP代码迁移,分别展示了两版本在业务决策支持和技术实施中的价值。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 538

codeforces 197D Infinite Maze dfs

有一个无限的迷宫,由无数个n*m的小单元组成,类似于拼图,给定这个小单元,每个位置是空地或者障碍,再给定起点,问能否从起点走到无穷远的位置 刚开始的时候没有想清楚,实际上问题的本质在于,如果一个格子能从他相邻的两个不同的格子进入,那么一定可以走到无穷远的位置。 想明白这个就挺好写了,先从起点开始dfs,对于每个没有走到的位置标记是从哪个格子走到他的,然后如果从不同的格子走到了一个已经标

cadongllas的博客 678

解决 maven编译项目-Fatal error compiling: 无效的目标发行版: 21 -> [Help 1]

惊奇的发现,maven没有用环境变量中设置好的JAVA_HOME目录,至此发现错误原因,原来执行java -version默认使用系统环境变量JAVA_HOME,而maven优先使用了windows的用户变量JAVA_HOME(这里之前忘记修改,仍然指向java8路径),修改用户变量JAVA_HOME指向java21或删除该用户变量JAVA_HOME(如无它用)后即可。打开maven包所在的目录,在maven的配置文件 mvn.cmd 文件中,设置java_home的目录。

litlit023的专栏 1799

第一周上H题搜索(Infinite Maze)

We’ve got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (x, y) is a wall if and onl

JSASL的博客 271

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Infinite Maze CodeForces - 197D

题意:给定一个可以无限拼接的图,已知起点S,问你是不是能无限走下去。 思路:如何能无限走下去呢? 对于一个田字图,只要在1中可以到达的点,从第一幅图的S在234中依旧可以到达,那么接下来就是重复上次的操作。对于越界的情况,1等效2 对于为什么是4张图的原因是,1图可以延伸234就可以延伸到任意点/*** Welcome To See My Code ***/ /***If I get TLE ,

Haipai1998的博客 494

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

CodeForces - 197D

开场连wa三发A题,差点心态崩了,还好坚持打完了,一共A了三题 A题,判断能不能放第一个圆,能放的话,先手比赢 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> ...

weixin_30588729的博客 184

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

CodeForces 197D 搜索

题意一张无限大的地图,重复某一块。问是否可以一直走。题解暴力DFS。注意取模和判重。重复分两种情况,一种是回到原点,一种是取模后相同,但是不在同一位置。第一种情况直接过滤就行,第二种情况输出“YES”。注意事项一定要特别注意,起始点S初始化的时候要i+n*m,如果不这样做,地图将无法向左和向上扩展,扩展到最左侧和最上方时将停止扩展,从而导致WA。代码#include <iostream> #incl

zhenlingcn的博客 384

Codeforces 196 B. Infinite Maze(bfs)

We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite

HJY 603

Codeforces 196 B. Infinite Maze

B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We've got a rectangular n × m-cell maze. E

码代码的猿猿 1540

Infinite Maze CodeForces - 196B(简单搜索)

传送门:QAQ 题意:给你一张图,让你从图的起点开始走,走过的地方会崩塌,但是会有相同的图可以拼接起来,问你是否可以在这些途中无限走。   思路:如果在一张新图上走到之前旧图走过的点,那就可以无限走了。   代码: #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;cmath&gt; #include&lt;cstd...

liexss的博客 366

CodeForces 197B Limit

题意:第一行给出两个数字m,n,每个数字代表两个表达式的最高次项,接下来两行数据分别有表示从x^n-x^0的系数,求当x趋向于正无穷时,这两个表达式比值的极限。 链接:http://codeforces.com/problemset/problem/197/B 思路:判断这两个表达式的最高次项是否相同,若分子的最高次小于分母的最高次,结果趋向于0;若分子的最高次大于分母的最高次,当两式的最高次

luminous11的专栏 613

codeforces 378C MAZE

D - Maze Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 377A Description Pavel loves grid mazes. A grid maze is an

Marcus-Bao的个人主页 1053

codeforces 196b

http://codeforces.com/contest/196/problem/B B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

郭大帅 444

Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations(线段树)

题目地址:http://codeforces.com/problemset/problem/339/D 思路:线段树。PushUp时根据层数奇数偶数判断使用|还是^ #include #include #include #include #include using namespace std; #define maxn (1<<18) int a[maxn]; int sum[ma

哆啦AC梦的博客 584

[CodeForces - 197D] D - Infinite Maze

D - Infinite Maze We've got a rectangularn × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the p...

a8233821的博客 211

Codeforces 197

A Problem description 在一个n*m的矩形上,两个人先后放半径为r的圆盘,圆盘不能相叠,但边缘可以相交. 若有人放盘后没有多余的位置放下一个盘,则此人胜利. 若第一个人有必胜方案,则输出First,若第二个人有必胜方案,则输出Second. Data Limit:1 ≤ a, b, r ≤ 100Time Limit: 2s Solution ...

a380988182的博客 193
上一篇: HDU1253:胜利大逃亡(BFS)
下一篇: OpenJ_Bailian 2755 神奇的口袋
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值