Drazil and Tiles CodeForces - 515D(思维+拓扑排序)这题强

题目链接

div2 的D题,比较好的一道拓扑排序了,一开始也没有发现咋用拓扑排序做。。

题意:铺砖,砖的大小为1 X 2. 然后要求你在给定的区域内为.的填充上砖,左右填充<>,上下填充^v;

题意分析:

1.这道题可以用拓扑排序做,可以遍历个点,找出其周围‘.’的个数。

2.如果周围'.'点的个数为1则必须对其进行安排。

3.不断拓扑遍历,直到结束,如果还有.则方法不为1

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;

typedef pair <int, int> P;
int n, m, re[maxn][maxn];
int nx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char ch[maxn][maxn];
void init()
{
    memset(re, 0, sizeof(re));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            for(int k = 0; k < 4; k++)
    {
        int tx = i + nx[k][0], ty = j + nx[k][1];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m && ch[tx][ty] == '.')
            re[i][j]++;
    }
}

void tosort()
{
    queue <P> que;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
    {
        if(re[i][j] == 1 && ch[i][j] == '.')
        {
            P a; a.first = i; a.second = j;
            que.push(a);
        }
    }

    while(!que.empty())
    {
        P a = que.front(); que.pop();
        for(int i = 0; i < 4; i++)
        {
            int tx = a.first + nx[i][0], ty = a.second + nx[i][1];

            if(tx >= 0 && tx < n && ty >= 0 && ty < m && ch[tx][ty] == '.')
            {
//                cout<<"tx = "<<tx<<"ty = "<<ty<<endl;
                for(int j = 0; j < 4; j++)
                {
                    int dx = tx + nx[j][0], dy = ty + nx[j][1];
                    if(dx >= 0 && dx < n && dy >= 0 && dy < m && (--re[dx][dy] == 1) && ch[dx][dy] == '.')
                    {
                        P b; b.first = dx; b.second = dy;
                        que.push(b);
                    }
                }
                if(nx[i][0] == 0 && nx[i][1] == 1)
                {
                    ch[a.first][a.second] = '<';
                    ch[tx][ty] = '>';
                }
                if(nx[i][0] == 0 && nx[i][1] == -1)
                {
                    ch[a.first][a.second] = '>';
                    ch[tx][ty] = '<';
                }
                if(nx[i][0] == -1 && nx[i][1] == 0)
                {
                    ch[a.first][a.second] = 'v';
                    ch[tx][ty] = '^';
                }
                if(nx[i][0] == 1 && nx[i][1] == 0)
                {
                    ch[a.first][a.second] = '^';
                    ch[tx][ty] = 'v';
                }
                break;
            }
        }
    }
    int flag = 0;
     for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
        {
            if(ch[i][j] == '.')
            {
//                printf("ch[i][j] = ");
//                cout<<"i = "<<i<<"j = "<<j<<" = ";
//                cout<<ch[i][j]<<endl;
                printf("Not unique\n");
                flag = 1;
                return;
            }
        }

//    cout<<"--------"<<endl;
    for(int i = 0; i < n; i++)
        printf("%s\n", ch[i]);

}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        for(int i = 0; i < n; i++)
            scanf("%s", ch[i]);
//        for(int i = 0; i < n; i++)
//            printf("%s\n",ch[i]);
//        printf("\n");
        init();
        tosort();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值