본문 바로가기
Algorithms/simulation

추억의 2048 게임

by OKOK 2019. 1. 30.

1. 2048

2. x, y 축 연습

3. 구현 연습

4. 생각한대로 짜는 연습

5. 예외 처리 확인하기

#include <stdio.h>
#define MAXN 6

int inMap[MAXN][MAXN];
int outMap[MAXN][MAXN];
int way;
char inCmd[10];
int N;

void init()
{
    int y, x;
    for (y = 0; y < N; y++)
    {
        for (x = 0; x < N; x++)
        {
            inMap[y][x] = 0;
            outMap[y][x] = 0;
        }
    }
}

void inputMap(int n)
{
    int y, x;
    for (y = 0; y < n; y++)
    {
        for (x = 0; x < n - 1; x++)
        {
            scanf("%d ", &inMap[y][x]); // space 가 있으므로
        }
        scanf("%d\n", &inMap[y][n - 1]); // 입력을 받음
    }
}

void showMap(int map[MAXN][MAXN], int n)
{
    int y, x;
    for (y = 0; y < n; y++)
    {
        for (x = 0; x < n; x++)
        {
            printf("%d ", map[y][x]);
        }
        printf("\n");
    }
}

void solve(char cmd, int n)
{
    int y, x;
    int outy, outx;
    int tmp;
    switch (cmd)
    {
    case 'u':
        for (x = 0; x < n; x++)
        {
            outy = 0;
            tmp = 0;
            for (y = 0; y < n;) {
                if (inMap[y][x] == 0) {
                    y++;
                    continue;
                }
                if (inMap[y][x] == tmp) {
                    outMap[outy - 1][x] = inMap[y][x] << 1;
                    tmp = 0;
                    y++;
                }
                else {
                    tmp = inMap[y][x];
                    outMap[outy][x] = tmp;
                    outy++;
                    y++;
                }
            }
        }
        break;
    case 'd':
        for (x = 0; x < n; x++) {
            outy = n - 1;
            tmp = 0;
            for (y = n - 1; y >= 0;) {
                if (inMap[y][x] == 0) {
                    y--;
                    continue;
                }
                if (inMap[y][x] == tmp) {
                    outMap[outy + 1][x] = inMap[y][x] << 1;
                    tmp = 0;
                    y--;
                }
                else {
                    tmp = inMap[y][x];
                    outMap[outy][x] = tmp;
                    outy--;
                    y--;
                }
            }
        }
        break;
    case 'l':
        for (y = 0; y < n; y++)
        {
            outx = 0;
            tmp = 0;
            for (x = 0; x < n;) {
                if (inMap[y][x] == 0) {
                    x++;
                    continue;
                }
                if (inMap[y][x] == tmp) {
                    outMap[y][outx - 1] = inMap[y][x] << 1;
                    tmp = 0;
                }
                else {
                    tmp = inMap[y][x];
                    outMap[y][outx] = tmp;
                    outx++;
                }
                x++;
            }
        }
        break;
    case'r':
        for (y = 0; y < n; y++) {
            outx = n - 1;
            tmp = 0;
            for (x = n - 1; x >= 0;) {
                if (inMap[y][x] == 0) {
                    x--;
                    continue;
                }
                if (inMap[y][x] == tmp) {
                    outMap[y][outx + 1] = inMap[y][x] << 1;
                    tmp = 0;
                }
                else {
                    tmp = inMap[y][x];
                    outMap[y][outx] = tmp;
                    outx--;
                }
                x--;
            }
        }
        break;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    int T;
    int t;
    scanf("%d\n", &T);
    for (t = 1; t <= T; t++)
    {
        scanf("%d %s\n", &N, inCmd);
        init();
        inputMap(N);
        solve(inCmd[0], N);
        printf("#%d\n", t);
        showMap(outMap, N);
    }
    return 0;
}

'Algorithms > simulation' 카테고리의 다른 글

Problem D : 계수정렬(counting sort)1  (0) 2019.11.05
그래프의 삼각형  (0) 2019.01.30
다솔이의 월급 상자  (0) 2019.01.30
희성이의 원근법  (0) 2019.01.30
장애물 경주 난이도  (0) 2019.01.30

댓글