# P1524 十字绣

## 输入输出样例

4 5
.....
.\...
..\..
.....
.....
....\
.\X..
.....


4思路：然而我并不会(⊙o⊙)…可能是并查集吧！
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,num,sum,ans;
int map[300][300];
char s[3][201][201];
int b[50000],c[50000];
int fa[50000],val[50000],vis[50000];
int find(int x){
if(fa[x]==x)    return x;
else return fa[x]=find(fa[x]);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[1][i]);
for(int i=1;i<=n;i++)
scanf("%s",s[2][i]);
for(int k=1;k<=2;k++)
for(int i=1;i<=n;i++)
for(int j=0;j<m;j++){
if(s[k][i][j]=='/'||s[k][i][j]=='X'){
if(map[j+2][i]==0){
num++;
fa[num]=num;
map[j+2][i]=num;
}
if(map[j+1][i+1]==0){
num++;
fa[num]=num;
map[j+1][i+1]=num;
}
val[map[j+2][i]]+=2*k-3;
val[map[j+1][i+1]]+=2*k-3;
int dx=find(map[j+2][i]);
int dy=find(map[j+1][i+1]);
if(dx!=dy)    fa[dy]=dx;
}
if(s[k][i][j]==92||s[k][i][j]=='X'){
if(map[j+1][i]==0){
num++;
fa[num]=num;
map[j+1][i]=num;
}
if(map[j+2][i+1]==0){
num++;
fa[num]=num;
map[j+2][i+1]=num;
}
val[map[j+1][i]]+=2*k-3;
val[map[j+2][i+1]]+=2*k-3;
int dx=find(map[j+1][i]);
int dy=find(map[j+2][i+1]);
if(dx!=dy)    fa[dy]=dx;
}
}
for(int i=1;i<=num;i++){
int dx=find(i);
if(vis[dx]==0){
sum++;
c[sum]=dx;
vis[dx]=1;
}
b[dx]=b[dx]+abs(val[i]);
}
for(int i=1;i<=sum;i++){
if(b[c[i]]==0)    b[c[i]]=1;
ans=ans+(b[c[i]]+1)/2;
}
cout<<ans;
}


posted @ 2017-09-23 21:05  一蓑烟雨任生平  阅读(323)  评论(0编辑  收藏  举报