CF1063B Labyrinth
大家一起膜Rorshach。
一般的$bfs$会造成有一些点访问不到的情况,在$system\ test$的时候会$WA40$(比如我……)。
发现这张地图其实是一个边权只有$0/1$的图,我们需要计算的是从$(r, c)$开始到每一个点的最短路,因为边权只有两种的特性,我们可以用一个双端队列,每一次向上向下走的放在队首,每一次向左向右走放在队尾,就可以得到正确的解。
也可以用优先队列,这样子多一个$log$。
时间复杂度$O(n^2)$。
Code:
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
using namespace std; const int N = ;
const int dx[] = {, , , -};
const int dy[] = {, , -, }; int n, m, r, c, lstp, rstp, ans = ;
bool vis[N][N];
char mp[N][N]; struct Node {
int x, y, ls, rs; inline Node(int nowX = , int nowY = , int nowLs = , int nowRs = ) {
x = nowX, y = nowY, ls = nowLs, rs = nowRs;
} };
deque <Node> Q; inline bool valid(Node now) {
return now.x >= && now.x <= n && now.y >= && now.y <= m && mp[now.x][now.y] != '*' && !vis[now.x][now.y];
} void bfs() {
Q.push_front(Node(r, c, lstp, rstp));
vis[r][c] = , ++ans;
for(; !Q.empty(); ) {
Node out = Q.front(); Q.pop_front();
for(int i = ; i < ; i++) {
Node in = Node(out.x + dx[i], out.y + dy[i], out.ls - (dy[i] == -), out.rs - (dy[i] == ));
if(!valid(in)) continue;
if(in.ls == - || in.rs == -) continue;
if(i == || i == ) Q.push_back(in);
else Q.push_front(in);
vis[in.x][in.y] = , ++ans;
}
}
} int main() {
scanf("%d%d%d%d%d%d", &n, &m, &r, &c, &lstp, &rstp);
for(int i = ; i <= n; i++) scanf("%s", mp[i] + ); bfs(); /* for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= m; j++)
printf("%d ", vis[i][j]); */ printf("%d\n", ans);
return ;
}
CF1063B Labyrinth的更多相关文章
- cf1063B Labyrinth (bfs)
可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...
- $CF1063B\ Labyrinth$ $01$最短路/$01BFS$
\(Des\) 有一个网格图,上面的格子分为空地和障碍,障碍是不可以走的.现在从给定的起点出发开始到处乱走,最多可以往左走\(l\)次,往右走\(r\)次.求可能到达的点数. \(Sol\) 如果只限 ...
- 题解 CF1063B 【Labyrinth】
题解 CF1063B [Labyrinth] 完了我发现我做CF的题大部分思路都和别人不一样qwq 这道题其实很水,不至于到紫题 我们只要bfs一下,向四个方向剪下枝,就A了(好像还跑的蛮快?) 是一 ...
- 【极值问题】【CF1063B】 Labyrinth
传送门 Description 给你一个\(n~\times~m\)的矩阵,一开始你在第\(r\)行第\(c\)列.你的上下移动不受限制,向左最多移动\(x\)次,向右最多移动\(y\)次.求你最多能 ...
- 2014百度之星资格赛 1004:Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- ural 1145. Rope in the Labyrinth
1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...
- [POJ1383]Labyrinth
[POJ1383]Labyrinth 试题描述 The northern part of the Pyramid contains a very large and complicated labyr ...
- timus 1033 Labyrinth(BFS)
Labyrinth Time limit: 1.0 secondMemory limit: 64 MB Administration of the labyrinth has decided to s ...
- poj 1383 Labyrinth
题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...
随机推荐
- linux 下安装rar解压
在liunx下原本是不支持rar文件的,需要安装liunx下的winrar版本,操作如下 wget http://www.rarsoft.com/rar/rarlinux-4.0.1.tar.gz t ...
- BZOJ - 2243 染色 (树链剖分+线段树+区间合并)
题目链接 线段树维护区间连续段个数即可.设lc为区间左端点颜色,rc为区间右端点颜色,则合并两区间的时候,如果左区间右端点和右区间左端点颜色相同,则连续段个数-1. 在树链上的区间合并可以定义一个结构 ...
- loj 6085.「美团 CodeM 资格赛」优惠券
题目: 一个有门禁的大楼,初始时里面没有人. 现在有一些人在进出大楼,每个人都有一个唯一的编号.现在有他们进出大楼的记录,但是有些被污染了,只能知道这里有一条记录,具体并不能知道. 一个人只有进大楼, ...
- webpack里CommonJS的require与ES6 的module.exports加载模块有何不同
只需明白commonjs的规则即可,import会被转化为commonjs格式的,babel默认会把ES6的模块转化为commonjs规范的. import vue from 'vue'; //等价于 ...
- bzoj 4712 洪水——动态DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4712 因为作为动态DP练习而找到,所以就用动态DP做了,也没管那种二分的方法. 感觉理解似乎 ...
- bae3.0第四步 第一个polls系统
1.创建自己的app 进入新建的blog工程目录,执行其下面的manage.py来创建polls应用,命令为: python manage.py startapp polls2.定义app的model ...
- Python学习笔记 - PostgreSQL的使用
一.安装PostgreSQL模块 pip install psycopg2 有时候会失败,多安装2次就好了(我是第二次成功了). 二.数据库连接接口 由于Python统一了数据库连接的接口,所以psy ...
- 四川第七届 C Censor (字符串哈希)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- 创建github怎样管理
创建版本库 第一步: 创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录 $mkdir learngit $cd learngit $pwd mkdir learngit 创建一个名叫“ ...
- PostgreSQL 监控数据库活动
监控数据库活动 1. 标准Unix 工具 [root@mysqlhq ~]# ps auxww | grep ^postgrespostgres 12106 0.0 0.0 340060 15064 ...