JUMPGAME 도저히 모르겠네요..

  • gzone130
    gzone130

    종만북 보고 공부중입니다.

    C를 이용해서 아래와 같이 코드를 짜봤습니다.

    정답이랑은 제대로 나오는데 문제를 돌리면 틀렸다고 나옵니다..

    도대체 어디서 문제가 되는 건지 감이 잡히질 않습니다.

    도와주세요!

    #include<stdio.h>
    void init_cache(int(*cache)[101]);
    int jump(int (*cache)[101],int(*map)[101], int x, int y, int n);
    int main()
    {
        //freopen("input.txt", "r", stdin);
        int tc, t;
        int n, i, j;
        int map[101][101];
        int cache[101][101];
        scanf("%d", &tc);
        for (t = 1; t <= tc; t++) {
    
            init_cache(cache);
    
            scanf(" %d", &n);
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    scanf(" %d", &map[i][j]);
                }
            }
    
    
            if (jump(cache, map, 0, 0, n)>0)
                printf("YES\n");
            else
                printf("NO\n");
        }
    
        return 0;
    }
    
    void init_cache(int(*cache)[101])
    {
        int i, j;
        for (i = 0; i < 101; i++) {
            for (j = 0; j < 101; j++) {
                cache[i][j] = -1;
            }
        }
    }
    int jump(int(*cache)[101], int(*map)[101], int x, int y, int n)
    {
        int move = map[x][y];
        int *ret = &(cache[x][y]);
    
        if (x < 0 || y < 0 || x >= n || y >= n)
            return 0;
    
        if (x == n - 1 && y == n - 1)
            return 1;
    
        if (*ret != -1)
            return *ret;
    
        *ret = 0;
    
        *ret = jump(cache,map, x + move, y, n) + jump(cache,map, x, y + move, n);
    
        return *ret;
    }
    

    5년 전
2개의 댓글이 있습니다.
  • abc3242
    abc3242

    이유는 모르겠는데 *ret = 뒤에 있는 부분에서 더하기 대신에 || 하면 맞네요.


    5년 전 link
  • gzone130
    gzone130

    그렇게 하니 맞는데 이유를 모르겠군요..


    5년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.