※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 26578번 문제인 Word이다.
문제는 아래 링크를 확인하자.

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

 

26578번: Word

Word searches are hard. Luckily, you're a programmer, so you can just write a program to find the words! Fortunately, the word searches that you are doing are only looking for one word, and that word is "word". Write a program that finds the number of inst

www.acmicpc.net

주어지는 워드서치에서 가능한 단일방향의 네 글자의 조합(방향이 다르면 다른 것으로 취급한다)의 경우의 수를 직접 모두 조사해 "word"가 몇 개 존재하는지를 세어 문제를 해결하자.

 

이는 반복문으로 아래와 같이 구현할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int T;
int R, C;
vector<string> board;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	while (T--) {
		board.clear();
		int cnt = 0;
		cin >> R >> C;
		for (int r = 0; r < R; r++) {
			board.emplace_back("");
			cin >> board.back();
		}

		for (int r = 0; r < R; r++) {
			for (int c = 0; c + 3 < C; c++) {
				if (board[r][c] == 'w' && board[r][c + 1] == 'o' && board[r][c + 2] == 'r' && board[r][c + 3] == 'd') cnt++;
			}
		}
		for (int r = 0; r < R; r++) {
			for (int c = 3; c < C; c++) {
				if (board[r][c] == 'w' && board[r][c - 1] == 'o' && board[r][c - 2] == 'r' && board[r][c - 3] == 'd') cnt++;
			}
		}
		for (int r = 0; r + 3 < R; r++) {
			for (int c = 0; c < C; c++) {
				if (board[r][c] == 'w' && board[r + 1][c] == 'o' && board[r + 2][c] == 'r' && board[r + 3][c] == 'd') cnt++;
			}
		}
		for (int r = 3; r < R; r++) {
			for (int c = 0; c < C; c++) {
				if (board[r][c] == 'w' && board[r - 1][c] == 'o' && board[r - 2][c] == 'r' && board[r - 3][c] == 'd') cnt++;
			}
		}
		for (int r = 0; r + 3 < R; r++) {
			for (int c = 0; c + 3 < C; c++) {
				if (board[r][c] == 'w' && board[r + 1][c + 1] == 'o' && board[r + 2][c + 2] == 'r' && board[r + 3][c + 3] == 'd') cnt++;
			}
		}
		for (int r = 3; r < R; r++) {
			for (int c = 0; c + 3 < C; c++) {
				if (board[r][c] == 'w' && board[r - 1][c + 1] == 'o' && board[r - 2][c + 2] == 'r' && board[r - 3][c + 3] == 'd') cnt++;
			}
		}
		for (int r = 0; r + 3 < R; r++) {
			for (int c = 3; c < C; c++) {
				if (board[r][c] == 'w' && board[r + 1][c - 1] == 'o' && board[r + 2][c - 2] == 'r' && board[r + 3][c - 3] == 'd') cnt++;
			}
		}
		for (int r = 3; r < R; r++) {
			for (int c = 3; c < C; c++) {
				if (board[r][c] == 'w' && board[r - 1][c - 1] == 'o' && board[r - 2][c - 2] == 'r' && board[r - 3][c - 3] == 'd') cnt++;
			}
		}

		cout << cnt << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 2380 // Befunge] Star  (0) 2022.12.20
[BOJ 6916 // C++] 0123456789  (0) 2022.12.20
[BOJ 10188 // C++] Quadrilateral  (0) 2022.12.20
[BOJ 26529 // C++] Bunnies  (0) 2022.12.19
[BOJ 2372 // Ada] Livestock Count  (0) 2022.12.19

+ Recent posts