Problem Solving/BOJ

[백준 / BOJ] C++ 27495 만다라트 만들기

nageune 2023. 2. 20. 16:35
728x90
반응형

27495번: 만다라트 만들기

 

문제

https://www.acmicpc.net/problem/27495

 

27495번: 만다라트 만들기

1번째 줄에는 “#x. “를 출력한 다음, 사전순으로 가장 먼저 오는 중간 목표를 출력한다. 숫자가 알파벳보다 사전순으로 먼저 오고, 알파벳 대문자가 알파벳 소문자보다 사전순으로 먼저 온다. 2

www.acmicpc.net

 

 

풀이

배열에서 (1,1), (1,4), (1,7), (4,1), (4,7), (7,1), (7,4), (7,7) 8점의 좌표와 문자열을 문자열 사전순으로 정렬하고 좌표를 (x, y)라고 한다면 (x-1, y-1)부터 (x+1, y+1)까지 (x, y)를 제외한 값을 또다시 정렬해 차례대로 출력하면 된다. 출력 형식만 잘 지켜서 구현하면 된다.

 

 

코드

#include <bits/stdc++.h>
using namespace std;

vector<vector<string>> graph(9, vector<string>(9, ""));
int cnt = 1;

void func(int x, int y) {
  cout << "#" << cnt << ". " << graph[x][y] << '\n';
  vector<string> tmp;
  for (int i = x - 1; i <= x + 1; i++)
    for (int j = y - 1; j <= y + 1; j++)
      if (i == x && j == y)
        continue;
      else
        tmp.push_back(graph[i][j]);
  sort(tmp.begin(), tmp.end());
  for (int i = 0; i < 8; i++) {
    cout << "#" << cnt << "-" << i + 1 << ". " << tmp[i] << '\n';
  }
  cnt++;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
      cin >> graph[i][j];
  int d[] = {1, 4, 7};
  vector<pair<string, pair<int, int>>> v;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      if (i == 1 && j == 1)
        continue;
      else
        v.push_back({graph[d[i]][d[j]], {d[i], d[j]}});
  sort(v.begin(), v.end());
  for (auto i : v) {
    int x = i.second.first;
    int y = i.second.second;
    func(x, y);
  }
  return 0;
}

 

728x90
반응형