백준 3005번

사전 지식: sort 정렬
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int R, C;
cin >> R >> C;
vector<string> board(R);
for (int i = 0; i < R; ++i) {
cin >> board[i];
}
vector<string> words;
for (int i = 0; i < R; ++i) {
string tmp = "";
for (int j = 0; j < C; ++j) {
if (board[i][j] != '#') {
tmp += board[i][j];
}
if (board[i][j] == '#' || j == C - 1) {
if (tmp.size() >= 2) {
words.push_back(tmp);
}
tmp.clear();
}
}
}
for (int j = 0; j < C; ++j) {
string tmp = "";
for (int i = 0; i < R; ++i) {
if (board[i][j] != '#') {
tmp += board[i][j];
}
if (board[i][j] == '#' || i == R - 1) {
if (tmp.size() >= 2) {
words.push_back(tmp);
}
tmp.clear();
}
}
}
sort(words.begin(), words.end());
cout << words.front() << "\n";
}
코드 설명
- R과 C를 입력받음
- 정답 퍼즐 보드 입력받음 (R의 범위를 모르니 동적배열 사용)
- 단어 리스트(words) 를 동적배열로 만듬
- for문으로 행에서 #이 나오기 전까지 하나씩 받아서 단어를 words에 저장
- #을 만나면 크기 확인 (2 이상이여야 단어) > 행이 끝날때 까지 반복
- 행이 끝났으면 열도 똑같이 반복
- sort 정렬
- 제일 앞에있는 단어 출력