codeforces D - Arthur and Walls

这题说的是给了一个矩阵,必须让.连接起来的图形变成矩形,2000*2000的矩形,那么我们就可以知道了,只要是存在一个有点的区域应该尽量将他削为矩形,那么将这个图形进行缩放,最后我们知道只要存在一个2*2 的矩形中有1个是*就必须将这个*号去掉。 采用bfs去做

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <cstdio>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn = 2005;
 8 char ma[maxn][maxn];
 9 int num[maxn][maxn];
10 bool inq[maxn*maxn];
11 int tx[]={  0,-1, -1, -1, 0, 1, 1, 1 ,0};
12 int ty[]={ -1,-1,  0,  1, 1, 1, 0,-1 , -1};
13 int main()
14 {
15       int n,m;
16      while(scanf("%d%d",&n,&m)==2){
17             queue<int>Q;
18          for(int i=0; i<n; i++){
19             scanf("%s",ma[i]);
20             for(int j=0; j<m; j++)
21                  if(ma[i][j]==‘.‘) {
22                    Q.push(i*m+j);
23                  }
24           }
25          while(!Q.empty()){
26              int id = Q.front(); Q.pop();
27              int xx = id/m, yy=id%m;
28              for(int i =0; i<8; i+=2){
29                  int sum=0,loc=-1;
30                  for(int j=i; j<i+3; j++){
31                         int dx = xx + tx[j];
32                  int dy = yy + ty[j];
33                  if(dx<0||dx>=n ||dy>=m||dy<0)
34                     sum=4;
35                  if(ma[dx][dy]==‘*‘)
36                     sum++,loc=dx*m+dy;
37                  if(sum>1)break;
38                  }
39                  if(sum==1){
40                          ma[loc/m][loc%m]=‘.‘; Q.push(loc);
41                  }
42              }
43          }
44          for(int i=0; i<n; i++)
45              printf("%s\n",ma[i]);
46      }
47     return 0;
48 }
时间: 2024-08-09 22:00:50

codeforces D - Arthur and Walls的相关文章

CodeForces 525D Arthur and Walls

广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳过 改完之后,检查周围8个点是否是元素块,如果有新产生的元素块,那么压入队列. 这样操作完之后就是答案. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include&l

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1

codeforces 525 D Arthur and Walls

题意: 给出一个n*m的表格,里面有'*'和'.',求把最少的'*'移除掉,使得'.'所在的连通块是矩形. 限制: 1 <= n,m <= 2000 思路: 2*2地考虑,如果2*2的格子里只有一个'*',说明这个'*'要去掉,其他情况都不用去掉.然后去掉这个'*'后,又会对其他四个格子有影响. 复杂度好难估计. /*codeforces 525 D Arthur and Walls 题意: 给出一个n*m的表格,里面有'*'和'.',求把最少的'*'移除掉,使得'.'所在的连通块是矩形. 限

Codeforces Round #297 (Div. 2) D题. Arthur and Walls(BFS)

题目地址:Arthur and Walls 这题有一个脑洞,对于当前的点(i,j)并且此点为"*"来说,若存在包含它的2*2正方形中除了它自己外,另外三个点都是".",那么这个点就必须要变成".".由于去掉这个点之后会对周围的8个点造成影响,所以可以用BFS去搜.WA第12组的应该是只考虑了会影响到周围的4个点了. 代码如下: #include <iostream> #include <string.h> #include

D. Arthur and Walls (CF 525 D 搜索bfs)

D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of

CodeForces 525D D. Arthur and Walls(BFS)

题目链接:http://codeforces.com/problemset/problem/525/D 题意:n*m的格子,'*'代表墙壁,'.'代表房间,要求房间都必须是矩形,输出改动后的 n*m: 思路:看了官方题解,思路蛮巧妙的.因为要求一定是矩形,所有在每个2*2的格子里,若有3个'.'和1个'*',那么就将'*'改成'.',这样就能确保房间一定是矩形了. 代码如下: #include<cstdio> #include<cstring> #include<iostre

codeforces MUH and Cube Walls

题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出b串在a串中出现几次! 思路: KMP变形!如何转换成KMP求解呢? 举一个例子说明一下: a: 5 10 8 10 11 9 11 12 10 15 b: 4 2 4 5 3 根据题意 a中的 10 8 10 11 9 与 b是匹配的, 11 9 11 12 10跟b也是匹配的! 如何将b串以及

Codeforces 557C Arthur and Table 砍桌腿

题意:有n个桌腿,要砍掉某些桌腿使得剩下的桌腿能支撑桌子.规定剩下的桌腿中长度最大的桌腿的数量如果超过一半即可支撑桌子.砍掉每个桌腿需要付出代价.求最小的代价和. 枚举.假如最后剩下的桌腿的最大长度为lenth,这样长度的桌腿有num个.那么长度大于lenth的桌腿肯定都被砍去了,然后在剩下的桌腿中按照代价从大到小选择num - 1个桌腿留下来(不砍),将剩余的再砍去,这样一定是最小的代价和.当然也不是无脑枚举,能枚举是因为代价种类很少,才200个.首先我们按桌腿的长度排序,然后枚举每种长度作为

Codeforces 508E Arthur and Brackets

题意: 给出括号序列中每个右括号可能离对应左括号多远  求这个括号序列 思路: 记忆化搜索解决  用f[l][r]表示对于第l个左括号到第r个左括号区间最前面的左括号与其对应右括号的距离 状态只有n^2个  不用担心TLE 求f[l][r]的方法为  如果最前的左括号可以包住l+1~r个括号就尝试包起来  否则将l~r分治为l~x和x+1~r两个子问题 代码: #include<cstdio> #include<iostream> #include<cstring> #