1 #include<cstdio>
  2 #include<cstring>
  3 #define MAXM 10
  4 #define MAXL 324
  5 #define MAXN 240000
  6 #define INF 0x7FFFFFFF
  7 char sd[MAXM][MAXM];
  8 int L[MAXN], R[MAXN], U[MAXN], D[MAXN], H[MAXN];
  9 int size, C[MAXN], S[MAXN], X[MAXN], Q[MAXN], vis[MAXL * 3];
 10 bool Read()
 11 {
 12     int i, j;
 13     for (i = 1; i < MAXM; i++)
 14     {
 15         for (j = 1; j < MAXM; j++)
 16         {
 17             scanf(" %c", &sd[i][j]);
 18             if (sd[i][j] == 'e')
 19                 return false;
 20         }
 21     }
 22     return true;
 23 }
 24 void Init()
 25 {
 26     int i;
 27     for (i = 0; i <= MAXL; i++)
 28     {
 29         L[i + 1] = i;
 30         R[i] = i + 1;
 31         U[i] = D[i] = i;
 32         S[i] = 0;
 33     }
 34     R[MAXL] = 0;
 35     size = MAXL + 1;
 36 }
 37 void Remove(int c)
 38 {
 39     int i, j;
 40     L[R[c]] = L[c];
 41     R[L[c]] = R[c];
 42     for (i = D[c]; i != c; i = D[i])
 43     {
 44         for (j = R[i]; j != i; j = R[j])
 45         {
 46             U[D[j]] = U[j];
 47             D[U[j]] = D[j];
 48             S[C[j]]--;
 49         }
 50     }
 51 }
 52 void Resume(int c)
 53 {
 54     int i, j;
 55     L[R[c]] = c;
 56     R[L[c]] = c;
 57     for (i = D[c]; i != c; i = D[i])
 58     {
 59         for (j = R[i]; j != i; j = R[j])
 60         {
 61             U[D[j]] = j;
 62             D[U[j]] = j;
 63             S[C[j]]++;
 64         }
 65     }
 66 }
 67 inline void Link(int r, int c)
 68 {
 69     D[size] = D[c];
 70     U[size] = c;
 71     U[D[c]] = size;
 72     D[c] = size;
 73     if (H[r] < 0)
 74         H[r] = L[size] = R[size] = size;
 75     else
 76     {
 77         L[size] = H[r];
 78         R[size] = R[H[r]];
 79         L[R[H[r]]] = size;
 80         R[H[r]] = size;
 81     }
 82     S[c]++;
 83     X[size] = r;
 84     C[size++] = c;
 85 }
 86 bool Dance(int now)
 87 {
 88     int i, j, c, temp;
 89     if (R[0] == 0)
 90         return true;
 91     for (temp = INF,i = R[0]; i; i = R[i])
 92     {
 93         if (S[i] < temp)
 94         {
 95             temp = S[i];
 96             c = i;
 97         }
 98     }
 99     Remove(c);
100     for (i = D[c]; i != c; i = D[i])
101     {
102         vis[X[i]] = true;
103         for (j = R[i]; j != i; j = R[j])
104             Remove(C[j]);
105         if (Dance(now + 1))
106             return true;
107         for (j = L[i]; j != i; j = L[j])
108             Resume(C[j]);
109         vis[X[i]] = false;
110     }
111     Resume(c);
112     return false;
113 }
114 int main()
115 {
116     int i, j, k, r;
117     while (Read())
118     {
119         Init();
120         for (r = 0, i = 1; i < MAXM; i++)
121         {
122             for (j = 1; j < MAXM; j++)
123             {
124                 if (sd[i][j] == '.')
125                 {
126                     for (k = 1; k < MAXM; k++)
127                     {
128                         H[++r] = -1;
129                         Q[r] = k;
130                         Link(r, (i - 1) * 9 + k);
131                         Link(r, 81 + (j - 1) * 9 + k);
132                         Link(r, 162 + ((i - 1) / 3 * 3 + (j - 1) / 3) * 9 + k);
133                         Link(r, 243 + (i - 1) * 9 + j);
134                     }
135                 }
136                 else
137                 {
138                     H[++r] = -1;
139                     k = sd[i][j] - '0';
140                     Q[r] = k;
141                     Link(r, (i - 1) * 9 + k);
142                     Link(r, 81 + (j - 1) * 9 + k);
143                     Link(r, 162 + ((i - 1) / 3 * 3 + (j - 1) / 3) * 9 + k);
144                     Link(r, 243 + (i - 1) * 9 + j);
145                 }
146             }
147         }
148         memset(vis, false, sizeof(vis));
149         Dance(0);
150         for (i = 1; i <= r; i++)
151         {
152             if (vis[i])
153                 printf("%d", Q[i]);
154         }
155         putchar('\n');
156     }
157     return 0;
158 }
posted on 2012-07-24 10:36  DrunBee  阅读(258)  评论(0编辑  收藏  举报