天天看點

HDU 4069 Squiggly Sudoku

Problem Description

Today we play a squiggly 

​​sudoku​​, The objective is to fill a 9*9 grid with digits so that each column, each row, and each of the nine Connecting-sub-grids that compose the grid contains all of the digits from 1 to 9.

Left figure is the puzzle and right figure is one solution.

Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.

Input

The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.

Each case contains nine lines, Each line contains nine integers.

Each module number tells the information of the gird and is the sum of up to five integers:

0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.

16: wall to the up

32: wall to the right

64: wall to the down

128: wall to the left

I promise there must be nine Connecting-sub-grids, and each contains nine girds.

Output

For each case, if there are Multiple Solutions or no solution just output "Multiple Solutions" or "No solution". Else output the exclusive solution.(as shown in the sample output)

Sample Input

3

144 18 112 208 80 25 54 144 48

135 38 147 80 121 128 97 130 32

137 32 160 144 114 167 208 0 32

192 100 160 160 208 96 183 192 101

209 80 39 192 86 48 136 80 114

152 48 226 144 112 160 160 149 48

128 0 112 166 215 96 160 128 41

128 39 153 32 209 80 101 136 35

192 96 200 67 80 112 208 68 96

144 48 144 81 81 16 53 144 48

128 96 224 144 48 128 103 128 38

163 208 80 0 37 224 209 0 32

135 48 176 192 64 112 176 192 104

192 101 128 89 80 82 32 150 48

149 48 224 208 16 48 224 192 33

128 0 114 176 135 0 80 112 169

137 32 148 32 192 96 176 144 32

192 96 193 64 80 80 96 192 96

144 88 48 217 16 16 80 112 176

224 176 129 48 128 40 208 16 37

145 32 128 96 196 96 176 136 32

192 32 227 176 144 80 96 192 32

176 192 80 98 160 145 80 48 224

128 48 144 80 96 224 183 128 48

128 36 224 144 51 144 32 128 105

131 64 112 136 32 192 36 224 176

224 208 80 64 64 116 192 83 96

Sample Output

Multiple Solutions

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 10005;
const ll size = 15;
int n, m, x, y, T, a[size][size], tot, p[1000][3], f[size][size][4], of[size][size];

inline void read(int &ret)
{
  char c;
  do {
    c = getchar();
  } while (c < '0' || c > '9');
  ret = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
}

struct DLX
{
  int L[maxn], R[maxn], U[maxn], D[maxn];
  int row[maxn], col[maxn], ans[maxn], cnt[maxn];
  int n, m, num, sz;
  bool flag;
  void add(int now, int l, int r, int u, int d, int x, int y)
  {
    L[now] = l; R[now] = r; U[now] = u;
    D[now] = d;   row[now] = x;  col[now] = y;
  }
  void reset(int n, int m)
  {
    flag = false;
    this->n = n; this->m = m;
    for (int i = 0; i <= m; i++)
    {
      add(i, i - 1, i + 1, i, i, 0, i);
      cnt[i] = 0;
    }
    L[0] = m;   R[m] = 0;   sz = m + 1;
  }
  void insert(int x, int y)
  {
    int ft = sz - 1;
    if (row[ft] != x)
    {
      add(sz, sz, sz, U[y], y, x, y);
      U[D[sz]] = sz; D[U[sz]] = sz;
    }
    else
    {
      add(sz, ft, R[ft], U[y], y, x, y);
      R[L[sz]] = sz; L[R[sz]] = sz;
      U[D[sz]] = sz; D[U[sz]] = sz;
    }
    ++cnt[y]; ++sz;
  }
  void remove(int now)
  {
    R[L[now]] = R[now];
    L[R[now]] = L[now];
    for (int i = D[now]; i != now; i = D[i])
    for (int j = R[i]; j != i; j = R[j])
    {
      D[U[j]] = D[j];
      U[D[j]] = U[j];
      --cnt[col[j]];
    }
  }
  void resume(int now)
  {
    for (int i = U[now]; i != now; i = U[i])
    for (int j = L[i]; j != i; j = L[j])
    {
      D[U[j]] = j;
      U[D[j]] = j;
      ++cnt[col[j]];
    }
    R[L[now]] = now;
    L[R[now]] = now;
  }
  int dfs(int x)
  {
    if (!R[0]) {
      num = x;
      for (int i = 0; i < num; i++)
        a[p[ans[i]][0]][p[ans[i]][1]] = p[ans[i]][2];
      if (!flag) { flag = true; return 0; }
      return 1;
    }
    int now = R[0];
    for (int i = now; i != 0; i = R[i])
    if (cnt[now]>cnt[i]) now = i;
    remove(now);
    for (int i = D[now]; i != now; i = D[i])
    {
      ans[x] = row[i];
      for (int j = R[i]; j != i; j = R[j]) remove(col[j]);
      if (dfs(x + 1)) return 2;
      for (int j = L[i]; j != i; j = L[j]) resume(col[j]);
    }
    resume(now);
    if (x == 0 && flag) return 1;
    return 0;
  }
  void display()
  {
    for (int i = 1; i <= 9;i++)
      for (int j = 1; j <= 9; j++)
        printf("%d%s", a[i][j], j == 9 ? "\n" : "");
  }
}dlx;

void insert(int x, int y, int z)
{
  p[++tot][0] = x;
  p[tot][1] = y;
  p[tot][2] = z;
  dlx.insert(tot, 9 * (x - 1) + y);
  dlx.insert(tot, 81 + 9 * (x - 1) + z);
  dlx.insert(tot, 162 + 9 * (y - 1) + z);
  dlx.insert(tot, 243 + 9 * (of[x][y] - 1) + z);
}

int find(int x, int y, int &z)
{
  if (z >= 128) z -= 128, f[x][y][0] = 1;
  if (z >= 64) z -= 64, f[x][y][1] = 1;
  if (z >= 32) z -= 32, f[x][y][2] = 1;
  if (z >= 16) z -= 16, f[x][y][3] = 1;
  return 0;
}

void get(int x, int y, int flag)
{
  if (of[x][y]) return;
  of[x][y] = flag; 
  if (!f[x][y][0]) get(x, y - 1, flag);
  if (!f[x][y][1]) get(x + 1, y, flag);
  if (!f[x][y][2]) get(x, y + 1, flag);
  if (!f[x][y][3]) get(x - 1, y, flag);
}

int main()
{
  int c[size][size], r[size][size], u[size][size];
  int T, tt = 0;
  read(T);
  while (T--)
  {
    tot = 0;
    memset(c, 0, sizeof(c));
    memset(r, 0, sizeof(r));
    memset(u, 0, sizeof(u));
    memset(f, 0, sizeof(f));
    memset(of, 0, sizeof(of));
    dlx.reset(729, 324);
    for (int i = 1; i <= 9; ++i)
      for (int j = 1; j <= 9; ++j)
        read(a[i][j]), find(i, j, a[i][j]);
    for (int i = 1; i <= 9; ++i)
      for (int j = 1; j <= 9; ++j) 
        if (!of[i][j]) get(i, j, ++tot);
    tot = 0;
    for (int i = 1; i <= 9; ++i)
      for (int j = 1; j <= 9; ++j)
        if (a[i][j])
        {
          insert(i, j, a[i][j]);
          c[i][a[i][j]] = r[j][a[i][j]] = u[of[i][j]][a[i][j]] = 1;
        }
    for (int i = 1; i <= 9; ++i)
      for (int j = 1; j <= 9; ++j)
        for (int k = 1; k <= 9; ++k)
          if (a[i][j] + c[i][k] + r[j][k] + u[of[i][j]][k] < 1) insert(i, j, k);
    printf("Case %d:\n", ++tt);
    switch (dlx.dfs(0))
    {
      case 0:printf("No solution\n"); break;
      case 1:dlx.display(); break;
      case 2:printf("Multiple Solutions\n"); break;
    }
  }
  return 0;
}