BOJ/DP

[C/C++] 알고스팟 - 외발 뛰기

JWonK 2021. 7. 12. 16:51
728x90
반응형

https://algospot.com/judge/problem/read/JUMPGAME

 

algospot.com :: JUMPGAME

외발 뛰기 문제 정보 문제 땅따먹기를 하다 질린 재하와 영훈이는 땅따먹기의 변종인 새로운 게임을 하기로 했습니다. 이 게임은 그림과 같이 n*n 크기의 격자에 각 1부터 9 사이의 정수를 쓴 상

algospot.com

 

메모제이션 기법을 통해서만 해결 가능한 문제

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#define MAX_SIZE 101
#define swap(type,x,y) do{type t=x; x=y;y=t;} while(0)

using namespace std;

int N;
int adj[MAX_SIZE][MAX_SIZE];
int graph[MAX_SIZE][MAX_SIZE];

int isPossible(int y, int x) {
	if (y >= N || x >= N) return 0;
	if (y == N - 1 && x == N - 1) return 1;
	if (adj[y][x] != -1) return adj[y][x];
	int jump = graph[y][x];
	return adj[y][x] = (isPossible(y + jump, x) || isPossible(y, x + jump));
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int test_case;
	cin >> test_case;
	
	while (test_case--) {
		cin >> N;
		memset(adj, -1, sizeof(adj));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				cin >> graph[i][j];
			}
		}
		if (isPossible(0,0)) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	
	return 0;
}
728x90
반응형