大家一起膜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的更多相关文章

  1. cf1063B Labyrinth (bfs)

    可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...

  2. $CF1063B\ Labyrinth$ $01$最短路/$01BFS$

    \(Des\) 有一个网格图,上面的格子分为空地和障碍,障碍是不可以走的.现在从给定的起点出发开始到处乱走,最多可以往左走\(l\)次,往右走\(r\)次.求可能到达的点数. \(Sol\) 如果只限 ...

  3. 题解 CF1063B 【Labyrinth】

    题解 CF1063B [Labyrinth] 完了我发现我做CF的题大部分思路都和别人不一样qwq 这道题其实很水,不至于到紫题 我们只要bfs一下,向四个方向剪下枝,就A了(好像还跑的蛮快?) 是一 ...

  4. 【极值问题】【CF1063B】 Labyrinth

    传送门 Description 给你一个\(n~\times~m\)的矩阵,一开始你在第\(r\)行第\(c\)列.你的上下移动不受限制,向左最多移动\(x\)次,向右最多移动\(y\)次.求你最多能 ...

  5. 2014百度之星资格赛 1004:Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. ural 1145. Rope in the Labyrinth

    1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...

  7. [POJ1383]Labyrinth

    [POJ1383]Labyrinth 试题描述 The northern part of the Pyramid contains a very large and complicated labyr ...

  8. timus 1033 Labyrinth(BFS)

    Labyrinth Time limit: 1.0 secondMemory limit: 64 MB Administration of the labyrinth has decided to s ...

  9. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

随机推荐

  1. PHP JSON文件解析并获取key、value,判断key是否存在

    /****************************************************************************** * PHP JSON文件解析并获取key ...

  2. BeetleX高性能通讯开源组件

    net core高性能通讯开源组件BeetleX https://www.cnblogs.com/smark/p/9617682.html BeetleX beetleX是基于dotnet core实 ...

  3. BZOJ3075,LG3082 [USACO13MAR]项链Necklace

    题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...

  4. angularjs控制输入框只输入数字及最多输入两位小数

    ps:示例中作用在循环中,其它的你可以按实际需求进行修改使用 <input type="text" ng-model="item.productNumber&quo ...

  5. git revert reset

    git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit. git reset 是把HEAD向后移动了一下,而git revert是HEAD ...

  6. VirtualBox为虚拟OS硬盘扩容

    1.关闭虚拟OS. 2.进入到在VirtualBox的安装路径,执行命令例子如: VBoxManage.exe modifyhd F:\VM\Debian7.2.vdi --resize 40000 ...

  7. 用PowerShell在China Azure创建ARM虚拟机

    Azure目前有两种工作模式:ASM和ARM. 在国内的Azure,我们都是使用ASM的模式.但这种模式有很多限制,比如每个VM必须有一个公网地址,部署不能批量部署等等.ARM对Azure的整体架构做 ...

  8. 蓝桥杯 基础训练 BASIC-27 2n皇后问题

    基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都 ...

  9. html5 日常小结

    HTML5新标签汇总 1.  html5新的 (input type=类型) 元素 <input type="number" name="quantity" ...

  10. sql语句优化方案

    1. 为查询缓存优化你的查询 NOW() 和 RAND() 或是其它的诸如此类的SQL函数都不会开启查询缓存,因为这些函数的返回是会不定的易变的. 所以,你所需要的就是用一个变量来代替MySQL的函数 ...