Diademata 2018. 4. 14. 01:59
반응형

Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with digits so that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid contains all of the digits from 1 to 9.


This algorithm should check if the given grid of numbers represents a correct solution to Sudoku.


Example


For the first example below, the output should be true. For the other grid, the output should be false: each of the nine 3 × 3 sub-grids should contain all of the digits from 1 to 9.


 


Arcade Intro 60문제 다 풀었다~

 

code>>


bool sudoku(std::vector<std::vector<int>> grid) {

std::set<int> col;

std::set<int> row;

std::set<int> square;

for (int i = 0; i < grid.size(); i++)

{

for (int ii = 0; ii < grid[i].size(); ii++)

{

if (col.count(grid[i][ii]) > 0) return false;

col.insert(grid[i][ii]);


if (ii % 3 == 0 && i % 3 == 0)

{

for (int y = i; y < i + 3; y++)

{

for (int x = ii; x < ii + 3; x++)

{

if (square.count(grid[y][x]) > 0) return false;

square.insert(grid[y][x]);

}

}

square.clear();

}

if (i > 0)continue;

if(row.count(grid[ii][i]) > 0) return false;

row.insert(grid[ii][i]);

}

col.clear();

row.clear();

square.clear();

}

return true;

}

반응형