CODEFORCES 515D Drazil and Tiles <路径dfs + 跳格>

题目:http://codeforces.com/problemset/problem/515/D

题意:给你一张方格,叫你用1X2填充方格,有些方格已经被占了(用‘*’),没被占的('.')。竖起来填,用 "^v"表示,横起来,用”<>"表示。如果放的方法独一无二,输出放好的方格图。否则输出“Not unique".

分析:dfs一下,一对一对地找。
Input
3 3
...
.*.
...
Output
Not unique
Input
4 4
..**
*...
*.**
....
Output
<>**
*^<>
*v**
<><>
#include <bits/stdc++.h>

using namespace std;

int const MAX = 2010;
char s[MAX][MAX];
int n, m, cnt;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void dfs(int x, int y)
{
    if(x < 1 || x > n || y < 1 || y > m || s[x][y] != '.')
        return;
    int dir = -1, sum = 0;
    for(int i = 0; i < 4; i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(s[xx][yy] == '.')
        {
            sum ++;
            dir = i;
        }
    }
    if(sum == 1)
    {
        if(dir == 0)
        {
            s[x][y] = '<';
            s[x][y + 1] = '>';
        }
        else if(dir == 1)
        {
            s[x][y] = '>';
            s[x][y - 1] = '<';
        }
        else if(dir == 2)
        {
            s[x][y] = '^';
            s[x + 1][y] = 'v';
        }
        else if(dir == 3)
        {
            s[x][y] = 'v';
            s[x - 1][y] = '^';
        }
        cnt += 2;
        for(int i = 0; i < 4; i++)
            dfs(x + dx[dir] + dx[i], y + dy[dir] + dy[i]);
    }
}

void init()
{
    cnt = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(s[i][j] == '*')
                cnt ++;
}

int main()
{
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%s", s[i] + 1);
    init();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            dfs(i, j);
    if(cnt == n * m)
    {
        for(int i = 1; i <= n; i++)
        {
            printf("%s\n", s[i] + 1);
        }
    }
    else
        printf("Not unique\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值