알고리즘/algospot

algospot KAKURO1

꾸준함. 2018. 2. 25. 17:04

문제 링크입니다: https://algospot.com/judge/problem/read/KAKURO1

http://jaimemin.tistory.com/416?category=985009과 반대로 카쿠로 퍼즐의 답이 주어졌을 때 http://jaimemin.tistory.com/416?category=985009의 입력양식을 출력해내는 문제였습니다.

물론 난이도는 KAKURO2와는 비교도 안되게 쉬웠습니다.

#include <iostream>

#include <vector>

#include <cstring> //memset

using namespace std;

 

const int MAX = 20;

 

int N;

int board[MAX][MAX];

vector<pair<pair<int, int>, pair<int, int>>> h; //가로 (y, x, 0, 힌트)

vector<pair<pair<int, int>, pair<int, int>>> v; //세로 (y, x, 1, 힌트)

 

void horizontalHint(void)

{

        for(int y=0; y<N; y++)

               for(int x=0; x<N-1; x++) //가로는 N-1까지만 확인하면 된다

                       if (!board[y][x] && board[y][x + 1]) //힌트가 입력되어야하는 칸이라면

                       {

                              int sum = board[y][x + 1];

                              int idx = x + 1;

                              while ((idx + 1 < N) && board[y][idx + 1]) //idx+1가 범위내에 있고 칸에 숫자가 있다면

                              {

                                      sum += board[y][idx + 1];

                                      idx++;

                              }

                              v.push_back(make_pair(make_pair(y + 1, x + 1), make_pair(0, sum)));

                       }

}

 

void verticalHint(void)

{

        for(int y=0; y<N-1; y++) //세로는 N-1까지만 확인하면 된다

               for (int x = 0; x < N; x++)

                       if (!board[y][x] && board[y + 1][x]) //힌트가 입력되어야하는 칸이라면

                       {

                              int sum = board[y + 1][x];

                              int idx = y + 1;

                              while ((idx + 1 < N) && board[idx + 1][x]) //idx+1가 범위내에 있고 칸에 숫자가 있다면

                              {

                                      sum += board[idx + 1][x];

                                      idx++;

                              }

                              v.push_back(make_pair(make_pair(y + 1, x + 1), make_pair(1, sum)));

                       }

}

 

void printColor(void)

{

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

        {

               for (int j = 0; j < N; j++)

                       if (!board[i][j])

                              cout << 0 << " ";

                       else

                              cout << 1 << " ";

               cout << endl;

        }

}

 

int main(void)

{

        int test_case;

        cin >> test_case;

        cout << test_case << endl;

 

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

        {

               h.clear(), v.clear();

               cin >> N;

               if (N > MAX)

                       exit(-1);

               cout << N << endl;

 

               for (int j = 0; j < N; j++)

                       for (int k = 0; k < N; k++)

                              cin >> board[j][k];

 

               printColor();

               horizontalHint();

               verticalHint();

               cout << v.size() + h.size() << endl; //힌트 개수

               //y+1, x+1은 이미 정렬되어있다

               for (int j = 0; j < h.size(); j++)

                       cout << h[j].first.first << " " << h[j].first.second << " " << h[j].second.first << " " << h[j].second.second << endl;

               for (int j = 0; j < v.size(); j++)

                       cout << v[j].first.first << " " << v[j].first.second << " " << v[j].second.first << " " << v[j].second.second << endl;

        }

        return 0;

}

 


개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 프로그래밍 대회에서 배우는 알고리즘 문제해결전략

반응형

'알고리즘 > algospot' 카테고리의 다른 글

algospot ROOTS  (4) 2018.02.26
algospot WITHDRAWAL  (0) 2018.02.25
algospot CANADATRIP  (0) 2018.02.25
algospot ARCTIC  (0) 2018.02.24
algospot DARPA  (0) 2018.02.23