#include <iostream>
#include <vector>
using namespace std;
bool jump(vector<vector<int>>& board, int n, int xpos, int ypos) {
// 기저사례: 범위를 벗어난 경우
if (xpos >= n || ypos >= n) return false;
// 기저사례: 마지막 칸에 도달한 경우
if (xpos == n - 1 && ypos == n - 1) return true;
return jump(board, n, xpos + board[xpos][ypos], ypos) || jump(board, n, xpos, ypos + board[xpos][ypos]);
}
int main()
{
int n;
cin >> n;
vector<vector<int>> board(n, vector<int>(n, 0));
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++)
cin >> board[r][c];
if (jump(board, n, 0, 0))
cout << "Possible\n";
else
cout << "Impossible\n";
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int jump(vector<vector<int>>& cache, vector<vector<int>>& board, int n, int xpos, int ypos) {
// 기저사례: 범위를 벗어난 경우
if (xpos >= n || ypos >= n) return 0;
// 기저사례: 마지막 칸에 도달한 경우
if (xpos == n - 1 && ypos == n - 1) return 1;
int& ret = cache[xpos][ypos];
if (ret != -1) return ret;
return ret = jump(cache, board, n, xpos + board[xpos][ypos], ypos) || jump(cache, board, n, xpos, ypos + board[xpos][ypos]);
}
int main()
{
int n;
cin >> n;
vector<vector<int>> board(n, vector<int>(n, 0));
vector<vector<int>> cache(n, vector<int>(n, -1));
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++)
cin >> board[r][c];
if (jump(cache, board, n, 0, 0))
cout << "Possible\n";
else
cout << "Impossible\n";
return 0;
}
# 입력
<TC1>
7
2 5 1 6 1 4 1
6 1 1 2 2 9 3
7 2 3 2 1 3 1
1 1 3 1 7 1 2
4 1 2 3 4 1 2
3 3 1 2 3 4 1
1 5 2 9 4 7 1
<TC2>
7
2 5 1 6 1 4 1
6 1 1 2 2 9 3
7 2 3 2 1 3 1
1 1 3 1 7 1 2
4 1 2 3 4 1 3
3 3 1 2 3 4 1
1 5 2 9 4 7 1
# 출력
<TC1>
Possible
<TC2>
Impossible
Leave a comment