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

CH2601 电路维修(双端队列bfs)建图恶心

CH2601 电路维修

  • 双端队列bfs,其实就是因为只有0和1所以可以直接2维护队列单调性(和优先队列一个道理)
  • 建图的过程需要仔细斟酌(想一想id为什么这么写)
  • 还有,空间要开够(很玄学),我一开始N开到600一直过不了,后来必须改到700以上
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cctype>
 5 #include <queue>
 6 #include <cstring>
 7 using namespace std;
 8 
 9 #define inf 0x3f3f3f3f
10 #define res register int
11 const int N=720,M=N*N;
12 int n,m,T;
13 int head[M],ver[M*2],nxt[M*2],edge[M*2],tot;
14 int d[M*2];
15 inline int id(int i,int j){return i*(m+1)+j;}
16 // 每行m个格子,则有m+1个顶点
17 inline void add(int x,int y,int z)
18 {
19     ver[++tot]=y; nxt[tot]=head[x]; head[x]=tot; edge[tot]=z;
20     ver[++tot]=x; nxt[tot]=head[y]; head[y]=tot; edge[tot]=z;
21 }
22 
23 deque <int> q;
24 inline int bfs(int st,int ed)
25 {
26     memset(d,0x3f,sizeof(d));
27     q.push_back(st); d[st]=0;
28     while(!q.empty())
29     {
30         int x=q.front(); q.pop_front();
31         // if(d[x]!=inf) continue;
32         for(res i=head[x] ; i ; i=nxt[i])
33         {
34             int y=ver[i];
35 //            if(d[y]!=inf) continue;
36             if(d[y]>d[x]+edge[i])
37             {
38                 d[y]=d[x]+edge[i];
39                 if(edge[i]) q.push_back(y);
40                 else q.push_front(y);
41             }
42         }
43     }
44     return d[ed];
45 }
46 
47 char s[N][N];
48  
49 
50 int main()
51 {
52 //    freopen("input","r",stdin);
53     scanf("%d",&T);
54     while(T--)
55     {
56         scanf("%d %d",&n,&m);
57         tot=0;
58         memset(head,0,sizeof(head));
59         for(res i=0 ; i<n ; i++) cin>>s[i];
60         for(res i=0 ; i<n ; i++)
61             for(res j=0 ; j<m ; j++)
62             {
63                 if(s[i][j]=='/')
64                 {
65                     add(id(i+1,j),id(i,j+1),0);
66                     add(id(i,j),id(i+1,j+1),1);
67 //                    if(i==0&&j==0) printf("%")
68                 }
69                 else
70                 {
71                     add(id(i+1,j),id(i,j+1),1);
72                     add(id(i,j),id(i+1,j+1),0);
73                 }
74             }
75         int ans=bfs(id(0,0),id(n,m));
76         if(ans==inf) puts("NO SOLUTION");
77         else printf("%d\n",ans);
78     }
79     
80     return 0;    
81 }
View Code

 

转载于:https://www.cnblogs.com/wmq12138/p/10388010.html

相关文章:

  • 新书推荐|Windows黑客编程技术详解
  • 大主子表关联的性能优化方法
  • 5G一周热闻:华为夺联通5G大单,首张5G电话卡发放
  • einx 1.0 发布,golang 游戏服务器框架
  • “迁移策略+新容器运行时”应对有状态应用的冷热迁移挑战
  • 京东物流王梓晨:打造全栈团队,你要避开这些大坑
  • 解决Centos7 yum 出现could not retrieve mirrorlist 错误
  • [ajaxupload] - 上传文件同时附件参数值
  • 14DBCP连接池
  • 闭包,sync使用细节
  • 力扣(LeetCode)21
  • uni-app项目数字滚动
  • 挂载磁盘报错“Structure needs cleaning”
  • js ES6 求数组的交集,并集,还有差集
  • 安装python包到指定虚拟环境
  • [deviceone开发]-do_Webview的基本示例
  • 《深入 React 技术栈》
  • 10个确保微服务与容器安全的最佳实践
  • CSS实用技巧干货
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • JavaScript-Array类型
  • MySQL主从复制读写分离及奇怪的问题
  • node 版本过低
  • node入门
  • Node项目之评分系统(二)- 数据库设计
  • Promise面试题,控制异步流程
  • Sass 快速入门教程
  • springMvc学习笔记(2)
  • use Google search engine
  • Vue 重置组件到初始状态
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 记一次和乔布斯合作最难忘的经历
  • 近期前端发展计划
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 什么是Javascript函数节流?
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 一天一个设计模式之JS实现——适配器模式
  • 用jquery写贪吃蛇
  • 栈实现走出迷宫(C++)
  • 正则表达式小结
  • # .NET Framework中使用命名管道进行进程间通信
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (三)mysql_MYSQL(三)
  • (一)基于IDEA的JAVA基础12
  • (转) Android中ViewStub组件使用
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [145] 二叉树的后序遍历 js