题解 CF1063B 【Labyrinth】

完了我发现我做CF的题大部分思路都和别人不一样qwq

这道题其实很水,不至于到紫题

我们只要bfs一下,向四个方向剪下枝,就A了(好像还跑的蛮快?)

是一道锻炼代码能力的好题

Code:

#include <bits/stdc++.h>

#define check(x, y) (x >= 0 && x < n && y >= 0 && y < m)//判断是否越界

const int MaxN = 2010;
const int dx[] = {0, 1, -1, 0}, dy[] = {-1, 0, 0, 1};//bfs方向数组 struct p
{
int x, y;
int cntx, cnty;
}; int ans; int n, m, x, y, limx, limy; std::string s[MaxN]; int vis[MaxN][MaxN]; int disx[MaxN][MaxN], disy[MaxN][MaxN]; void bfs(int x, int y)
{
memset(disx, 0x3f, sizeof(disx));
memset(disy, 0x3f, sizeof(disy));
std::queue<p> q;
q.push((p){x, y, 0, 0});
disx[x][y] = disy[x][y] = 0;
while (!q.empty())
{
p tmp = q.front();
q.pop();
x = tmp.x, y = tmp.y;
for (int i = 0; i <= 3; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (!check(nx, ny) || s[nx][ny] == '*')//当前位置是否合法
continue;
int cntx = tmp.cntx + bool(dy[i] == -1), cnty = tmp.cnty + bool(dy[i] == 1);//计算向左/右走步数
if (cntx < std::min(disx[nx][ny], limx + 1) || cnty < std::min(disy[nx][ny], limy + 1))//判断,剪枝
{
disx[nx][ny] = cntx;
disy[nx][ny] = cnty;//更新向左/右走步数
q.push((p){nx, ny, cntx, cnty}); }
}
}
} int main()
{
scanf("%d%d", &n, &m);
scanf("%d%d", &x, &y), --x, --y;
scanf("%d%d", &limx, &limy);
for (int i = 0; i < n; i++)
std::cin >> s[i];
bfs(x, y);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (disx[i][j] <= limx && disy[i][j] <= limy)
++ans;//统计答案
printf("%d\n", ans);
return 0;
}

题解 CF1063B 【Labyrinth】的更多相关文章

  1. cf1063B Labyrinth (bfs)

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

  2. 2014 百度之星 题解 1004 Labyrinth

    Problem Description 度度熊是一仅仅喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能 ...

  3. CF1063B Labyrinth

    大家一起膜Rorshach. 一般的$bfs$会造成有一些点访问不到的情况,在$system\ test$的时候会$WA40$(比如我……). 发现这张地图其实是一个边权只有$0/1$的图,我们需要计 ...

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

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

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

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

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

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

  7. [POJ1383]Labyrinth

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

  8. 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 ...

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. CSS Cursor屬性 (光标停留显示)

    <html> <body> <p>请把鼠标移动到单词上,可以看到鼠标指针发生变化:</p> <span style="cursor:au ...

  2. 多线程使用libcurl

    curl默认情况下有两个地方是线程不安全的, 需要特殊处理, 1是curl_global_init 这个函数必须单线程调用, 2是默认多线程调用https会莫名其妙的挂掉, 以下是网上的解决方案 ht ...

  3. js 遍历树的层级关系的实现

    1.遍历树的层级关系 1)先整理数据 2)找到id和数据的映射关系 3)然后找到父节点的数据,进行存储 test() { const list = [ { id: ", parentId: ...

  4. robot framework 的关键字Continue For Loop 用法

    Continue For Loop关键字就是python的continue的意思,跳出本层循环,继续执行下一个循环. 我先举个栗子: :FOR    ${index}    IN RANGE    5 ...

  5. table固定宽度与自动宽度

    table-layout:auto(创建的table默认是此布局模式): 对table和td.th指定的宽度无效,浏览器会计算所有单元格的内容宽度才能得出一列宽度 如果想对单元格的内容自动折行需使用w ...

  6. HTML5 - 初识

    众所周知,我们现在的手机APP开发模式分为一下几大类: 一.原生APP 二.纯HTML5 三.原生+HTML5 四.React Native 公司的职位划分: 1.平面设计师  : 作图.切图.HTM ...

  7. 正padding负margin实现多列等高布局(转)

    转自: 巧妙运用CSS中的负值 (http://www.webhek.com/post/2345qwerqwer.html) 代码来自: https://codepen.io/Chokcoco/pen ...

  8. DbTemplate

    package com.me.dbComponent; import java.sql.Connection; import java.sql.PreparedStatement; import ja ...

  9. c# VS.NET 中的调试工具

  10. mysql的2种备份mysqldump 和 Xtrabackup

    mysqldump备份方式 备份 mysqldump -uroot -p 数据库名 > 备份的文件名 恢复(先关闭数据库) mysql -uroot -p 数据库名 < 备份的文件名 Xt ...