CF377A Maze

题意:有一个地图,有空地和墙,空地是一个连通块,问要再添k堵墙,使得空地还是一个连通块,输出改变后的地图。
解法:这题的思维很新颖,没想到啊,如果正面搜索,需要考虑的情况比较多,所以逆向思维,先把所有’.‘变成’X’,然后,再找出一个大小为ans-k数量的连通块,记得dfs的时候计数器要定义为全局变量,害,下次我还写bfs。
代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {int f = 1; res = 0;char c = getchar();while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 510;
const LL Mod = 1e9+7;
const int M = 1e6+10;
char tu[N][N];
int n, m, k, ans, cnt;
void dfs(int x, int y) {//cout << x << " " << y << " " << cnt << " " << ans-k << endl;for(int i = 0, tx, ty; i < 4; ++i) {tx = x + ne[i][0], ty = y + ne[i][1];if(tx >=1 && ty >= 1 && tx <= n && ty <= m && tu[tx][ty] == 'X') {if(cnt == ans-k) return;tu[tx][ty] = '.'; ++cnt;dfs(tx, ty);}}
}
int main() {scanf("%d %d %d", &n, &m, &k);int fx, fy;_rep(1, n, i) {scanf("%s", tu[i]+1);_rep(1, m, j) if(tu[i][j] == '.') {tu[i][j] = 'X', ++ans;fx = i; fy = j;}}tu[fx][fy] = '.'; ++cnt; dfs(fx, fy);_rep(1, n, i) printf("%s\n", tu[i]+1);return 0;
}

CF377A Maze题解相关推荐

  1. nc15665 maze 题解 (传送门) 广搜bfs+优先队列

    maze:原题链接 题目描述: 小明来到一个由n x m个格子组成的迷宫 ,有些格子是陷阱,用'#'表示,小明进入陷阱就会死亡,'.'表示没有陷阱.小明所在的位置用'S'表示,目的地用'T'表示. 小 ...

  2. [HDCTF2019]Maze 题解

    少欲则心静,心静则事简.                                         --人民日报 1.查壳 是一个加了upx壳的32位EXE文件 2.使用Kali Linux脱壳 ...

  3. 哈尔滨理工大学软件与微电子学院程序设计竞赛(同步赛) H

    Maze 题解 DFS/BFS. 这里用的是 D F S DFS DFS.其中有并查集的思想. 题目所求为某个格子最多能到达的格子的数量,那么如果格子A可以到达格子B,则格子B必定能到格子A. 也就是 ...

  4. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  5. Codeforces Round #648 (Div. 2)题解 A-D

    文章目录 A - Matrix Game B - Trouble Sort C - Rotation Matching D - Solve The Maze A - Matrix Game 题解:其实 ...

  6. LightOJ - 1027 A Dangerous Maze —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze     PDF (English) Statistics Fo ...

  7. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  8. TYUT-A专题题解(二)

    TYUT-A专题题解(一) TYUT-A专题题解(二) 36暴力枚举 AOJ0008 Sum of 4 Integers[暴力]_海岛Blog-CSDN博客 HDU1407 测试你是否和LTC水平一样 ...

  9. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

最新文章

  1. Mysql技术内幕——InnoDB存储引擎
  2. 无需用户输入!Adobe提出自动生成高质量合成图像新方法
  3. 渗透测试中的文件传输技巧
  4. 02.centos6.4找不到ifcfg-eth0(静态ip配置)
  5. delete mysql 大表_无语了,直到今天,我才揪出MySQL磁盘消耗迅猛的“真凶”!
  6. 做个高颜值的优秀按钮,用 CSS3 实现社交按钮动画
  7. java.lang.IllegalStateException: getOutputStream() has already been called for this response
  8. 微软RPA工具Power Automate Desktop
  9. Django讲课笔记08:定义商城的数据模型
  10. 【Qt】arm-none-eabi-gdb-py.exe由于找不到python27.dll 无法继续执行代码
  11. OTSU 获取最佳阈值,及opencv二值化
  12. wifi服务器无响应如何修复,wifi打不开怎么修复?wifi打不开原因是什么
  13. 我从不劝退,我就是退。
  14. E+H电磁流量计你知道多少?
  15. python-matplotlib-柱状堆积图
  16. 计算机中解决不匹配,电脑显示屏显示不匹配.怎么办
  17. 人脸检测(十五)--改进版VJ人脸检测(LBP特征,VISAPP2017)
  18. 从文本文件读取数据方法
  19. 集成Health Kit时因证书问题出现错误码50063的解决方案
  20. Linux 强制破解root密码 是root密码哦

热门文章

  1. FLP不可能定理学习笔记
  2. python读取大智慧数据_用Python抓取大智慧除权数据
  3. Android提高第二十一篇之MediaPlayer播放网络视频
  4. 四种游泳姿势的动画图解,让你轻松学会游泳
  5. CVX下载、安装以及“此类型的变量不支持使用点进行索引错误”的错误
  6. 湖南元宇宙落地“快准狠”丨长沙马栏山产业园与LAYABOX合作打造元宇宙生态产业园...
  7. 搞IT要具备的特质,像沙僧一样~
  8. 研究型论文_基于流量异常分析多维优化的入侵检测方法
  9. hdu 3572 Task Schedule IPSA 最大流
  10. php 工商银行公众号支付代码_php实现工商银行在线支付接口