본문 바로가기
알고리즘

[종만북 예제 P150] 보글 게임 전체코드

by 돌맹96 2024. 12. 4.
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
반응형