题意:给出n*m的矩阵,矩阵由'.'和'#'组成,再给出k,表示需要在'.'处加k堵墙,使得剩下的'.'仍然是连通的

先统计出这个矩阵里面总的点数'.'为sum

因为题目说了一定会有一个解,所以找到一个'.',就开始搜,搜到sum-k个连通的点的时候跳出,剩下的没有访问过的点就全部填成墙

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; char g[maxn][maxn];
int vis[maxn][maxn];
int ans,n,m,k,cnt;
int dir[][]={,,-,,,,,-}; void dfs(int x,int y){
vis[x][y]=;
cnt++;
if(cnt==ans) return;
for(int i=;i<;i++){
if(cnt==ans) return;
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx<||xx>n||yy<||yy>m||vis[xx][yy]||g[xx][yy]!='.') continue;
dfs(xx,yy);
}
} int main(){
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) cin>>g[i][j]; ans=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(g[i][j]=='.') ans++;
}
}
ans=ans-k; for(int i=;i<=n;i++){
int flag=;
for(int j=;j<=m;j++){
cnt=;
if(g[i][j]=='.'){
dfs(i,j);
flag=;
break;
}
}
if(flag) break;
} for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(g[i][j]=='.'&&!vis[i][j]) g[i][j]='X';
}
} for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
printf("%c",g[i][j]);
printf("\n");
}
return ;
}

Codeforces 377 A Maze【DFS】的更多相关文章

  1. Codeforces 667C Reberland Linguistics【DFS】

    一道卡题意的题. 题目链接: http://codeforces.com/problemset/problem/667/C 题意: 一个串可以看成一个长度大于4的根,加上其后面的若干个相邻(in a ...

  2. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  3. Codeforces 1036C Classy Numbers 【DFS】

    <题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R]  (1≤L≤R≤1018).,问你这个区间内有多 ...

  4. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  5. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  6. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  8. 【dfs】codeforces Journey

    http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...

  9. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

随机推荐

  1. maven mirror repository

    简单点来说,repository就是个仓库.maven里有两种仓库,本地仓库和远程仓库.远程仓库相当于公共的仓库,大家都能看到.本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用.当你向仓库请 ...

  2. Pytho中两种方式导入模块的差别

    1.使用import module,只是把模块导入,访问模块中的函数名或者是属性是必须使用点运算符(.)来访问,否则直接访问会提示找不到这些函数或者属性. 2.使用from numpy import ...

  3. ASP.NET 处理get/post数据方式

    1.GET方式 NameValueCollection coding; coding = HttpUtility.ParseQueryString(Request.Url.Query, Encodin ...

  4. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

  5. 只 一行显示可左右滚动的文本(UITextField中文限制)

    // // ViewController.m // 一行显示可滚动的文本 // // Created by apple on 15-5-8. // Copyright (c) 2015年 apple. ...

  6. isMobile 一个简单的JS库,用来检测移动设备

    点这里 github地址:https://github.com/kaimallea/isMobile Example Usage I include the minified version of t ...

  7. 关于Try/Catch 代码块

    应当放在Try/Catch 代码块中的常见任务包括连接到一个数据库或与其交互.处理文件.调用Web 服务. 老实说,我这人很少有打破沙锅问到底的精神.不过昨晚听一技术人员跟他的项目经理说要在程序中使用 ...

  8. strrchr函数

    C语言函数 函数简介 函数名称: strrchr 函数原型:char *strrchr(char *str, char c); 所属库: string.h 函数功能:查找一个字符c在另一个字符串str ...

  9. 2011 ACM/ICPC 成都赛区(为2013/10/20成都现场赛Fighting)

    hdu 4111  Alice and Bob 博弈:http://www.cnblogs.com/XDJjy/p/3350014.html hdu 4112 Break the Chocolate ...

  10. ffmpeg转码MPEG2-TS的音视频同步机制分析

    http://blog.chinaunix.net/uid-26000296-id-3483782.html 一.FFmpeg忽略了adaptation_field()数据 FFmpeg忽略了包含PC ...