ZOJ1654 ZOJ那个___不让我过!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define For(i,j,k) for (i=j;i<=k;i++)
using namespace std;

const int dmax=1010;
int a[dmax][dmax],c1[dmax][dmax],c2[dmax][dmax],b[dmax][dmax];
int d[dmax],e,e1,fg,flag;
bool p[dmax];

int update(char c){
	switch (c){
		case '#':return 2;
		case '*':return 1;
		case 'o':return 0;
	}
	return -1;
}
bool dfs(int x){
	int i;
	For(i,1,e1)
		if (!p[i] && b[x][i]){
			p[i]=1;
			if (!d[i] || dfs(d[i])){
				d[i]=x;
				return 1;
			}
		}
	return 0; 
}

int main(){
	int i,j,k,m,n,id=0,T;
	scanf("%d",&T);
while (T--){
	memset(c1,0,sizeof(c1));
	memset(c2,0,sizeof(c2));
	memset(b,0,sizeof(b));
	memset(d,0,sizeof(d));
	memset(a,0,sizeof(a));
	e=e1=0;
	scanf("%d%d",&n,&m);
	getchar();
	For(i,1,n){
		For(j,1,m)
			a[i][j]=update(getchar());
		getchar();
	}
	For(i,1,n)
		For(j,1,m)
			if (!c1[i][j] && a[i][j]==0){
				e++;
				k=j;
				while (k>=1 && a[i][k]!=2){
					c1[i][k]=e;
					k--;
				}
				while (j<=n && a[i][j]!=2){
					c1[i][j]=e;
					j++;
				}
			}
	For(j,1,m)
		For(i,1,n)
			if (!c2[i][j] && a[i][j]==0){
				e1++;
				k=i;
				while (k>=1 && a[k][j]!=2){
					c2[k][j]=e1;
					k--;
				}
				while (i<=n && a[i][j]!=2){
					c2[i][j]=e1;
					i++;
				}
			}
	For(i,1,n)
		For(j,1,m)
			if (a[i][j]==0)
				b[c1[i][j]][c2[i][j]]=1;
	int ans=0;
	For(i,1,e){
		For(j,1,e1)
			p[j]=0;
		dfs(i);
	}
	memset(p,0,sizeof(p));
	For(i,1,e1)
		if (d[i])
			ans++;
    printf("Case :%d\n",++id);
	printf("%d\n",ans);
}
	return 0;
}

你可能感兴趣的:(ZOJ1654 ZOJ那个___不让我过!)