728x90
반응형
출처 : 프로그래밍 대회에서 배우는 알고리즘 문제해결 전략
#include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <cmath>
using namespace std;
const int dx[8] = { -1, -1, -1, 1, 1, 1, 0, 0 };
const int dy[8] = { -1, 0, 1, -1, 0, 1, -1, 1 };
char board[5][5] = { {'U', 'R', 'L', 'P', 'M'}, {'X', 'P', 'R', 'E', 'T'}, {'G', 'I', 'A', 'E', 'T'}, {'X', 'T', 'N', 'Z', 'Y'}, {'X', 'O', 'Q', 'R', 'S'} };
bool inRange(int y, int x)
{
if(y < 0 || y > 5 || x < 0 || x > 5)
return false;
return true;
}
bool hasWord(int y, int x, const string& word)
{
if (!inRange(y, x))
return false;
if (board[y][x] != word[0])
return false;
if (word.size() == 1)
return true;
for (int direction = 0; direction < 8; ++direction)
{
int nextY = y + dy[direction], nextX = x + dx[direction];
if (hasWord(nextY, nextX, word.substr(1)))
return true;
}
return false;
}
int main(void) {
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
cout << hasWord(x, y, "GIRL");
}
}
return 0;
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
[BOJ-1213] C++ 팰린드롬 만들기 / 실버3 (0) | 2024.12.11 |
---|---|
[프로그래머스 - C++] 야근 지수 (0) | 2024.12.04 |
[PCCP 기출문제] 1번 / 동영상 재생기 (0) | 2024.11.19 |
[프로그래머스 - python] 방문길이 (0) | 2024.02.25 |
[BOJ-1085] C++ 직사각형에서 탈출 (0) | 2023.11.08 |