JUMPGAME 시간초과ㅠ왜 그런가요?

  • happyer16
    happyer16
    #include <iostream>
    
    using namespace std;
    
    bool findPath = false;
    
    inline int jump(int current, int jump) { return current + jump; }
    
    void find(int* maze, int current, int size) {
        int move = maze[current];
        cout << "좌표 : (" << current%size << ", " << current / size << ")" << endl;
    
            if (move == 0) {
                findPath = true;
                return;
            }
            else {
                int right = jump(current, move);
                int down = jump(current, move * 7);
                if (right < (current / size + 1)*size && !findPath) {
                    find(maze, right, size);
                }
                if (down / size < size && !findPath) {
                    find(maze, down, size);
                }
            }
    
    }
    
    int main() {
        int testCase;
        int size;
    
        cin >> testCase;
        bool* find_result = new bool[testCase];
    
        for (int i = 0; i < testCase; i++) {
            cin >> size;
            int* maze = new int[size*size];
            for (int j = 0; j < size; j++) {
                int m = j*size;
                for (int k = 0; k < size; k++) {
                    cin >> maze[m + k];
                }
            }
    
            find(maze, 0, size);
            find_result[i] = findPath;
    
            delete[] maze;
            findPath = false;
        }
    
        for (int i = 0; i < testCase; i++) {
            if (find_result[i]) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        return 0;
    }
    

    8년 전
1개의 댓글이 있습니다.
  • Toivoa
    Toivoa

    방문했던 위치를 다시 방문했을 때 다시 계산하고 있네요


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