zoukankan      html  css  js  c++  java
  • poj3020

    匈牙利,似乎还是有多余空格

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 55

    char map[maxn][maxn];
    bool chk[maxn][maxn];
    int match[maxn][maxn];
    int tot, n, m;
    int dir[4][2] =
    {
    {
    0, 1 },
    {
    1, 0 },
    {
    -1, 0 },
    {
    0, -1 } };

    void input()
    {
    scanf(
    "%d%d", &n, &m);
    gets(map[
    0]);
    tot
    = 0;
    for (int i = 0; i < n; i++)
    gets(map[i]);
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    if (map[i][j] == '*')
    tot
    ++;
    }

    bool SearchPath(int x, int y)
    {
    for (int i = 0; i < 4; i++)
    {
    int x1 = x + dir[i][0];
    int y1 = y + dir[i][1];
    if (!chk[x1][y1] && map[x1][y1] == '*')
    {
    chk[x1][y1]
    = true;
    if (match[x1][y1] == -1 || SearchPath(x1 - dir[match[x1][y1]][0],
    y1
    - dir[match[x1][y1]][1]))
    {
    match[x1][y1]
    = i;
    return true;
    }
    }
    }
    return false;
    }

    int MaxMatch()
    {
    int ret = 0;
    memset(match,
    -1, sizeof(match));
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    if (((i + j) & 1) && map[i][j] == '*' && match[i][j] == -1)
    {
    memset(chk,
    0, sizeof(chk));
    if (SearchPath(i, j))
    ret
    ++;
    }
    return ret;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    while (t--)
    {
    input();
    printf(
    "%d\n", tot - MaxMatch());
    }
    return 0;
    }

  • 相关阅读:
    Mutex和Lock
    Thread和Promise以及packaged_task
    async和Future
    《并行程序设计导论》——读书笔记汇总
    UnrealEngine4蓝图可视化编程 完整例子 勘误
    BOOST下载
    sql 解析xml
    AutoResetEvent 笔记2
    ssh免密登录设置 (普通用户和root用户)
    npm设置淘宝镜像
  • 原文地址:https://www.cnblogs.com/rainydays/p/2090422.html
Copyright © 2011-2022 走看看