• UVa-11694 Gokigen Naname


      1 #include <bits/stdc++.h>
      2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
      3 const int maxn = 9;
      4 using namespace std;
      5 
      6 int n;
      7 int target[maxn][maxn];
      8 int cur[maxn][maxn];
      9 char rnt[maxn][maxn];
     10 
     11 int root[maxn*maxn];
     12 void initRoot()
     13 {
     14     _for(i,0,(n+1)*(n+1))
     15     root[i] = i;
     16 }
     17 int _find(int x)
     18 {
     19     return x == root[x] ? x : _find(root[x]);
     20 }
     21 
     22 bool judge2(int z)
     23 {
     24     int x = z/n;
     25     int y = z%n;
     26     if(target[x][y]!=-1 && cur[x][y]!=target[x][y]) return false;
     27     if(x==n-1 && target[x+1][y]!=-1 && cur[x+1][y]!=target[x+1][y]) return false;
     28     if(y==n-1 && target[x][y+1]!=-1 && cur[x][y+1]!=target[x][y+1]) return false;
     29     if(x==n-1 && target[x+1][y+1]!=-1 && y==n-1 && cur[x+1][y+1]!=target[x+1][y+1]) return false;
     30     return true;
     31 }
     32 
     33 bool dfs(int z)
     34 {
     35     if(z==n*n) return true;
     36     int x = z/n;
     37     int y = z%n;
     38     rnt[x][y] = '/';
     39 
     40     int f1 = _find(z+z/n+1);
     41     int f2 = _find(z+z/n+n+1);
     42     if(f1!=f2)
     43     {
     44         int tmp = root[f2];
     45         root[f2] = f1;
     46         cur[x+1][y]++;
     47         cur[x][y+1]++;
     48         if(judge2(z) && dfs(z+1)) return true;
     49         cur[x+1][y]--;
     50         cur[x][y+1]--;
     51         root[f2] = tmp;
     52     }
     53 
     54     rnt[x][y] = '\';
     55     f1 = _find(z+z/n);
     56     f2 = _find(z+z/n+n+2);
     57     if(f1!=f2)
     58     {
     59         int tmp = root[f2];
     60         root[f2] = f1;
     61         cur[x][y]++;
     62         cur[x+1][y+1]++;
     63         if(judge2(z) && dfs(z+1)) return true;
     64         cur[x][y]--;
     65         cur[x+1][y+1]--;
     66         root[f2] = tmp;
     67     }
     68     return false;
     69 }
     70 void output()
     71 {
     72     _for(i,0,n)
     73     {
     74         _for(j,0,n)
     75         {
     76             cout << rnt[i][j];
     77         }
     78         cout << endl;
     79     }
     80 }
     81 int main()
     82 {
     83     int kase;
     84     scanf("%d",&kase);
     85     while(kase--)
     86     {
     87         scanf("%d",&n);
     88         initRoot();
     89         memset(target,-1,sizeof(target));
     90         memset(cur,0,sizeof(cur));
     91         memset(rnt,0,sizeof(rnt));
     92         _for(i,0,n+1)
     93         _for(j,0,n+1)
     94         {
     95             char tmp;
     96             cin >> tmp;
     97             if(tmp=='.')
     98                 target[i][j] = -1;
     99             else
    100                 target[i][j] = tmp-'0';
    101         }
    102         dfs(0);
    103         output();
    104     }
    105     return 0;
    106 }

    磨了一万年,DFS很快写出来了,但是并查集搞得我生不如死

  • 相关阅读:
    9. Palindrome Number
    7. Reverse Integer
    650. 2 Keys Keyboard
    646. Maximum Length of Pair Chain
    523. Continuous Subarray Sum
    516. Longest Palindromic Subsequence
    dp问题解题思路
    494. Target Sum
    小波变换网文精粹:小波:看森林,也看树木(一)
    数学、海豚和花朵
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10288852.html
Copyright © 2020-2023  润新知