zoukankan      html  css  js  c++  java
  • CF292-D

    D. Drazil and Tiles
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Drazil created a following problem about putting 1 × 2 tiles into an n × m grid:

    "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."

    But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ".

    Drazil found that the constraints for this task may be much larger than for the original task!

    Can you solve this new problem?

    Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

    The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

    Output

    If there is no solution or the solution is not unique, you should print the string "Not unique".

    Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

    Sample test(s)
    input
    3 3
    ...
    .*.
    ...
    output
    Not unique
    input
    4 4
    ..**
    *...
    *.**
    ....
    output
    <>**
    *^<>
    *v**
    <><>
    input
    2 4
    *..*
    ....
    output
    *<>*
    <><>
    input
    1 1
    .
    output
    Not unique
    input
    1 1
    *
    output
    *
    Note

    In the first case, there are indeed two solutions:


    <>^
    ^*v
    v<>

    and


    ^<>
    v*^
    <>v

    so the answer is "Not unique".

    给定一个n*m的网格,其中包含'.'和'*','.'表示可以铺地,'*'表示已被占用

    问是否能用1*2或者2*1的砖块将空闲的地方唯一铺满。铺不满或者有多种方案输出 Not unique。如果能,则输出铺地方案

    比赛时以为和状态压缩dp的铺砖块题类似,然后就按照那种方法想去了,果然就被坑了。。。。

    类似拓扑排序.

    直接考虑唯一铺满的情况.对每个方格求其能铺的方案数,找出能唯一铺的格子,维护一个队列,每次都满足只能唯一铺的格子

    到最后看是否存在还没铺的地,有的话就不能唯一铺满,否则就可以。

    #include <iostream>
    #include <string.h>
    #include <queue>
    using namespace std;
    char str[2050][2050];
    char res[2050][2050];
    int degree[2050][2050];
    int dir1[4]={0,0,1,-1};
    int dir2[4]={1,-1,0,0};
    char c1[4]={'<','>','^','v'};
    char c2[4]={'>','<','v','^'};
    class pa
    {
    public:
        int first;
        int second;
    };
    
    queue<pa> q;
    
    void judge(int i,int j)
    {
        if(str[i][j]=='*')
        {
            degree[i][j]=0;
            return;
        }
        for(int k=0;k<4;k++)
            if(str[i+dir1[k]][j+dir2[k]]=='.')
                degree[i][j]++;
    }
    
    int main()
    {
        int n,m;
        cin>>n>>m;
        memset(degree,0,sizeof(degree));
        for(int i=1;i<=n;i++)
        {
            cin>>str[i];
            for(int j=m;j>=1;j--)
                str[i][j]=str[i][j-1];
            str[i][0]='*';
            strcpy(res[i],str[i]);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                judge(i,j);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(degree[i][j]==1)
                {
                    pa p;
                    p.first=i;
                    p.second=j;
                    q.push(p);
                }
        while(!q.empty())
        {
            pa p=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                if(res[p.first+dir1[i]][p.second+dir2[i]]=='.')
                {
                    res[p.first][p.second]=c1[i];
                    res[p.first+dir1[i]][p.second+dir2[i]]=c2[i];
                    degree[p.first][p.second]=0;
                    degree[p.first+dir1[i]][p.second+dir2[i]]=0;
                    for(int j=0;j<4;j++)
                    {
                        int a,b;
                        a=p.first+dir1[i]+dir1[j];
                        b=p.second+dir2[i]+dir2[j];
                        if(res[a][b]=='.')
                            degree[a][b]--;
                        if(degree[a][b]==1)
                        {
                            pa p;
                            p.first=a;
                            p.second=b;
                            q.push(p);
                        }
                    }
                }
            }
        }
        bool flag=true;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                if(res[i][j]=='.')
                    flag=false;
            }
        if(flag)
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                    cout<<res[i][j];
                cout<<endl;
            }
        else
            cout<<"Not unique"<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    “#ifdef __cplusplus extern "C" { #endif”的定义
    【原创】分布式之redis复习精讲
    python爬虫入门(六) Scrapy框架之原理介绍
    PYTHON面试
    14.Ubuntu基本命令
    python爬虫入门(五)Selenium模拟用户操作
    python爬虫入门(四)利用多线程爬虫
    python爬虫入门(三)XPATH和BeautifulSoup4
    python爬虫入门(一)urllib和urllib2
    7.Ajax
  • 原文地址:https://www.cnblogs.com/wzsblogs/p/4295829.html
Copyright © 2011-2022 走看看