计蒜课 引爆炸弹 并查集

其中用set代替unique可以AC原因未知

解法

本质是在求联通块,用并查集,用暴力。

ACcode

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 1003
int ma[N][N];int fa[N],num[N][N],tot = 0 ;  
int findfa(int x){
	return x == fa[x] ? x : fa[x] = findfa(fa[x]) ; 
}
void unin(int x,int y){
	int xfa = findfa(x); int yfa = findfa(y);
	if ( xfa != yfa ){
		fa[xfa] = yfa; 
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	int n = read(), m = read();
	mset(num,0);	
	rep(i,1,n){
		rep(j,1,m){
			scanf("%1d",&ma[i][j]);
			if( ma[i][j] == 1 )
				num[ i ][ j ] = ++tot; 
		}	
	}	
	rep(i,1,tot)	fa[i] = i; 
	rep(i,1,n){
		bool is = 0 ; int st; 
		rep(j,1,m){
			if ( num[i][j] != 0 ){
				if ( is == 0 ){
					is = 1; 
					st = num[i][j];
				}
				else {
					unin(st,num[i][j]);
				}
			}
		}
		is = 0 ; 
	}
	rep(j,1,m){
		bool is = 0 ; int st; 
		rep(i,1,n){
			if ( num[i][j] != 0 ){
				if ( is == 0 ){
					is = 1; 
					st = num[i][j];
				}
				else {
					unin(st,num[i][j]);
				}
			}
		}
		is = 0; 
	}	
	set<int>s; 
	rep(i,1,tot){
		findfa(i);	
		s.insert(fa[i]);
	}
	cout<<s.size()<<endl; 
	return 0;
}

另外一种解法

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 1003
int n,m; int ma[N][N];int row[N],col[N];
void boom(int x,int y){
	ma[x][y] = 0; 
	if ( row[x] == 0 ){
		row[x] = 1; 
		for (int i = 1; i <= m; i++){
			if ( ma[x][i] == 1 ){
				boom(x,i);
			}
		}
	}
	if ( col[y] == 0 ){
		col[y] = 1; 
		for (int i = 1; i <= n; i++){
			if ( ma[i][y] == 1 ){
				boom(i,y);
			}
		}
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	n = read(), m = read();
	rep(i,1,n){
		rep(j,1,m){
			scanf("%1d",&ma[i][j]);
		}	
	}	
	mset(row,0) ; 
	mset(col,0);
	int sum = 0 ; 
	rep(i,1,n){
		rep(j,1,m){
			if( ma[i][j] == 1 ){
				boom(i,j);				
				sum++;
			}
		}	
	}	
	cout<<sum<<endl; 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值