传送门:D. Monopole Magnets

这一场也是很神奇了,先是推迟三天,后是评测鸡崩了,unrated...

题意:每一行,每一列必须都要至少有一个s,n要可以到所有的黑格,n的上下左右如果有一个s,他就可以往那个方向移动一格,n最后不能在白格,求n最少的个数。

题解:

1、每一行每一列如果有#,#必须是连续的,否则输出-1

2、如果有一行没有#,那么至少有一列要没有#,s就可以放在交点处,否则输出-1。

3、求解连通块的个数

 1 #include<bits/stdc++.h>
2 #define ll long long
3 using namespace std;
4
5 char a[1010][1010];
6 bool x[1010],y[1010]; //x记录有#的行,y记录有#的列
7 int dx[]={1,-1,0,0};
8 int dy[]={0,0,1,-1};
9
10 void dfs(int xx,int yy)
11 {
12 a[xx][yy]='.';
13 for(int i=0;i<4;i++){
14 int xxx=xx+dx[i];
15 int yyy=yy+dy[i];
16 if(a[xxx][yyy]=='#') dfs(xxx,yyy);
17 }
18 return ;
19 }
20
21 int main()
22 {
23 ios::sync_with_stdio(false);
24 cin.tie(0);
25 cout.tie(0);
26 int n,m;
27 cin>>n>>m;
28 for(int i=0;i<n;i++) cin>>a[i];
29 int flag=1;
30 for(int i=0;i<n;i++){
31 for(int j=0;j<m;j++){
32 if(a[i][j]=='#') {
33 if(x[i]&&j&&a[i][j-1]!='#'){
34 flag=0;
35 break;
36 }
37 if(y[j]&&i&&a[i-1][j]!='#'){
38 flag=0;
39 break;
40 }
41 x[i]=1,y[j]=1;
42 }
43 }
44 if(!flag) break;
45 }
46 int p=0,q=0;
47 for(int i=0;i<n;i++) if(!x[i]) p=1;
48 for(int i=0;i<m;i++) if(!y[i]) q=1;
49 if(p^q) flag=0; //如果有行没有# 而没有列没有#或者反过来,就输出-1.
50 if(!flag) {cout<<-1<<endl;return 0;}
51 int ans=0;
52 for(int i=0;i<n;i++){
53 for(int j=0;j<m;j++){
54 if(a[i][j]=='#') ans++,dfs(i,j); //简单的找连通块
55 }
56 }
57 cout<<ans<<endl;
58 return 0;
59 }

Codeforces 1345 D - Monopole Magnets的更多相关文章

  1. Codeforces Round #330 (Div. 1) C. Edo and Magnets 暴力

    C. Edo and Magnets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594/pr ...

  2. CodeForces - 344A Magnets (模拟题)

    CodeForces - 344A id=46664" style="color:blue; text-decoration:none">Magnets Time ...

  3. codeforces Magnets

    link:http://codeforces.com/contest/344/problem/A 这道题目很简单. 把输入的01 和10 当做整数,如果相邻两个数字相等的话,那么就属于同一组,否则,就 ...

  4. Codeforces 344A Magnets

    Description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dom ...

  5. coderforces Gym 100803A/Aizu 1345/CSU 1536/UVALive 6832 Bit String Reordering(贪心证明缺)

    Portal: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1345  http://codeforces.com/gym/100 ...

  6. Codeforces Round #639 (Div. 2)

    Codeforces Round #639 (Div. 2) (这场官方搞事,唉,just solve for fun...) A找规律 给定n*m个拼图块,每个拼图块三凸一凹,问能不能拼成 n * ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. jquery表格插件Datatables使用、快速上手

    Datatables使用 一.简介 官网:https://datatables.net/ 中文官网:http://datatables.club/ Datatables是一款jquery表格插件.它是 ...

  2. centos 安装 部署 gitlab github

    https://www.cnblogs.com/wenwei-blog/p/5861450.html 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 ...

  3. nodejs事件和事件循环详解

    目录 简介 nodejs中的事件循环 phase详解 timers pending callbacks idle, prepare poll轮询 check close callbacks setTi ...

  4. 【Linux】md5sum 生产所有文件的md5值,并对照目标文件是否相同

    现在加入有很多很多文件需要测试md5,想看下是否都传输成功了,如何批量生成文件的md5并且逐条对照呢? 下面来简单介绍下 md5sum这个命令有一个选项"-c" 这个选项的意思是c ...

  5. P1140 相似基因(字符串距离,递推)

    题目链接: https://www.luogu.org/problemnew/show/P1140 题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了44种核苷酸,简记作A,C,G,TA,C, ...

  6. VMware下安装Ubantu 18.04

    一.VIM安装及配置 1.安装VIM sudo apt-get install vim 二.拼音输入法以及搜狗拼音输入法安装 1.安装Fcitx输入框架 sudo apt-get install fc ...

  7. Nginx的简介和使用nginx实现请求转发

    一.什么是Nginx Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为H ...

  8. 中间件:ElasticSearch组件RestHighLevelClient用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.基础API简介 1.RestHighLevelClient RestHighLevelClient的API作为ElasticSearch备 ...

  9. uniapp根据登录用户的角色动态的改变tabBar的数量和内容

    此文章借鉴于https://blog.csdn.net/fuyuumiai/article/details/109746357,在此基础上修改小部分内容,适用于我这种uniapp小白 介绍: 现在我们 ...

  10. ovs-actions

    1. 端口说明 OVS支持如下的标准OpenFlow端口名称(括号中是端口号): in_port (65528 or 0xfff8; 0xfffffff8) table (65529 or 0xfff ...