fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int N; //크기
  7. bool flag; //정답 확인
  8. void solution(vector <vector <int>> g, vector <vector <int>> &c, int x, int y)
  9. {
  10. if (c[y][x] == 1) return; //이미 탐색한 구역이라면
  11. if (flag == true) return; //정답을 이미 확인했다면
  12. if (x == N - 1 && y == N - 1) flag = true;
  13. if (x + g[y][x] < N) solution(g, c, x + g[y][x], y); //오른쪽 탐색
  14. if (y + g[y][x] < N) solution(g, c, x, y + g[y][x]); //아래쪽 탐색
  15. c[y][x] = 1; //오른쪽과 아래쪽을 탐색이 끝났다면
  16. }
  17.  
  18. int main(void)
  19. {
  20. int C;
  21. cin >> C;
  22.  
  23. while (C--)
  24. {
  25. cin >> N;
  26. vector <vector <int>> ground;
  27. vector <vector <int>> cache(N, vector<int>(N, 0));
  28. for (int i = 0; i < N; i++)
  29. {
  30. vector <int> line;
  31. for (int j = 0; j < N; j++)
  32. {
  33. int temp;
  34. cin >> temp;
  35. line.push_back(temp);
  36. }
  37. ground.push_back(line);
  38. }
  39. flag = false;
  40. solution(ground, cache, 0, 0);
  41. if (flag) cout << "YES" << endl;
  42. else cout << "NO" << endl;
  43. }
  44. }
Success #stdin #stdout 0.01s 5672KB
stdin
2
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 0
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 0 
stdout
YES
NO