【极值问题】【CF1063B】 Labyrinth
Description
给你一个\(n~\times~m\)的矩阵,一开始你在第\(r\)行第\(c\)列。你的上下移动不受限制,向左最多移动\(x\)次,向右最多移动\(y\)次。求你最多能到多少个点。包括起始点。
Input
第一行是\(n\)和\(m\),代表矩阵规模。
第二行是\(r\)和\(c\),代表你的位置
第三行是\(x\)和\(y\),代表移动限制
下面\(n\)行每行\(m\)个字符,有且仅有'.'和''两种。如果第\(i\)行第\(j\)列是''代表你不能经过这个点。
Output
输出一行一个数代表能到的最多点数
Sample Input
4 5
3 2
1 2
.....
.***.
...**
*....
Sample Output
10
Hint
\(For~All:\)
\(0~\leq~n,m,r,c~\leq~2000\),\(0~\leq~x,y~\leq~10^9\)
Solution
朴素的\(bfs\)显然是对的,可以状态太多存不下。
考虑如果从\((sx,sy)\)点到一个点\((x,y)\)时,假设共向右走了\(r\)步,向左走了\(l\)步,显然\(r-l\)是一个定值。具体的,\(r-l~=~y-sy\)。于是,对于任意一个目标\((x,y)\),发现\(l\)事实上与\(r\)线性正相关。对于一个点,显然到该点的\(r\)越小越好,同时由于\(l\)和\(r\)线性正相关,所以最小化\(r\)的同时,\(l\)已经被最小化了。于是可以直接建图跑最短路,所有向右的边权为1,其他边权为0。跑完后扫描整个地图就可以判断合法性了。
这里的一个新姿势是\(0/1bfs\)。当边权只有\(0/1\)时,可以使用双端队列进行bfs,具体的,当当前边的权值时\(0\)时,将终点压入队首,否则压入队尾。考虑这么做的正确性:易证任意一时刻队列中的点dist差值不超过1。于是正确性显然。\(0/1bfs\)的复杂度为\(O(V+E)\)。相比dij少了一个log。
Code
#include<queue>
#include<cstdio>
#include<cstring>
#define rg register
#define ci const int
#define cl const long long int
typedef long long int ll;
namespace IO{
char buf[110];
}
template <typename T>
inline void qr(T &x) {
rg char ch=getchar(),lst=' ';
while((ch > '9') || (ch < '0')) lst=ch,ch=getchar();
while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(lst == '-') x=-x;
}
template <typename T>
inline void qw(T x,const char aft,const bool pt) {
if(x < 0) {putchar('-');x=-x;}
rg int top=0;
do {
IO::buf[++top]=x%10+'0';
} while(x/=10);
while(top) putchar(IO::buf[top--]);
if(pt) putchar(aft);
}
template <typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template <typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template <typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;}
template <typename T>
inline void mswap(T &a,T &b) {
T _temp=a;a=b;b=_temp;
}
const int maxn = 2010;
const int fx[]={0,-1,0,1};
const int fy[]={1,0,-1,0};
const int fv[]={1,0,0,0};
int n,m,sx,sy,x,y;
int MU[maxn][maxn];
char mp[maxn][maxn];
std::deque<int>Qx,Qy;
int main() {
qr(n);qr(m);qr(sx);qr(sy);qr(x);qr(y);
for(rg int i=1;i<=n;++i) scanf("%s",mp[i]+1);
memset(MU,0x3f,sizeof MU);
MU[sx][sy]=0; Qx.push_front(sx);Qy.push_front(sy);
while(!Qx.empty()) {
int hx=Qx.front(),hy=Qy.front();Qx.pop_front();Qy.pop_front();
for(rg int i=0;i<4;++i) {
int dx=hx+fx[i],dy=hy+fy[i];
if((dx > n) || (dy > m) || (!dx) || (!dy) || (mp[dx][dy] == '*') || (MU[dx][dy] <= MU[hx][hy]+fv[i])) continue;
MU[dx][dy]=MU[hx][hy]+fv[i];
if(i) {Qx.push_front(dx);Qy.push_front(dy);}
else {Qx.push_back(dx);Qy.push_back(dy);}
}
}
rg int _ans=0;
for(rg int i=1;i<=n;++i) {
for(rg int j=1;j<=m;++j) if(mp[i][j] != '*') {
if((MU[i][j] <= y) && ((MU[i][j]-j+sy) <= x)) ++_ans;
}
}
qw(_ans,'\n',true);
return 0;
}
Summary
当题设需要最小化多个变量时,不妨考虑变量间的相关关系,从此转化成单变量的极值问题。
当边权只有\(0\)和\(1\)的时候,可以考虑使用\(0/1bfs\),省去dij的log。
【极值问题】【CF1063B】 Labyrinth的更多相关文章
- cf1063B Labyrinth (bfs)
可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...
- CF1063B Labyrinth
大家一起膜Rorshach. 一般的$bfs$会造成有一些点访问不到的情况,在$system\ test$的时候会$WA40$(比如我……). 发现这张地图其实是一个边权只有$0/1$的图,我们需要计 ...
- $CF1063B\ Labyrinth$ $01$最短路/$01BFS$
\(Des\) 有一个网格图,上面的格子分为空地和障碍,障碍是不可以走的.现在从给定的起点出发开始到处乱走,最多可以往左走\(l\)次,往右走\(r\)次.求可能到达的点数. \(Sol\) 如果只限 ...
- 题解 CF1063B 【Labyrinth】
题解 CF1063B [Labyrinth] 完了我发现我做CF的题大部分思路都和别人不一样qwq 这道题其实很水,不至于到紫题 我们只要bfs一下,向四个方向剪下枝,就A了(好像还跑的蛮快?) 是一 ...
- 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 ...
随机推荐
- FU-A方式分包
当 NALU 的长度超过 MTU 时, 就必须对 NALU 单元进行分片封包. 也称为 Fragmentation Units (FUs). 0 1 2 3 0 1 2 3 4 5 6 7 8 9 ...
- linux学习总结---mysql总结②
函数: 字符串 日期时间 数学 子查询:嵌套查询 1. 做子查询: select sclass from studb where sname='..' 2.select * from studb wh ...
- Python全栈 正则表达式(re模块正则接口全方位详解)
re模块是Python的标准库模块 模块正则接口的整体模式 re.compile 返回regetx对象 finditer fullmatch match search 返回 match对象 match ...
- IDEA搭载Tomcat使用JSTL连接Oracle数据库
1.在IDEA中,JSTL库添加到WEB-INF/lib下面可以直接在JSP页面上通过 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- Linear Equations in Linear Algebra
Linear System Vector Equations The Matrix Equation Solution Sets of Linear Systems Linear Indenpende ...
- MyBatis中文文档
http://mybatis.github.io/mybatis-3/zh/index.html
- 算法与数据结构实验题 4.1 伊姐姐数字 game
★实验任务 伊姐姐热衷于各类数字游戏,24 点.2048.数独等轻轻松松毫无压力.一 日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下: 一开始桌上放着 n 张数字卡片,从左到右 ...
- 在cmd里面使用mysql命令
1.先找出mysqld文件所在的位置,我的是在C:\Program Files\MySQL\MySQL Server 5.1\bin. 2.cd C:\Program Files\MySQL\MySQ ...
- python-网易云简单爬虫
一.准备工作 1.使用python3.6和pycharm 2.使用的模块 tkinter .requests .beautifulSoup.getpass.os 3.网易云的榜单页面地址 https: ...
- 【Linux】- 文件基本属性
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定. 在Linux中我们可 ...