简体   繁体   English

C++如何检查3x3矩阵的每一行和每一列是否只包含不同的数字

[英]C++ how to check if each row and column of 3x3 matrix contains only different numbers

In my Hitori game, the player removes numbers from the gameboard.在我的 Hitori 游戏中,玩家从游戏板上删除数字。

The game is won if all rows and columns have only different numbers.如果所有行和列只有不同的数字,则游戏获胜。

In line 43, I would need to make some check function so that the board is checked after every removal, but I dont know how to do that.在第 43 行,我需要检查 function 以便在每次拆卸后检查电路板,但我不知道该怎么做。 Should I perhaps make several for-loops?我应该做几个for循环吗?

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std; using Board = std::vector<vector<int>>;
const unsigned char EMPTY = ' ';

void initBoard(Board& board) {

string input = "1 1 3 1 2 2 2 2 3";

istringstream is { input };

    for (auto& row : board)
    {
        for(auto& column : row)
        {
        is >> column;
        }
    }
}

void printBoard(const Board& board) {


    for(unsigned int row = 0; row < 3; ++row)
    {
       cout << "| " << row + 1 << " | ";
        for(unsigned int column = 0; column < 3; ++column)
        {
            if(board.at(row).at(column) == 0)
            {
                cout << EMPTY << " ";
            }
            else
            {
                cout << board.at(row).at(column) << " ";
            }
        }
       cout << "|" << endl;
    }
    // CHECK IF EACH ROW AND COLUMN CONTAIN THE SAME NUMBER SEVERAL TIMES
    // IF NOT -> THE GAME HAS BEEN WON

}

void updateBoard(Board& board) {

    int x, y;

    while (true) {
        cout << "Enter coordinates (x, y): ";
        cin >> x;
        cin >> y;

            board.at(y-1).at(x-1) = 0;
            printBoard(board);
}
}

int main()
{
    Board board(3, vector<int>(3)); // board is vector with 3 rows and 3 columns
    initBoard(board);
    printBoard(board);
    updateBoard(board);
}

You need something that can be used to count how often a value occurs in a row or column.您需要一些可用于计算值在行或列中出现的频率的东西。 Because your values are nonnegative integers, you can use them as the indices of an array or vector, as suggested by Pete Becker.因为您的值是非负整数,所以您可以按照 Pete Becker 的建议将它们用作数组或向量的索引。 Another way is to use them as the keys of an associative container like std::unordered_map .另一种方法是将它们用作关联容器的键,例如std::unordered_map

My preference would be to put these checks in separate function(s) that you call from within the game loop, so that it can terminate when a win is detected.我的偏好是将这些检查放在您从游戏循环中调用的单独函数中,以便在检测到胜利时终止。 Here is an example function that checks the columns of the board, returning true if each has unique values:这是一个示例 function 检查板的列,如果每个都有唯一的值,则返回 true:

#include <algorithm>
#include <unordered_map>

using RepCount = std::unordered_map<int, int>;

bool allUnique(RepCount const& reps)
{
    return std::ranges::all_of(reps, [](auto& pair) {
        auto [value, count] = pair;
        return value == 0 || value == EMPTY || count == 1;
    });
}

bool checkColumns(Board const& board)
{
    for (unsigned col = 0; col < 3; ++col) {
        RepCount reps;
        for (auto& row : board)
            ++reps[row[col]];
        if (!allUnique(reps))
            return false;
    }
    return true;
}

You can invert this loop to perform the same check for each row.您可以反转此循环以对每一行执行相同的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM