您的位置:首页 > 其它

D.Lakes in Berland(DFS)

2016-10-06 00:26 281 查看
Lakes in Berland

题意:三个参数:n,m,k,表示n * m 的矩形,‘*’表示的是土地,‘.’表示的是水,如果水四面都被土包围,那就是湖,否则就是海;求:将图中的湖变得剩下K个之后的图,输出,并求出将水变成图的最小的个数;

思路:先将海筛选掉,就是判断边界上有没有‘.’,然后dfs标记一下;之后再对图进行湖的记录,用一个结构体,记录湖的其中的一个点 和 湖中‘.’的个数;之后再进行排序,对point - k 个湖进行变换,输出;

#include<bits/stdc++.h>
using namespace std;
const int maxn = 50 + 5;
int n,m,k;

char s[maxn][maxn];
struct Node
{
int x,y;
int num;
}num[maxn * maxn];
bool vis[maxn][maxn];

int derict[][2] =
{
1,0,
-1,0,
0,-1,
0,1,
};
void flag_dfs(int x, int y)//把海筛掉
{
vis[x][y] = true;
for(int i = 0 ; i < 4 ; i ++)
{
int xx = x + derict[i][0];
int yy = y + derict[i][1];
if(xx >=1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && s[xx][yy] == '.')
{
vis[xx][yy] = true;
flag_dfs(xx,yy);
}
}
}
int temp;
void dfs(int x, int y)//对湖的信息进行记录
{
vis[x][y] = true;
for(int i = 0 ; i < 4 ; i ++)
{
int xx = x + derict[i][0];
int yy = y + derict[i][1];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && s[xx][yy] == '.')
{
temp ++;
vis[xx][yy] = true;
dfs(xx,yy);
}
}
}
bool cmp(struct Node a,struct Node b)
{
return a.num < b.num;
}
void dfss(int x,int y)//改变湖的dfs
{
vis[x][y] = true;
s[x][y] = '*';
for(int i = 0 ; i < 4 ; i ++)
{
int xx = x+ derict[i][0];
int yy = y + derict[i][1];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && s[xx][yy] == '.')
{
vis[xx][yy] = true;
s[xx][yy] = '*';
dfss(xx,yy);
}
}
}
int main()
{
while( ~ scanf("%d%d%d",&n,&m,&k) )
{
memset(vis,false,sizeof(vis));

for(int i = 1 ; i <= n ; i ++)
{
scanf("%s",s[i] + 1);
}
for(int i = 1; i <= n ; i ++)
{
if(s[i][1] == '.' && !vis[i][1])
flag_dfs(i,1);
if(s[i][m] == '.' && !vis[i][m])
flag_dfs(i,m);
}
for(int i = 1; i <= m ;i ++)
{
if(s[1][i] == '.' && !vis[1][i])
flag_dfs(1,i);
if(s
[i] == '.' && !vis
[i])
flag_dfs(n,i);
}
// for(int i = 1 ; i <= n ; i ++)
// {
// for(int j = 1 ;j <= m ; j ++)
// {
// cout << vis[i][j] << " ";
//
// }
// cout << endl;
// }
int point = 0;
for(int i = 1; i <= n ;i ++)
{
for(int j = 1; j <= m ; j ++)
{
if(s[i][j] == '.' && ! vis[i][j])
{
// cout << i << ' ' << j <<endl;
temp = 1;
dfs(i,j);
// cout << vis[2][3] <<endl;
num[point].x = i;
num[point].y = j;
num[point].num = temp;
point ++;
}
}
}
// cout << point <<endl;
sort(num,num + point , cmp);
memset(vis,false,sizeof(vis));
int ans =0;
for(int i = 0 ; i < point - k ; i ++)
{
ans += num[i].num;
dfss(num[i].x,num[i].y);
}
cout << ans << endl;
for(int i = 1; i <= n ; i ++)
{
cout << s[i] + 1 << endl;
}
}
return 0;
}

下面附上同一个code forces写出来的两个水题:

A. The New Year: Meeting Friends

题意:相当于数轴上的三个点,求三点距离;

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
typedef __int64 ll;
int main()
{
int a[10];
while(~ scanf("%d%d%d",&a[0],&a[1],&a[2]))
{
sort(a,a+3);
int ans = 0;
for(int i = 1; i < 3; i++)
{
ans += a[i] - a[i -1];
}
cout << ans <<endl;
}
return 0;
}
B. Text Document Analysis




题意:

求括号之外的单词的最长的长度   和   括号之内的单词的个数;




#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
typedef __int64 ll;
char s[maxn];
int main()
{
int n;
while(~ scanf("%d",&n) )
{
scanf("%s",s);
int len = strlen(s);
int ans1 = 0, ans2 =0;
for(int i =0 ;i < len ;i ++)
{
if(s[i] == '(')
{
int j;
for(j = i + 1; j < len && s[j] != ')';j ++)
{
if(((s[j] >= 'a' && s[j] <= 'z')||(s[j] >= 'A' && s[j] <= 'Z')) && !((s[j +1] >= 'a' && s[j+1] <= 'z')||(s[j +1] >= 'A' && s[j + 1] <= 'Z')))
{
// cout << j <<endl;
ans2 ++;
}
}
i = j;
}
if((s[i] >= 'a' && s[i] <= 'z')|| (s[i] >= 'A' && s[i] <= 'Z') )
{
int j = i + 1;
while(j < len && (((s[j] >= 'a' && s[j] <= 'z')||(s[j] >= 'A' && s[j] <= 'Z'))))
{
j ++;
// cout << j <<endl;
}
// cout <<i <<" fghfgh " <<j <<endl;
if( j - i > ans1 )
ans1 = j -i;
i = j - 1;
}
}
cout << ans1 <<" " << ans2 <<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: