BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
- /*
- 题意:问最少替换'*'为'.',使得'.'连通的都是矩形
- BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找
- 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉
- */
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- using namespace std;
- const int MAXN = 2e3 + ;
- const int INF = 0x3f3f3f3f;
- int n, m;
- int dx[][] = {{,,},{,-,-},{-,-,},{,,}};
- int dy[][] = {{,,},{,,},{,-,-},{,-,-}};
- char s[MAXN][MAXN];
- bool ok(int x, int y)
- {
- if (x < || x >= n) return false;
- if (y < || y >= m) return false;
- return true;
- }
- void BFS(void)
- {
- queue<pair<int, int> > Q;
- for (int i=; i<n; ++i)
- {
- for (int j=; j<m; ++j)
- {
- if (s[i][j] == '.')
- {
- Q.push (make_pair (i, j));
- }
- }
- }
- while (!Q.empty ())
- {
- int x = Q.front ().first; int y = Q.front ().second;
- Q.pop ();
- for (int i=; i<; ++i)
- {
- int cnt = ; int px, py; bool flag = true;
- for (int j=; j<; ++j)
- {
- int tx = x + dx[i][j]; int ty = y + dy[i][j];
- if (ok (tx, ty))
- {
- if (s[tx][ty] == '*')
- {
- cnt++; px = tx; py = ty;
- }
- }
- else flag = false;
- }
- if (flag && cnt == )
- {
- s[px][py] = '.'; Q.push (make_pair (px, py));
- }
- }
- }
- }
- int main(void) //Codeforces Round #297 (Div. 2) D. Arthur and Walls
- {
- while (scanf ("%d%d", &n, &m) == )
- {
- for (int i=; i<n; ++i) scanf ("%s", s[i]);
- BFS ();
- for (int i=; i<n; ++i) printf ("%s\n", s[i]);
- }
- return ;
- }
- /*
- 5 5
- .*.*.
- *****
- .*.*.
- *****
- .*.*.
- 6 7
- ***.*.*
- ..*.*.*
- *.*.*.*
- *.*.*.*
- ..*...*
- *******
- */
BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls的更多相关文章
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- 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 ...
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- Linux主要命令
pwd 查看当前路径 cd .. 表示后一级目录级 cd . 表示当前目录 cd ../.. 后退两级 cd 表示进入当前家目录 date 返回当前的一个具体时间 -s 修改 ...
- html-基本form元素---ShinePans
<html> <meta http-equiv="content-type" content="text/html;charset=UTF-8" ...
- Python 003- 小知识汇总(更新中)
#查询key是否存在,可以在使用未知的字典的时候使用 #-*- coding:utf-8 -*- D={'a':1,'c':3,'b':2} for key in sorted(D): print(k ...
- TreeSet实现Comparator接口的排序算法的分析
为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...
- hadoop reduce 阶段遍历 Iterable 的 2 个“坑”
01 package com.test; 02 03 import java.util.ArrayList; 04 import java.util.Iterator; 05 import jav ...
- Shell hook
[目的]实现 ll -as +hook+ clear Shell脚本及钩子 - CSDN博客 https://blog.csdn.net/shengzhu1/article/details ...
- LOJ#139. 树链剖分
LOJ#139. 树链剖分 题目描述 这是一道模板题. 给定一棵$n$个节点的树,初始时该树的根为 1 号节点,每个节点有一个给定的权值.下面依次进行 m 个操作,操作分为如下五种类型: 换根:将一个 ...
- 百度dureos CMake Error
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, ...
- 对 block 内存管理的一些看法
首先交代一下retain cycle ,和 产生retain cycle后我们应该怎么处理. 1.retain cycle在block中是极易产生,block就是一段可以灵活使用的代码,你可以把它当做 ...
- Spring Boot 访问静态资源
方法1一: 在resources目录下建立static的目录,将静态资源放到此处,可以直接访问 访问:127.0.0.1:9010/img/123.png