D - Labyrinth

对于位置(i,j), j - c = R - L = const(常数), 其中R表示往右走了几步,L表示往左走了几步

所以R越大, L就越大, R越小, L就越小, 所以只需要最小化L和R中的其中一个就可以了

由于每次变化为0或1,所以用双端队列写bfs, 保证最前面的值最小, 简化版的dijkstra

不过看到好多没写双端队列的也过了......

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e3 + ;
char s[N][N];
int mnr[N][N];
int dir[][] = {, , , , -, , , -};
deque<piii> q;
int n, m;
void bfs(int x, int y, int rx, int ry) {
mem(mnr, 0x3f);
mnr[x][y] = ;
q.push_back({{x, y}, });
while(!q.empty()) {
piii p = q.front();
q.pop_front();
for (int i = ; i < ; i++) {
int xx = p.fi.fi + dir[i][];
int yy = p.fi.se + dir[i][];
if(i == ) {
if( <= xx && xx <= n && <= yy && yy <= m && s[xx][yy] == '.' && p.se + < mnr[xx][yy]) {
mnr[xx][yy] = p.se+;
q.push_back({{xx, yy}, p.se+});
}
}
else {
if( <= xx && xx <= n && <= yy && yy <= m && s[xx][yy] == '.' && p.se < mnr[xx][yy]) {
mnr[xx][yy] = p.se;
q.push_front({{xx, yy}, p.se});
}
}
}
}
}
int main() {
int r, c, x, y;
scanf("%d %d", &n, &m);
scanf("%d %d", &r, &c);
scanf("%d %d", &x, &y);
for (int i = ; i <= n; i++) scanf("%s", s[i]+);
int ans = ;
bfs(r, c, x, y);
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
int cst = j - c;
int l = mnr[i][j] - cst;
if(mnr[i][j] <= y && l <= x) ans++;
}
}
printf("%d\n", ans);
return ;
}

Codeforces 1064 D - Labyrinth的更多相关文章

  1. CF 1064 D. Labyrinth

    D. Labyrinth http://codeforces.com/contest/1064/problem/D 题意: n*m的矩阵,只能往左走l次,往右走r次,上下走无限制,问能走到多少个点. ...

  2. [Codeforces Round #516][Codeforces 1063B/1064D. Labyrinth]

    题目链接:1063B - Labyrinth/1064D - Labyrinth 题目大意:给定一个\(n\times m\)的图,有若干个点不能走,上下走无限制,向左和向右走的次数分别被限制为\(x ...

  3. CodeForces 616C The Labyrinth

    先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #i ...

  4. codeforces 1064套题

    a题:题意就是问,3个数字差多少可以构成三角形 思路:两边之和大于第三遍 #include<iostream> #include<algorithm> using namesp ...

  5. Codeforces 1064D/1063B Labyrinth

    原题链接/原题链接(代理站) 题目翻译 给你一个\(n*m\)的迷宫和起始点,有障碍的地方不能走,同时最多向左走\(x\)次,向右走\(y\)次,向上向下没有限制,问你有多少个格子是可以到达的. 输入 ...

  6. [ CodeForces 1063 B ] Labyrinth

    \(\\\) \(Description\) 给出一个四联通的\(N\times M\) 网格图和起点.图中有一些位置是障碍物. 现在上下移动步数不限,向左至多走 \(a\) 步,向右至多走 \(b\ ...

  7. [ CodeForces 1064 B ] Equations of Mathematical Magic

    \(\\\) \(Description\) \(T\) 组询问,每次给出一个 \(a\),求方程 \[ a-(a\oplus x)-x=0 \] 的方案数. \(T\le 10^3,a\le 2^{ ...

  8. 【Codeforces 1063B】Labyrinth

    [链接] 我是链接,点我呀:) [题意] 你可以往左最多x次,往右最多y次 问你从x,y出发最多能到达多少个格子 只能往上下左右四个方向走到没有障碍的格子 [题解] 假设我们从(r,c)出发想要到固定 ...

  9. Codeforces Round #516 (Div. 2) (A~E)

    目录 Codeforces 1064 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Lab ...

随机推荐

  1. python基础,if语句,while循环

    if语句: ①2选一                                              ④多选一 if:     条件                              ...

  2. TCP/IP HTPP

    TCP/IP:Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议,又名网络通讯协议,是Internet最基本的协 ...

  3. spring管理的类如何调用非spring管理的类

    spring管理的类如何调用非spring管理的类. 就是使用一个spring提供的感知概念,在容器启动的时候,注入上下文即可. 下面是一个工具类. import org.springframewor ...

  4. SQL[Err] ORA-00933: SQL command not properly ended

    原文链接:https://www.cnblogs.com/godtrue/p/3784526.html 1:可能SQL语句中关键字前后缺少空格 2:Oracle 给表起别名时,直接在表名的后面空格别名 ...

  5. C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别

    1.虚函数(impure virtual) C++的虚函数主要作用是“运行时多态”,父类中提供虚函数的实现,为子类提供默认的函数实现. 子类可以重写父类的虚函数实现子类的特殊化. 如下就是一个父类中的 ...

  6. RoR - Creating and Modifying Table and Columns

    自动生成的id 被当作primary key来使用 timestamp method生成 created_at 与 updated_at columns create_table 和 drop_tab ...

  7. ES6 数值

    数值的表示 二进制表示法新写法: 前缀 0b 或 0B . console.log(0b11 === 3); // true console.log(0B11 === 3); // true 八进制表 ...

  8. JDBC事务(二)转账示例

    示例采用三层框架 web层: package cn.sasa.web; import java.io.IOException; import javax.servlet.ServletExceptio ...

  9. P5280 [ZJOI2019]线段树

    题目链接:洛谷 题目描述:[比较复杂,建议看原题] 这道题太神仙了,线段树上做树形dp. 根据树形dp的套路,都是按照转移的不同情况给节点分类.这里每次modify的时候对于节点的影响也不同,所以我们 ...

  10. 编程实现类似Linux系统的cp功能

    选做题目以及分析 题目:编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文 ...