[ CodeForces 1063 B ] Labyrinth
\(\\\)
\(Description\)
给出一个四联通的\(N\times M\) 网格图和起点。图中有一些位置是障碍物。
现在上下移动步数不限,向左至多走 \(a\) 步,向右至多走 \(b\) 步,求从起点出发能到达多少个空地。
- \(N,M\le 2000\)
\(\\\)
\(Solution\)
爷们太神了......
开始的想法是直接跑最短路, \(dist\) 为横向移动总步数。
后来发现矛盾在于,如果到一个格子向左走的步数较少,向右走的步数较多,和这种情况反过来,无法确定那种更优,进而无法确定用那种状态更新接下来的点。
然后听爷们讲了好久听懂了正确性证明。考虑要从起点 U 到达 V 这个格子,它横向的差值为 \(2\) 是固定的。
也就是说,任何一个合法的方案向左走的步数减掉向右走的步数都应该等于 \(2\)。
那么这种矛盾不存在了。因为向左向右的步数会同时增长,否则一定不会到达这个目标点。

然后就愉快上最短路,根据\(Dij\) 的原理,循环次数就是访问的点数。
\(\\\)
\(Code\)
#include<cmath>
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 2010
#define R register
#define gc getchar
#define inf 2000000000
using namespace std;
typedef long long ll;
inline ll rd(){
ll x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
}
int n,m,lx,rx,ux,uy,ans;
bool mp[N][N],vis[N][N];
struct dist{
int x,y,l,r,sum;
friend operator < (const dist &x,const dist &y){
return x.sum>y.sum;
}
}dis[N][N];
priority_queue<dist> q;
int main(){
n=rd(); m=rd();
ux=rd(); uy=rd();
lx=rd(); rx=rd();
char c=gc();
for(R int i=1;i<=n;++i)
for(R int j=1;j<=m;++j){
while(c!='.'&&c!='*') c=gc();
mp[i][j]=(c=='.');
c=gc();
dis[i][j].x=i; dis[i][j].y=j;
dis[i][j].l=dis[i][j].r=dis[i][j].sum=inf;
}
dis[ux][uy].l=dis[ux][uy].r=dis[ux][uy].sum=0;
q.push(dis[ux][uy]);
while(!q.empty()){
dist x=q.top(); q.pop();
if(vis[x.x][x.y]) continue;
vis[x.x][x.y]=1; ++ans;
if(mp[x.x+1][x.y]){
if(dis[x.x+1][x.y].sum>x.sum){
dis[x.x+1][x.y].l=x.l;
dis[x.x+1][x.y].r=x.r;
dis[x.x+1][x.y].sum=x.sum;
q.push(dis[x.x+1][x.y]);
}
}
if(mp[x.x-1][x.y]){
if(dis[x.x-1][x.y].sum>x.sum){
dis[x.x-1][x.y].l=x.l;
dis[x.x-1][x.y].r=x.r;
dis[x.x-1][x.y].sum=x.sum;
q.push(dis[x.x-1][x.y]);
}
}
if(mp[x.x][x.y-1]&&x.l<lx){
if(dis[x.x][x.y-1].sum>x.sum+1){
dis[x.x][x.y-1].l=x.l+1;
dis[x.x][x.y-1].r=x.r;
dis[x.x][x.y-1].sum=x.sum+1;
q.push(dis[x.x][x.y-1]);
}
}
if(mp[x.x][x.y+1]&&x.r<rx){
if(dis[x.x][x.y+1].sum>x.sum+1){
dis[x.x][x.y+1].l=x.l;
dis[x.x][x.y+1].r=x.r+1;
dis[x.x][x.y+1].sum=x.sum+1;
q.push(dis[x.x][x.y+1]);
}
}
}
printf("%d\n",ans);
return 0;
}
[ CodeForces 1063 B ] Labyrinth的更多相关文章
- [Codeforces Round #516][Codeforces 1063B/1064D. Labyrinth]
题目链接:1063B - Labyrinth/1064D - Labyrinth 题目大意:给定一个\(n\times m\)的图,有若干个点不能走,上下走无限制,向左和向右走的次数分别被限制为\(x ...
- Codeforces 1064 D - Labyrinth
D - Labyrinth 对于位置(i,j), j - c = R - L = const(常数), 其中R表示往右走了几步,L表示往左走了几步 所以R越大, L就越大, R越小, L就越小, 所以 ...
- CodeForces 616C The Labyrinth
先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #i ...
- Codeforces 1064D/1063B Labyrinth
原题链接/原题链接(代理站) 题目翻译 给你一个\(n*m\)的迷宫和起始点,有障碍的地方不能走,同时最多向左走\(x\)次,向右走\(y\)次,向上向下没有限制,问你有多少个格子是可以到达的. 输入 ...
- [ CodeForces 1063 A ] Oh Those Palindromes
\(\\\) \(Description\) 给出 \(N\) 个小写字母,将他们排成一个字符串,使得这个字符串里包含的回文串最多. \(N\le 10^5\) \(\\\) \(Solution\) ...
- 【Codeforces 1063B】Labyrinth
[链接] 我是链接,点我呀:) [题意] 你可以往左最多x次,往右最多y次 问你从x,y出发最多能到达多少个格子 只能往上下左右四个方向走到没有障碍的格子 [题解] 假设我们从(r,c)出发想要到固定 ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
- Codeforces Round #516 (Div. 2)D. Labyrinth
D. Labyrinth 题目链接:https://codeforces.com/contest/1064/problem/D 题意: 给出一个n*m的矩阵以及人物的起点,并且给出x,y,分别代表这个 ...
随机推荐
- 打开Eclipse出现“An internal error has occurred. java.lang.NullPointerException
今天打开eclipse出现了这个问题:下面是老外给出的办法,粘给大家: Instead of deleting the whole .metadata folder, just delete the ...
- tsdb import 相关
今天一直在做opentsdb 大量导入数据的工作. 中间遇到了一些值得记录的问题, 这里随手记一下 明天好好整理 1. 多进程logger python的logging模块不支持多进程,但我们可以用s ...
- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It ...
- 菜鸟学python-基础(2)
变量命名: 1)必须以字符或下划线开头 2)以单下划线开头(_fo)表示不能直接訪问的类属性,须要类提供的接口进行訪问 3)以双下划线开头(__foo)的代表类的私有成员 4)以双下划线开头(__fo ...
- SDUTOJ 2476Period
#include<iostream> #include<string.h> #include<stdio.h> #define N 1000010 using na ...
- Android网络爬虫程序(基于Jsoup)
摘要:基于 Jsoup 实现一个 Android 的网络爬虫程序,抓取网页的内容并显示出来.写这个程序的主要目的是抓取海投网的宣讲会信息(公司.时间.地点)并在移动端显示,这样就可以随时随地的浏览在学 ...
- eclipse中j2ee(struts2)部署及相关问题释疑
1.eclipse中进行web项目开发时.部署的时候和利用myeclipse部署时有非常大不同,由于在myeclipse的工具栏中有一个部署button.而且在myeclipse的preference ...
- Android架构的简单探讨(一)
在CSDN上看到这样一篇译文,虽然最终的解决方案要按照自己特定的项目去设计,但该文还是引起了很多自己的共鸣,原文猛戳这里. 这是他提出的基于Messaging的MVC框架: 其中包含的设计思想在于:哪 ...
- Email-ext plugin
https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin General This plugin extends Jenkins built i ...
- Ubuntu14.04 x64 zabbix 3.0 安装
U buntu14.04 x64 zabbix 3.0 安装 苦于网上的文档很多,但是对初学者来说,很多都搭建不成功,我重新安装一下.记录一下. 下载deb wget http://repo.za ...