hdu 1045 Fire Net (与 上一题 zoj 1654 如出一辙)

来源:互联网 发布:excel 2016数据有效性 编辑:程序博客网 时间:2024/06/08 05:42
/* * Subject : Hungary hdu 1045 * Author  :a_clay * Created : 2012-06-07 */#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <algorithm>//#include <conio.h>using namespace std;const int N = 5;const int M = 26;char str[N][N];int p[N][N];int rp[N][N];int map[M][M];bool vis[M];int pre[M];int n;int x_id, y_id;bool dfs(int u) {    int i;    for(i = 1; i <= y_id; i++) {        if(map[u][i] && !vis[i]) {            vis[i] = 1;            if(!pre[i] || dfs(pre[i])) {                pre[i] = u;                return true;            }        }    }    return false;}int MaxMatch() {    int i, num = 0;    for(i = 1; i <= x_id; i++) {        memset(vis, 0, sizeof(vis));        if(dfs(i)) num++;    }    return num;}int main() {    int T, i, j, k, ca = 0;    while(~scanf("%d", &n), n) {        memset(p, 0, sizeof(p));        memset(rp, 0, sizeof(rp));        scanf("%d", &n); // m 行 , n 列        x_id = 0, y_id = 0;        for(i = 0; i < n; i++) {            scanf("%s", str[i]);        }        for(i = 0; i < n; i++) { // 管理行的。。。            int k1 = 0;            while(k1 < n) {                if(str[i][k1] == '.') {                    ++x_id;                    while(k1 < n && str[i][k1] != 'X') {                        p[i][k1] = x_id;                        k1++;                    }                }                else k1++;            }                        int k2 = 0;            while(k2 < n) {                if(str[k2][i] == '.') {                    ++y_id;                    while(k2 < n && str[k2][i] != 'X') {                        rp[k2][i] = y_id;                        k2++;                    }                }                else k2++;            }        }        memset(map, 0, sizeof(map));        for(i = 0; i < n; i++) {            for(j = 0; j < n; j++) {                if(str[i][j] == '.') {                    map[p[i][j]][rp[i][j]] = 1;                }            }        }        memset(pre, 0, sizeof(pre));        printf("%d\n", MaxMatch());    }    //getch();    //system("pause");    return 0;}


 

原创粉丝点击