공부/SWEA

SWEA 4615. 재미있는 오셀로 게임 (C++)

밤톨ㅇl 2024. 5. 10. 15:42
#include <stdio.h>

int N, M, turn;
int othello[10][10];
int dx[8] = { -1, 1, 0, 0, -1, 1, -1, 1 };
int dy[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };

void stemp(int dir, int target_x, int target_y, int x, int y, int turn)
{
    int sx = x, sy = y;
    while (true)
    {
        if (sx == target_x && sy == target_y)
            break;
        else
        {
            othello[sy][sx] = turn;
            sx += dx[dir]; sy += dy[dir];
        }
    }
}

void solve(int y, int x, int turn)
{
    int cx = x;
    int cy = y;

    for (int i = 0; i < 8; i++)
    {
        bool flag = false;
        int nx = cx + dx[i];
        int ny = cy + dy[i];

        if (ny < 1 || nx < 1 || nx > N || ny > N || othello[ny][nx] == turn || othello[ny][nx] == 0)
            continue;
        else
        {
            while (true)
            {
                nx = nx + dx[i];
                ny = ny + dy[i];

                if (ny < 1 || nx < 1 || ny > N || nx > N || othello[ny][nx] == 0)
                    break;
                else if (othello[ny][nx] == turn)
                {
                    stemp(i, nx, ny, x, y, turn);
                    break;
                }
            }
        }
    }
}
int main()
{
    int tc = 0;
    scanf("%d", &tc);
    for (int t = 1; t <= tc; t++)
    {
        // 1: 흑, 2: 백
        int cnt_b = 0, cnt_w = 0;
        scanf("%d %d", &N, &M);

        othello[N / 2 ][N / 2 ] = 2;
        othello[N / 2 + 1][N / 2 + 1] = 2;
        othello[N / 2 + 1][N / 2 ] = 1;
        othello[N / 2][N / 2 + 1] = 1;

        for (int i = 1; i <= M; i++)
        {
            int x = 0, y = 0, turn = 0;
            scanf("%d %d %d", &x, &y, &turn);
            solve(y, x, turn);
        }
        for (int j = 1; j <= N; j++)
        {
            for (int z = 1; z <= N; z++)
            {
                if (othello[z][j] == 1)
                    cnt_b++;
                else if(othello[z][j] == 2)
                    cnt_w++;
            }
        }
        printf("#%d %d %d\n", t, cnt_b, cnt_w);
        for (int i = 0; i <= N; i++)
        {
            for (int j = 0; j <= N; j++)
            {
                othello[i][j] = 0;
            }
        }
    }
    return 0;
}

마지막에 보드 초기화를 안해가지고 개고생했넹ㅠㅠ

'공부 > SWEA' 카테고리의 다른 글

SWEA 20019. 회문의 회문 (C++)  (0) 2024.05.11
SWEA 20934. 방울 마술 (C++)  (0) 2024.05.11
SWEA 3282. 0/1 Knapsack (C++)  (0) 2024.05.10
SWEA 19113. 식료품 가게 (C++)  (0) 2024.05.10
SWEA 9839. 최고의 쌍 (C++)  (0) 2024.05.08