CSU 2145

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

string gg[10][10];
int g[10][10][2];
bool vis[10][10];

struct st
{
    int x, y;
    int state; //0:(-/-)  1:(x/-) 2:(-/x) 3:(-)
    int a, b;
};
vector<st> rec;

bool check(int x, int y, int val, int state)
{
    for(int i = 0; i < 6; i++)
    {
        //row
        if(g[x][i][0] == val || g[x][i][1] == val)
            return false;
        //col
        if(g[i][y][0] == val || g[i][y][1] == val)
            return false;
    }
    int curx = x - (x % 2),
        cury = y - (y % 3);
    for(int i = curx; i < curx + 2; i++)
    {
        for(int j = cury; j < cury + 3; j++)
        {
            if(g[i][j][0] == val || g[i][j][1] == val)
                return false;
        }
    }

    if(state == 1 && val < g[x][y][0]) return false;
    if(state == 2 && val > g[x][y][1]) return false;

    return true;
}

bool dfs(int curState)
{
    if(curState == rec.size())
    {
        return true;
    }
    int curx = rec[curState].x, cury = rec[curState].y;
    if(rec[curState].state == 0)
    {
        for(int i = 1; i <= 8; i++)
        {
            for(int j = i + 1; j <= 9; j++)
            {
                if(check(curx, cury, i, 0) && check(curx, cury, j, 0))
                {
                    g[curx][cury][0] = i, g[curx][cury][1] = j;
                    //printf("       %d %d\n",i,j);
                    if(dfs(curState + 1))
                        return true;
                    else
                        g[curx][cury][0] = -1, g[curx][cury][1] = -1;
                }
            }
            if(i==8)
                return false;
        }
    }
    else
    {
        for(int i = 1; i <= 9; i++)
        {
            if(check(curx, cury, i, rec[curState].state))
            {
                if(rec[curState].state == 1)
                {
                    g[curx][cury][1] = i;
                    if(dfs(curState + 1))
                        return true;
                    g[curx][cury][1] = -1;
                }
                else
                {
                    g[curx][cury][0] = i;
                    if(dfs(curState + 1))
                        return true;
                    g[curx][cury][0] = -1;
                }
            }
            if(i==9)
                return false;
        }
    }
}

int main()
{
    int cas;
    scanf("%d", &cas);
    int cnt;
    while(cas--)
    {
        rec.clear();
        scanf("%d", &cnt);
        printf("%d\n", cnt);
        memset(g, -1, sizeof(g));
        for(int i = 0; i < 6; i++)
            for(int j = 0; j < 6; j++)
                cin>>gg[i][j];

        for(int i = 0; i < 6; i++)
        {
            for(int j = 0; j < 6; j++)
            {
                int len = gg[i][j].length();
                st cur;
                cur.x = i, cur.y = j;
                cur.state = 0;
                cur.a = cur.b = -1;
                if(len == 3)
                {
                    vis[i][j] = true;
                    if(gg[i][j][0] != '-')
                    {
                        g[i][j][0] = gg[i][j][0] - '0';
                        cur.a = g[i][j][0];
                        cur.state += 1;
                    }
                    if(gg[i][j][2] != '-')
                    {
                        g[i][j][1] = gg[i][j][2] - '0';
                        cur.b = g[i][j][1];
                        cur.state += 2;
                    }
                    if(cur.state < 3) rec.push_back(cur);
                }
                else
                {
                    vis[i][j] = false;
                    if(gg[i][j][0] != '-') g[i][j][0] = gg[i][j][0] - '0';
                    else
                    {
                        cur.state = 3;
                        rec.push_back(cur);
                    }
                }
            }
        }

        dfs(0);
        for(int i = 0; i < 6; i++)
        {
            for(int j = 0; j < 6; j++)
            {
                if(vis[i][j]) printf("%d/%d ", g[i][j][0], g[i][j][1]);
                else printf("%d ", g[i][j][0]);
            }
            printf("\n");
        }
    }
    return 0;
}

posted @ 2017-07-24 21:29  />.<\  阅读(225)  评论(0编辑  收藏  举报