Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
문제를 풀어놓은지는 거의 1년이 넘었는데, 드디어 아케이드 60문제의 마지막을 올리게 되었다. 풀어놓았던 것이라서 다시 풀고 정리하며 제대로 올리고 싶었지만, 다시 풀기가 상당히 귀찮게 느껴져서 처리과정만 정리해서 60개 다 올린 것도 다행이라 생각한다. 아케이드 말고 코드시그널의 다른 챕터 문제들도 풀어둔게 있지만, 이제 릿코드 문제를 가끔 새로 풀면서 올리려고 한다.
I solved arcade problem in codefights(codesignals) almost a year ago, but I finally post this last solution of 60 Questions. At first, I wanted to re-solve each problem and post clean code, but it was so tired for me to do again, so I barely did just order process sentences. I also have some other solutions in codefights, but I'll solve leetcode and post it, new one.
Q.
e.g.
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.
1번 예제는, 아웃풋이 true 이다.
2번 예제는, 아웃풋이 false 이다. 각 3x3 하위그리드에서 겹치는 숫자들을 가지고 있다.
//Process
Code.. Lemme see code!!!!!
코드.. 코드를 보자!!!!!
bool sudoku(std::vector<std::vector<int>> grid) {
bool result = true;
for (int i = 0; result && i < grid.size(); i++)
{
for (int j = 0; result && j < grid[0].size(); j++)
{
if (checkSameRow(grid, i, grid[i][j]))
{
if (checkSameColumn(grid, j, grid[i][j]))
{
result = checkSameBlock(grid, i , j , grid[i][j]);
}
else
{
result = false;
}
}
else
{
result = false;
}
}
}
return result;
}
/**
* Process
* 1. Input 9x9 grid vector, i(rowIndex), j(columnIndex), value
* 2. Check i's remainder after dividing by 3
* 2.1. If 1 -> Check j's remainder after dividing by 3
* 2.1.1. If 1 -> check if it has same value on the area
* 2.1.1. If 2 ->
* 2.1.1. If 0 ->
* 2.2. If 1 -> Check j's remainder after dividing by 3
* 2.2.1. If 1 ->
* 2.2.1. If 2 ->
* 2.2.1. If 0 ->
* 2.3. If 1 -> Check j's remainder after dividing by 3
* 2.3.1. If 1 ->
* 2.3.1. If 2 ->
* 2.3.1. If 0 ->
* 3. Return the answer
* 4. Finish
*/
bool checkSameBlock(std::vector<std::vector<int>> grid, int i, int j, int value) {
bool answer = true;
i+=1;
j+=1;
int iRemainder = i % 3;
int jRemainder = j % 3;
int count = 0;
if (iRemainder == 1) {
if (jRemainder == 1) {
for (int k = i-1; k < i-1 + 3; k++) {
for (int l = j-1; l < j-1 + 3; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 2) {
for (int k = i-1; k < i-1 + 3; k++) {
for (int l = j-1-1; l < j-1+2; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 0) {
for (int k = i-1; k < i-1 + 3; k++) {
for (int l = j-1-2; l < j-1+1; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
}
} else if (iRemainder == 2) {
if (jRemainder == 1) {
for (int k = i-1-1; k < i-1 + 2; k++) {
for (int l = j-1; l < j-1 + 3; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 2) {
for (int k = i-1-1; k < i-1 + 2; k++) {
for (int l = j-1-1; l < j-1+2; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 0) {
for (int k = i-1-1; k < i-1 + 2; k++) {
for (int l = j-1-2; l < j-1+1; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
}
} else if (iRemainder == 0) {
if (jRemainder == 1) {
for (int k = i-1-2; k < i-1 + 1; k++) {
for (int l = j-1; l < j-1 + 3; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 2) {
for (int k = i-1-2; k < i-1 + 1; k++) {
for (int l = j-1-1; l < j-1+2; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
} else if (jRemainder == 0) {
for (int k = i-1-2; k < i-1 + 1; k++) {
for (int l = j-1-2; l < j-1+1; l++) {
if (grid[k][l] == value) {
count++;
}
}
}
}
}
if (count > 1) {
cout << "last";
answer = false;
}
return answer;
}
bool checkSameRow(std::vector<std::vector<int>> grid, int index, int value) {
int count = 0;
for (int i = 0; i < grid[index].size(); ++i)
{
if (grid[index][i] == value)
{
++count;
}
}
if (count > 1)
{
cout << "same row";
return false;
}
else
{
return true;
}
}
bool checkSameColumn(std::vector<std::vector<int>> grid, int index, int value) {
int count = 0;
for (int i = 0; i < grid.size(); ++i)
{
if (grid[i][index] == value)
{
++count;
}
}
if (count > 1)
{
cout << "same column";
return false;
}
else
{
return true;
}
}
Something else you might like..
2018/12/16 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)
2018/12/06 - [Life/Health care] - Vitamin C 비타민 씨 usage/side effects/dosage 용법/효능/부작용
2018/12/02 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/용법