POJ 2446解题报告

Chessboard

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14263 Accepted: 4436

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.

Source

POJ Monthly,charlescpp

           这道题与之前的POJ 3020题比较类似,方法几乎是相同的。下面是参考代码。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define pb push_back
#define CLR(x) memset(x,0,sizeof(x))
#define __CLR(x) memset(x,-1,sizeof(x))
using namespace std;

int n,m,k;
int g[40][40],match[1500];
vector<int>G[1500];
bool vis[1500];
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};

bool dfs(int u)
{
    for(int i=0; i<G[u].size(); i++)
    {
        int t=G[u][i];
        if(!vis[t])
        {
            vis[t]=1;
            if(match[t]==-1||dfs(match[t]))
            {
                match[t]=u;
                return true;
            }
        }
    }
    return false;
}

int main()
{

    while(~scanf("%d%d%d",&n,&m,&k))
    {
        CLR(g);
        for(int i=0; i<k; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[v][u]=-1;
        }
        int cnt=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(g[i][j]==0)
                {
                    g[i][j]=++cnt;
                }
            }
        }
        if(cnt%2==1)
            printf("NO\n");
        else
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(g[i][j]!=-1)
                    {
                        for(int k=0; k<4; k++)
                        {
                            int x=i+dx[k],y=j+dy[k];
                            if(x>=1&&x<=n&&y>=1&&y<=m&&g[x][y]!=-1)
                            {
                                G[g[i][j]].pb(g[x][y]);
                                G[g[x][y]].pb(g[i][j]);
                            }
                        }
                    }
                }
            }
            __CLR(match);
            int num=0;
            for(int i=1; i<=cnt; i++)
            {
                CLR(vis);
                if(dfs(i))
                    num++;
            }
            if(num==cnt) printf("YES\n");
            else printf("NO\n");
        }
        for(int i=0; i<1500; i++)
            G[i].clear();
    }
}

POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值