http://codeforces.com/contest/510/status/B

题目大意 给一个n*m  找有没有相同字母连起来的矩形串

第一种并查集 瞎搞一下

第一次的时候把val开成字符串了 所以wa

改了AC

#include<cstdio>
#include<map>
//#include<bits/stdc++.h>
#include<vector>
#include<stack>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
#include<cstdlib>
#include<climits>
#define PI acos(-1.0)
#define INF 0x3fffffff
using namespace std;
typedef long long ll;
typedef __int64 int64;
const ll mood=1e9+;
const int64 Mod=;
const double eps=1e-;
const int N=;
const int MAXN=1e4+;
typedef int rl;
inline void r(rl&num){
num=;rl f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')num=num*+ch-'',ch=getchar();
num*=f;
}
char ch[N][N];
int val[N][N];
int par[MAXN],sum[MAXN];
int n,m;
char tem;
int dx[]={,},dy[]={,};
void init()
{
int s=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{ val[i][j]=s;//cout<<val[i][j]<<endl;
par[s]=s;
sum[s]=;
s++;
}
}
}
int find(int x)
{
if(x==par[x]) return x;
return par[x]=find(par[x]);
}
bool unite(int x,int y)
{
x=find(x);y=find(y);
if(x==y&&sum[x]>=) return true;
if(x==y) return false;
par[x]=y;
sum[y]+=sum[x];
return false;
}
bool check(int x,int y)
{
if(x>=&&x<n&&y<m&&y>=&&ch[x][y]==tem) return true;
else return false;
}
int main()
{
r(n);r(m);
for(int i=;i<n;i++)
{
scanf("%s",ch[i]);
}
init();
bool mk=false;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
tem=ch[i][j];
for(int k=;k<;k++)
{
int x=dx[k]+i,y=dy[k]+j;
if(check(x,y))
{
if(unite(val[i][j],val[x][y]))
{
mk=true;
break;
}
}
}
if(mk) break;
}
if(mk) break;
}
if(mk) puts("Yes");
else puts("No");
return ;
}
/*
50 50
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYX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XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY
*/

并查集

这个类似联通块 dfs做 首尾相交就输出YES 做一下

#include<cstdio>
char ch[][];
int vis[][];
int flag;
int n,m;
int dx[]={,,-,},dy[]={,-,,};
void dfs(int atx,int aty,int x,int y,char z)
{
if(vis[x][y])
{
flag=;
return ;
}
vis[x][y]=;
int nx,ny;
for(int i=;i<;i++)
{
nx=x+dx[i];ny=y+dy[i];
if(nx<||nx>=n||ny<||ny>=m||z!=ch[nx][ny]||(atx==nx&&ny==aty)) continue;
dfs(x,y,nx,ny,z);
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%s",ch[i]);
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(!vis[i][j])
{
dfs(-,-,i,j,ch[i][j]);
}
if(flag){printf("Yes\n");return ;}
}
printf("No\n");
return ;
}

其实dfs做的很不顺利

还有一个方法 构成矩形块的充要条件 自己的上下左右要有和自己相同的块至少两个 小于两个的块必然不是 可以标记扔掉

这里感叹下高手对细节的处理真的好强 等自己写的时候才知道问题在哪里 仔细看下人家的才恍然大悟 这样才不会越界

就是在n*m的周围搞上一圈空

大致意思就是把不符合的点全部标记并回到1,2点并不是回到起点 1,1点如果满足条件没被标记的话 就会留下1,1点 所以判断ans>1

#include<cstdio>
char ch[][];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
ch[i][j]=getchar();
}
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(ch[i][j]!='%')
{
int tem=;
if(ch[i][j]==ch[i-][j]) tem++;
if(ch[i][j]==ch[i+][j]) tem++;
if(ch[i][j]==ch[i][j-]) tem++;
if(ch[i][j]==ch[i][j+]) tem++;
if(tem<) {ch[i][j]='%';i=j=;} //回到起点
}
}
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(ch[i][j]!='%') ans++;
if(ans>)
{
puts("Yes");
return ;
}
}
puts("No");
return ;
}

神奇的姿势

现在搞一下回到起点的 只要判断有点留下 那么就输出Yes;

#include<cstdio>
char ch[][];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
ch[i][j]=getchar();
}
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(ch[i][j]!='%')
{
int tem=;
if(ch[i][j]==ch[i-][j]) tem++;
if(ch[i][j]==ch[i+][j]) tem++;
if(ch[i][j]==ch[i][j-]) tem++;
if(ch[i][j]==ch[i][j+]) tem++;
if(tem<) {ch[i][j]='%';i=;j=;} }
}
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(ch[i][j]!='%')
{
puts("Yes");
return ;
}
}
puts("No");
return ;
}

理解版神奇的姿势

Codeforces Round #290 (Div. 2) _B找矩形环的三种写法的更多相关文章

  1. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  2. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  4. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  5. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  6. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  7. Codeforces Round #242 (Div. 2)C(找规律,异或运算)

    一看就是找规律的题.只要熟悉异或的性质,可以秒杀. 为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法. 一些性质如下: 交换律: 结合律: 恒等律: 归零律: 典 ...

  8. Codeforces Round #347 (Div.2)_B. Rebus

    题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...

  9. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

随机推荐

  1. java集合框架之聚合操作stream

    参考http://how2j.cn/k/collection/collection-aggregate/702.html#nowhere 聚合操作 JDK8之后,引入了对集合的聚合操作,可以非常容易的 ...

  2. 3、webpack打包出的文件解析

    分析打包后的结果,看看打包后的结果是什么东西 把打包后的结果.注释什么的删删‘’ 当前是一个匿名函数. 默认的时候会执行,执行的时候会传一个对象,对象有几部分,第一部分是我们的key.第二部分是我们的 ...

  3. Swift 4.0 中的 open,public,internal,fileprivate,private

    1.private private访问级别所修饰的属性或者方法只能在当前类里访问. 2.fileprivate fileprivate访问级别所修饰的属性或者方法在当前的Swift源文件里可以访问. ...

  4. AtCoder Grand Contest 005【A栈模拟,B单调栈】

    挖草,AtCoder实在是太吊了~ %%%,目前只A了两题: A题: 就是利用栈模拟一下就好了:S进栈,T的话有S就出栈,然后len减一下就好了: #include <bits/stdc++.h ...

  5. 2016 Noip提高组

    2557. [NOIP2016]天天爱跑步 ★★☆   输入文件:runninga.in   输出文件:runninga.out   简单对比时间限制:2 s   内存限制:512 MB [题目描述] ...

  6. Codevs 1312 连续自然数和

    1312 连续自然数和 题目描述 Description 对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.eg:1998+1999+2000+2001+2002=10 ...

  7. StringUtils中常用方法leftPad(),rightPad(),center()

    org.apache.commons.lang3的StringUtils 方法如下: public static String leftPadTime(Integer time){    return ...

  8. python-selenium-robotframework安装问题

    背景 当前系统安装了两个不同版本的python,分别是python27和python36(如图1),如图 说明 系统安装的两个python版本,python2中的python.exe默认不做修改:py ...

  9. UVA10140 Prime Distance【素数/数论】By cellur925

    题目传送门 我们注意到,L,R是肥肠大的.........我们不可能在1s内筛出2^31内的全部质数. “上帝为你关上一扇门,同时为你打开一扇窗” 我们又注意到,R-L是肥肠比较小的,珂以从这入手解决 ...

  10. VLAN-5-802.1Q-in-Q隧道

    Q-in-Q允许SP在跨越WAN服务时,保留802.1Q VLAN标签.由此,VLAN可以被拓展到多个地理分散的站点上.     入向SP交换机收到802.1Q数据帧,使用额外的802.1Q头部来标记 ...