如何在数独检查器中将这些字符串对象更改为char?

时间:2019-11-15 19:24:22

标签: c++ arrays

因此,我正在为任务创建一个数独检查器,但我想将代码中的字符串对象改为char数组。如果有人可以帮助您进行更改,请告诉我。我主要只是想摆脱这段代码中的st.find和st.end函数。 这也是分配的方向。该代码有效,但只想包含有关此分配的一些信息。 分配: 目的是用数字填充9×9网格,以便组成该网格的每一列,每一行以及9个3×3子网格(也称为“框”,“块”或“区域”)包含从1到9的所有数字,无重复或遗漏。 [维基百科]

也就是说:

每列应包含值1-9 每行应包含值1-9 每个框应包含值1-9 每个值应在上述每个值中仅出现一次 对于此作业,将为您提供一个填写好的数独板状态,并根据给定的规则检查板是否已正确解决。

注意:我们并不是要您解决数独游戏,这是一项更为复杂的任务,需要至少递归回溯(也许在以后的任务中)。

输入 作为输入,将为您提供一个由9 x 9网格组成的填充数独板,其中每个单元格的值为1-9。 这是我的代码:

// Program to check whether given sudoku
// board is valid or not
#include <bits/stdc++.h>
using namespace std;

// Checks whether there is any duplicate
// in current row or not
bool notInRow(char arr[][9], int row)
{
    // Set to store characters seen so far.
    set<char> st;

    for (int i = 0; i < 9; i++) {

        // If already encountered before, return false
        if (st.find(arr[row][i]) != st.end())
            return false;

        // If it is not an empty cell, insert value
        // at the current cell in the set
        if (arr[row][i] != '.')
            st.insert(arr[row][i]);
    }
    return true;
}

// Checks whether there is any duplicate
// in current column or not.
bool notInCol(char arr[][9], int col)
{
    set<char> st;

    for (int i = 0; i < 9; i++) {

        // If already encountered before, return false
        if (st.find(arr[i][col]) != st.end())
            return false;

        // If it is not an empty cell,
        // insert value at the current cell in the set
        if (arr[i][col] != '.')
            st.insert(arr[i][col]);
    }
    return true;
}

// Checks whether there is any duplicate
// in current 3x3 box or not.
bool notInBox(char arr[][9], int startRow, int startCol)
{
    set<char> st;

    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            char curr = arr[row + startRow][col + startCol];

            // If already encountered before, return false
            if (st.find(curr) != st.end())
                return false;

            // If it is not an empty cell,
            // insert value at current cell in set
            if (curr != '.')
                st.insert(curr);
        }
    }
    return true;
}

// Checks whether current row and current column and
// current 3x3 box is valid or not
bool isValid(char arr[][9], int row, int col)
{
    return notInRow(arr, row) && notInCol(arr, col) &&
           notInBox(arr, row - row % 3, col - col % 3);
}

bool isValidConfig(char arr[][9], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {

            // If current row or current column or
            // current 3x3 box is not valid, return false
            if (!isValid(arr, i, j))
                return false;
        }
    }
    return true;
}

// Drivers code
int main()
{
    char board[9][9];
    for(int i=0;i<9;i++) {
        for(int j=0;j<9;j++) {
            cin>>board[i][j];
        }
    }


    cout << (isValidConfig(board, 9) ? "Solution is good!\n" : "Wrong solution!\n");
    return 0;
}

0 个答案:

没有答案