codeforces - 377A 题解

题目大意:给定一个n*m区域,#为墙,“.”为空地,填充k片空地,同时使得剩下的空地继续保持连通

题目解法:DFS遍历sum-k片空地(sum是空地总数),然后枚举一遍,没有被遍历过的空地全部填充

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<climits>
 5 #include<cstring>
 6 using namespace std;
 7 int map[502][502];
 8 bool vis[502][502];
 9 int n,m,k;
10 int sum=0;
11 int amount=0;
12 void dfs(int x,int y)
13 {
14     vis[x][y]=true;
15     amount+=1;
16     if(amount==sum-k)
17     {
18         return;
19     }
20     if(!vis[x-1][y]&&map[x-1][y]&&amount!=sum-k)
21     {
22         dfs(x-1,y);
23     }
24     if(!vis[x+1][y]&&map[x+1][y]&&amount!=sum-k)
25     {
26         dfs(x+1,y);
27     }
28     if(!vis[x][y-1]&&map[x][y-1]&&amount!=sum-k)
29     {
30         dfs(x,y-1);
31     }
32     if(!vis[x][y+1]&&map[x][y+1]&&amount!=sum-k)
33     {
34         dfs(x,y+1);
35     }
36     return;
37 }
38 int main()
39 {
40
41     scanf("%d%d%d",&n,&m,&k);
42     memset(map,0,sizeof(map));
43     memset(vis,false,sizeof(vis));
44     for(int i=1;i<=n;i++)
45     {
46         getchar();
47         for(int j=1;j<=m;j++)
48         {
49             char c=getchar();
50             if(c==‘#‘)
51             {
52                 map[i][j]=0;
53             }
54             else
55             {
56                 map[i][j]=1;
57                 sum+=1;
58             }
59         }
60     }
61     for(int i=1;i<=n&&amount!=sum-k;i++)
62     {
63         for(int j=1;j<=m;j++)
64         {
65             if(map[i][j])
66             {
67                 dfs(i,j);
68                 break;
69             }
70         }
71     }
72     for(int i=1;i<=n;i++)
73     {
74         for(int j=1;j<=m;j++)
75         {
76             if(!map[i][j])
77             {
78                 printf("#");
79             }
80             else
81             {
82                 if(vis[i][j])
83                 {
84                     printf(".");
85                 }
86                 else
87                 {
88                     printf("X");
89                 }
90             }
91         }
92         printf("\n");
93     }
94     return 0;
95 }
时间: 2024-08-10 17:18:53

codeforces - 377A 题解的相关文章

CodeForces Dubstep 题解

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them. Let's assume that a song consists of some number of word

codeforces Towers 题解

Little Vasya has received a young builder's kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same. Vasya wants to construct the minimal number

codeforces 1296 题解(更新中)

codeforces 1296 题解 A. Array with Odd Sum 想要数组加和为奇数,可以从数组长度是奇数还是偶数着手 若数组长度为偶数,则数组全奇或全偶必定不能构造满足题目要求的数列 若数组长度为奇数,则数组全偶必定不能构造满足题目要求的数列 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; #define _for(i,a,b) for(int i =

codeforces 1303 题解(更新中)

codeforces 1303 题解 A. Erasing Zeroes 想让字符串中的 \(1\) 连续,而我们能做的只有删 \(0\) ,则需要删去除开头以及结尾外的 所有 \(0\) 块.所以从头扫一遍统计开头 \(0\) 块,从尾扫一遍统计结尾 \(0\) 块,再用 \(0\) 的数量减去这两部分即可,可能为负所以跟 \(0\) 取最大值. 时间复杂度 \(O(n)\) #include <bits/stdc++.h> using namespace std; typedef long

codeforces 377A. Puzzles 水题

A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/337/A Description The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to p

codeforces#536题解

CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and you bought a matrix with lots of "crosses". This matrix \(M\) of size \(n \times n\) contains only 'X' and '.' (without quotes). The element in t

CodeForces 281 题解

A题: 题意:给出按照时间顺序的比赛记录,比赛记录了哪一分钟有哪位球员得到了黄牌或红牌,输出罚下的人的序列. 题解:直接按照时间读入模拟就可..注意坑在有可能一位球员罚下后又得到黄牌或红牌,这时候不应再输出这个人了. B题: 题意:给出两个摔跤选手每个动作的得分,正数为第一个人得分负数为第二个人得分,总分高者胜,若相同则“字典序”较大的获胜,再相同则最后得分的人获胜. 题解:直接模拟就可..要用long long.. C题: 题意:两个队伍篮球比赛,给出每个队伍投进的每个球距离球框的距离,现在要

codeforces - 448C 题解

题意:给定一个栅栏,每次涂一行或者一列,问最少几次能够涂完 题解:分治算法+DP思想,每次的状态从竖着涂和横着涂中选择,同时向更高的部分递归计算. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<climits> 5 using namespace std; 6 int a[5001]; 7 int f[5001][5001]; 8 void cal(int l

codeforces - 148E 题解

题目大意:一个公主有一个摆满瓷器的架子,她生气的时候就要打碎m个瓷器.这个架子有n层,每层的瓷器每次只能从最左边拿或者从最右边拿,问打碎的瓷器的最大价值. 题解:这是一个泛化物品+分组背包的DP,首先将每一层上拿出瓷器的方案作为一个物品,拿出瓷器的方案的代价是瓷器数量,价值是这一层上所有方案中最大的价值. 首先为了计算方案的时候可以快速计算,我们在读入的时候就计算出前缀和sum,然后在计算方案的时候,每一层上打碎的数量对应一个物品,然后枚举总数量分配到两侧打碎的价格,得到最大价值.最后使用分组背