zoukankan      html  css  js  c++  java
  • (最小路径覆盖) poj 2446

    E - Chessboard
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below). 

    We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below: 
    1. Any normal grid should be covered with exactly one card. 
    2. One card should cover exactly 2 normal adjacent grids. 

    Some examples are given in the figures below: 

    A VALID solution.


    An invalid solution, because the hole of red color is covered with a card.


    An invalid solution, because there exists a grid, which is not covered.

    Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.

    Input

    There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.

    Output

    If the board can be covered, output "YES". Otherwise, output "NO".

    Sample Input

    4 3 2
    2 1
    3 3
    

    Sample Output

    YES

    Hint


    A possible solution for the sample input.
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<string>
    #include<queue>
    #include<vector>
    #include<stack>
    using namespace std;
    int mp[1230][1230],link[1230],mark[1230],g[35][35],ans,opt[35][35];
    int n,m,k,temp;
    bool dfs(int x)
    {
        for(int i=1;i<=temp;i++)
        {
            if(mark[i]==-1&&mp[x][i])
            {
                mark[i]=1;
                if(link[i]==-1||dfs(link[i]))
                {
                    link[i]=x;
                    return true;
                }
            }
        }
        return false;
    }
    int main()
    {
        int x,y;
        while(scanf("%d%d%d",&n,&m,&k)!=EOF)
        {
            temp=0;
            ans=0;
            memset(link,-1,sizeof(link));
            memset(g,0,sizeof(g));
            memset(opt,0,sizeof(opt));
            for(int i=1;i<=k;i++)
            {
                scanf("%d%d",&y,&x);
                opt[x][y]=1;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(!opt[i][j])
                    {
                        g[i][j]=++temp;
                    }
                }
            }
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(g[i][j]!=0)
                    {
                        if(i>1&&g[i-1][j]!=0)
                            mp[g[i][j]][g[i-1][j]]=1;
                        if(i<n&&g[i+1][j]!=0)
                            mp[g[i][j]][g[i+1][j]]=1;
                        if(j>1&&g[i][j-1]!=0)
                            mp[g[i][j]][g[i][j-1]]=1;
                        if(j<m&&g[i][j+1]!=0)
                            mp[g[i][j]][g[i][j+1]]=1;
                    }
                }
            }
            for(int i=1;i<=temp;i++)
            {
                memset(mark,-1,sizeof(mark));
                if(dfs(i))
                    ans++;
            }
            if(ans==temp)
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    ETL利器Kettle实战应用解析系列三 【ETL后台进程执行配置方式】
    ETL利器Kettle实战应用解析系列二 【应用场景和实战DEMO下载】
    Kettle使用介绍
    java反射详解
    request详解
    java访问接口
    原生JS写Ajax的请求函数-原生ajax
    阿拉伯数字金额转换为大写
    深入理解Java中的String
    Strust2中,加入监听器来判断用户是否在session中存在。
  • 原文地址:https://www.cnblogs.com/water-full/p/4460965.html
Copyright © 2011-2022 走看看