CODEFORCES 515D Drazil and Tiles <路徑dfs + 跳格>__求解策略:遞推

來源:互聯網
上載者:User

題目: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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.