본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #24 minesweeper. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

Aracade Intro #24 minesweeper. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar



Q.


In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.


지뢰찾기 게임에서 보드에 나와있는 숫자는 주변에 지뢰가 몇개나 있는지를 나타낸다. 지뢰찾기 게임 셋업을 위한 지뢰의 갯수에 맞는 숫자 배치를 하라.



e.g.


matrix = [[true, false, false],

          [false, true, false],

          [false, false, false]]

the output should be


minesweeper(matrix) = [[1, 2, 1],

                               [2, 1, 1],

                               [1, 1, 1]]



Check out the image below for better understanding:





//Process

//1. Input matrix vector

//2. iterate from begin to end //row

// 2.1. iterate from begin to end //column

//  2.1.1. check if it's true

//  2.1.2. increase every round of true sections

//3. Return answerMatrix



//처리과정

//1. 지뢰 매트릭스를 입력받는다.

//2. 행을 반복한다.

// 2.1. 열을 반복한다.

//  2.1.1. 지뢰가 있는지 확인해서

//   2.1.1.1. 있으면 -> 상하좌우 값을 +1 해준다

//3. 결과 매트릭스를 반환한다.





Code.. let me see code!!!!!!!


코드... 코드를 보자!!!!!!




std::vector<std::vector<int>> minesweeper(std::vector<std::vector<bool>> matrix) {

    

    std::vector<std::vector<int>> answerMatrix(matrix.size(), std::vector<int>(matrix[0].size()));

    

    int j;

    int rowLength = matrix.size();

    int columnLength = matrix[0].size();

    for (int i = 0; i < rowLength; ++i) 

    {

        j = 0;

        while (j < columnLength) 

        {

            if (matrix[i][j] == true) 

            {

                if (i > 0 && j > 0 && i < rowLength-1 && j < columnLength-1) {

                    answerMatrix[i-1][j-1] += 1;answerMatrix[i-1][j] += 1;

                    answerMatrix[i-1][j+1] += 1;answerMatrix[i][j-1] += 1;

                    answerMatrix[i][j+1] += 1;answerMatrix[i+1][j-1] += 1;

                    answerMatrix[i+1][j] += 1;answerMatrix[i+1][j+1] += 1;

                }

                else if ((i > 0 && i < rowLength-1) && (j == 0 || j == columnLength-1)) {

                    if (j == 0) {

                        answerMatrix[i-1][j] += 1;answerMatrix[i-1][j+1] += 1;

                        answerMatrix[i][j+1] += 1;answerMatrix[i+1][j] += 1;

                        answerMatrix[i+1][j+1] += 1;

                    }

                    else {

                        answerMatrix[i-1][j] += 1;answerMatrix[i-1][j-1] += 1;

                        answerMatrix[i][j-1] += 1;answerMatrix[i+1][j] += 1;

                        answerMatrix[i+1][j-1] += 1;

                    }

                }

                else if ((j > 0 && j < columnLength-1) && (i == 0 || i == rowLength-1)) {

                   if (i == 0) {

                        answerMatrix[i][j-1] += 1;answerMatrix[i][j+1] += 1;

                        answerMatrix[i+1][j-1] += 1;answerMatrix[i+1][j] += 1;

                        answerMatrix[i+1][j+1] += 1;

                    }

                    else {

                        answerMatrix[i-1][j] += 1;answerMatrix[i-1][j+1] += 1;

                        answerMatrix[i-1][j-1] += 1;answerMatrix[i][j+1] += 1;

                        answerMatrix[i][j-1] += 1;

                    }

                }

                else if (i == 0 && j == 0) {

                    answerMatrix[i][j+1] += 1; answerMatrix[i+1][j+1] += 1;

                    answerMatrix[i+1][j] += 1;

                }

                else if (i == 0 && j == columnLength-1) {

                    answerMatrix[i][j-1] += 1; answerMatrix[i+1][j-1] += 1;

                    answerMatrix[i+1][j] += 1;

                    }

                else if (j == 0 && i == rowLength-1) {

                    answerMatrix[i-1][j] += 1; answerMatrix[i-1][j+1] += 1;

                    answerMatrix[i][j+1] += 1;

                }

                else if (i == rowLength-1 && j == columnLength-1) {

                    answerMatrix[i-1][j-1] += 1; answerMatrix[i-1][j] += 1;

                    answerMatrix[i][j-1] += 1;

                }

            }

            ++j;

        }

    }

    return answerMatrix;

}





Something else....




2018/10/31 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #23 boxBlur. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/29 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #22 avoidObstacles. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #21 isIPv4Address. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/27 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #19 AreEquallyStrong. Algorithm, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #18 PalindromeRearranging, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #17 ArrayChange, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #16 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #15 AddBorder, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #14 AlternatingSums, Codefights, CodeSignal

2018/10/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #13 ReverseParentheses, Codefights, CodeSignal

2018/10/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #12 SortByHeight

2018/10/06 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #11 IsLucky

2018/10/03 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #10 CommonCharacterCount

2018/09/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #9 AllLongestStrings

2018/09/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #8 MatrixElementsSum

2018/09/23 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #7 AlmostIncreasingSequence

2018/09/22 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #6 MakeArrayConsecutive2

2018/09/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #5 ShapeArea

2018/09/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #4 AdjacentElementsProduct

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #3 CheckPalindrome

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #2 CenturyFromYear

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #1 Add