Atcoder C - Closed Rooms(思维+bfs)
题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_c
题意:略。
题解:第一遍bfs找到所有可以走的点并标记步数,看一下最少几步到达所有没锁的点,然后再
for一遍所有标记过的点之后就是考虑向上走向左走向右走向下走(直走)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int M = 1e3;
char mmp[M][M] , s[M][M];
int h , w , k , step[M][M] , dr[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};
struct TnT {
int x , y , step , flag;
};
bool vis[M][M];
void bfs(TnT sta) {
memset(step , 0 , sizeof(step));
step[sta.x][sta.y] = 1;
memset(vis , false , sizeof(vis));
vis[sta.x][sta.y] = true;
queue<TnT>q;
q.push(sta);
while(!q.empty()) {
TnT gg = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++) {
TnT gl = gg;
gl.x += dr[i][0];
gl.y += dr[i][1];
if(gl.x >= 0 && gl.x < h && gl.y >= 0 && gl.y < w && mmp[gl.x][gl.y] != '#' && !vis[gl.x][gl.y]) {
gl.step++;
step[gl.x][gl.y] = gl.step;
vis[gl.x][gl.y] = true;
q.push(gl);
}
}
}
}
int main() {
scanf("%d%d%d" , &h , &w , &k);
for(int i = 0 ; i < h ; i++) {
scanf("%s" , mmp[i]);
}
TnT sta;
for(int i = 0 ; i < h ; i++) {
for(int j = 0 ; j < w ; j++) {
if(mmp[i][j] == 'S') {
sta.x = i , sta.y = j;
break;
}
}
}
sta.step = 0 , sta.flag = k;
bfs(sta);
int ans = 1000000000;
for(int i = 0 ; i < h ; i++) {
for(int j = 0 ; j < w ; j++) {
if(step[i][j] != 0) {
if(i == 0 || i == h - 1 || j == 0 || j == w - 1) {
int gg = step[i][j];
if(gg % k == 0) {
ans = min(ans , gg / k);
}
else {
ans = min(ans , gg / k + 1);
}
}
else {
int gg = step[i][j] , gl = 0;
if(gg % k == 0) {
gl = gg / k;
}
else {
gl = gg / k + 1;
}
gg = i;
if(gg % k == 0) {
ans = min(ans , gl + gg / k);
}
else {
ans = min(ans , gl + gg / k + 1);
}
gg = j;
if(gg % k == 0) {
ans = min(ans , gl + gg / k);
}
else {
ans = min(ans , gl + gg / k + 1);
}
gg = h - 1 - i;
if(gg % k == 0) {
ans = min(ans , gl + gg / k);
}
else {
ans = min(ans , gl + gg / k + 1);
}
gg = w - 1 - j;
if(gg % k == 0) {
ans = min(ans , gl + gg / k);
}
else {
ans = min(ans , gl + gg / k + 1);
}
}
}
}
}
printf("%d\n" , ans);
return 0;
}
Atcoder C - Closed Rooms(思维+bfs)的更多相关文章
- Atcoder Grand Contest 039B(思维,BFS)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int col[207],s[207],n;c ...
- Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- 魔戒(思维+bfs)
Description 蓝色空间号和万有引力号进入了四维水洼,发现了四维物体--魔戒. 这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 "S" 和 "E&quo ...
- atcoder E - Jigsaw(思维)
题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_e 题解:这题很巧妙运用了cnt[i]抽象的表示了上下互补的状态,最后上面突出的一定要处理完.下面 ...
- Atcoder C - +/- Rectangle(思维+构造)
题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_c 题解:挺简单的构造,很容易想到的构造方法就是(h*w)的小矩阵里其他值赋值为1,最后一个赋值为 ...
- Atcoder F - Mirrored(思维+搜索)
题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_d 题意:求rev(N)=N+D的个数,rev表示取反.例如rev(123)=321 题解:具体看 ...
- CF543B Destroying Roads 枚举 + 思维 + BFS
Code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s".in", ...
随机推荐
- for循环打印空心菱形的新方法
相信大家在学习流程控制的循环结构时,一定都用for循环绘制过菱形和空心菱形吧,我记得我当时写的很麻烦,把一个菱形分为上下两部分,上面2重for循环,下面2重for循环,相信有很多的小伙伴都是这样做的吧 ...
- request获取url链接和参数
//Returns the part of this request's URL from the protocol name up to the query string in th ...
- JDK的命令行工具系列 (一) jps、jstat
概述 在我们进行故障定位和性能分析时, 可以使用Java Dump(也叫Dump文件)来帮助排查问题, 它记录了JVM运行期间的内存占用和线程执行等情况.其中Heap Dump文件是二进制格式, 它保 ...
- js获得页面get跳转的参数
通过js获得页面跳转参数 页面通过window.location.href或通过window.parent.location.href进行页面跳转,在新的页面如何获得相应的参数呢? window.lo ...
- HackBar收费版绕过
一段时间没用HackBar,近期做渗透,打开火狐浏览器,按F12键调出HackBar,发现居然需要收费买license才能使用. 经过研究,整理了以下两个绕过HackBar收费版的方法. 第一种:用其 ...
- 洛谷P2125 题解
吐槽: 只能说这道题很数学,本数学蒟蒻推了半天没推出来,只知道要用绝对值,幸亏教练提醒,才勉强想出正解(似乎不是这样的),真的是很无语. 以上皆为吐槽本题,可直接 跳过 分析: 既然题目是要使书架上的 ...
- L1005矩阵取数游戏
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for ( ...
- js 双向绑定数据
let aaa = []; let bbb = [1,2,3]; let ccc = [0,9,8]; aaa = bbb; //此时aaa与bbb被绑定(aaa指向bbb的指向) ,若使用push则 ...
- 佳木斯集训Day1
23333第一次写博客 其实在佳木斯集训之前我都已经两三个月没打代码了 在佳木斯的时候前几天真心手生,导致了前几次考试考的很差... D1的考试还是比较良心的,T1是一道大模拟,直接枚举最后几位是00 ...
- android ——Intent
Intent是android程序中各组件之间进行交互的重要方式,它可以用于指明当前组件想要执行的动作,也可以在不同组件之间传递数据,Intent一般被用于启动活动,启动服务以及发送广播. 一.显式的使 ...