带旋转的数独游戏(C++语言)

Description
数独是一个基于逻辑的组合数字放置拼图,在世界各地都很受欢迎。
在这个问题上,让我们关注 16*16 网格的拼图,其中包含 4*4 个区域。 目标是用十六进制数字填充整个网格,即 0123456789ABCDEF,以便每列,每行和每个区域包含所有十六进制数字。
下图显示了一个被成功解决的数独例子:


昨天,周老师解决了一个数独并将其留在桌面上。 然而,龙龙想和他开个玩笑——龙龙打算对这个已经解决的数独进行多次以下操作。
 选择一个 4*4  的小区域并顺时针旋转 90 度。
周老师回来发现他拼好的数独板被打乱了,开始挠头,你能帮他以最小的步数恢复原样吗?请你手把手的教他怎么做,也就是需要输出方案。
请注意选择要旋转的方块不能跨越任何小区域,也就是说必须选择一块完整的小区域旋转。小区域的定义在上面,16*16 的网格被分成 4*4 个小区域。


Input
第一行输入一个正整数 T(1≤T≤10^3 ) 表示数据组数;
接下来每组数据输入一个 16*16 的数独图,表示被龙龙打乱后的数独面板。


Output
对于每组数据:
第一行输出一个整数 ans ,表示周老师最少需要逆时针旋转多少次才能恢复原样。
接下来输出 ans 行,每行两个数px py,表示逆时针旋转一次第 px 行第 py 列的小矩阵。


样例

输入(1)

1
681D5A0C9FDBB2F7
0A734B62E167D9E5
5C9B73EF3C208410
F24ED18948A5CA63
39FAED5616400B74
D120C4B7CA3DEF38
7EC829A085BE6D51
B56438F129F79C2A
5C7FBC4E3D08719F
AE8B1673BF42A58D
60D3AF25619C30BE
294190D8EA57264C
C7D1B35606835EAB
AF52A1E019BE4306
8B36DC78D425F7C9
E409492FC7FA18D2

输出(1)

11
1 3
1 3
1 3
2 3
2 3
2 3
3 1
3 1
3 1
4 2
4 2

代码

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

const int N = 16; // Sudoku board size
int sudoku[N][N]; // Sudoku board
int rowCheck[N][N + 1]; // Check for row-wise numbers
int colCheck[N][N + 1]; // Check for column-wise numbers

struct Rotation {
    int x, y, t; // Rotation position and count
};

int minRotations; // Minimum rotation count
vector<Rotation> currentRotations; // Current rotation sequence
vector<Rotation> minRotationsSequence; // Minimum rotation sequence

// Rotate a small area counterclockwise by 90 degrees
void rotateCounterClockwise(int x, int y) {
    x *= 4; // Convert coordinates to the small area
    y *= 4;
    for (int i = 0; i < 2; i++) {
        for (int j = i; j < 3 - i; j++) {
            int temp = sudoku[x + i][y + j];
            sudoku[x + i][y + j] = sudoku[x + j][y + 3 - i];
            sudoku[x + j][y + 3 - i] = sudoku[x + 3 - i][y + 3 - j];
            sudoku[x + 3 - i][y + 3 - j] = sudoku[x + 3 - j][y + i];
            sudoku[x + 3 - j][y + i] = temp;
        }
    }
}

// Check if the rotated small area is valid
bool isValidRotation(int x, int y) {
    x *= 4; // Convert coordinates to the small area
    y *= 4;
    for (int i = x; i <= x + 3; i++) {
        for (int j = y; j <= y + 3; j++) {
            int num = sudoku[i][j];
            if (rowCheck[i][num] || colCheck[j][num]) {
                return false;
            }
        }
    }
    // Update the occurrence of numbers in each row and column
    for (int i = x; i <= x + 3; i++) {
        for (int j = y; j <= y + 3; j++) {
            int num = sudoku[i][j];
            rowCheck[i][num] = true;
            colCheck[j][num] = true;
        }
    }
    return true;
}

// Undo the rotation operation
void undoRotation(int x, int y) {
    x *= 4; // Convert coordinates to the small area
    y *= 4;
    for (int i = x; i < x + 4; i++) {
        for (int j = y; j < y + 4; j++) {
            int num = sudoku[i][j];
            rowCheck[i][num] = false;
            colCheck[j][num] = false;
        }
    }
}

// Depth-first search, try different rotation sequences
void dfs(int x, int y, int count) {
    if (x == 4) { // If all small areas have been traversed
        if (count < minRotations) { // Update the minimum rotation count and sequence
            minRotations = count;
            minRotationsSequence = currentRotations;
        }
        return;
    }

    for (int op = 0; op < 4; op++) { // Try different rotation counts
        if (!isValidRotation(x, y)) { // If the rotated small area is not valid, continue to rotate counterclockwise
            rotateCounterClockwise(x, y);
            continue;
        }

        currentRotations.push_back({x + 1, y + 1, op}); // Add the current rotation sequence to the list

        if (y == 3) { // If all small areas in the current row have been traversed, move to the next row
            dfs(x + 1, 0, count + op);
        }
        else dfs(x, y + 1, count + op); // Continue to traverse the next small area in the current row

        currentRotations.pop_back(); // Undo the current rotation sequence
        undoRotation(x, y); // Undo the rotation operation
        rotateCounterClockwise(x, y); // Restore the small area
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        minRotations = INT_MAX;
        currentRotations.clear();
        minRotationsSequence.clear();
        memset(colCheck, 0, sizeof colCheck);
        memset(rowCheck, 0, sizeof rowCheck);

        // Read the Sudoku board
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                char hex;
                cin >> hex;
                int dec = stoi(string(1, hex), nullptr, 16);
                sudoku[i][j] = dec;
            }
        }

        dfs(0, 0, 0); // Start depth-first search
        cout << minRotations << endl; // Output the minimum rotation count
        for (const auto& rotation : minRotationsSequence) { // Output the minimum rotation sequence
            for (int i = 0; i < rotation.t; i++) {
                cout << rotation.x << " " << rotation.y << endl;
            }
        }
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值