fork download
  1. #include <stdio.h>
  2.  
  3. int N, M;
  4. int teacher_x[1001], teacher_y[1001];
  5. int max_beauty = -1;
  6.  
  7. int move(int x, int y, int beauty) {
  8. // 이동이 불가능
  9. if (x < 0 || x > 2*N || y < 0 || y > N) return -1;
  10.  
  11. // 도착 지점에 도달
  12. if (x == 2*N && y == 0) {
  13. max_beauty = (max_beauty < beauty ? beauty : max_beauty);
  14. return 1;
  15. }
  16.  
  17. // 선생님과 만남.
  18. for (int i = 0; i < M; i++) {
  19. if (x == teacher_x[i] && y == teacher_y[i]) return -1;
  20. }
  21.  
  22. // 다음 진행.
  23. move(x + 1, y + 1, (y + 1 > beauty ? y + 1 : beauty));
  24. move(x + 1, y - 1, (y - 1 > beauty ? y - 1 : beauty));
  25.  
  26. return -1;
  27. }
  28.  
  29. int main() {
  30. scanf("%d %d", &N, &M);
  31. for (int i = 0; i < M; i++) {
  32. scanf("%d %d", &teacher_x[i], &teacher_y[i]);
  33. }
  34.  
  35. move(0, 0, 0);
  36. printf("%d\n", max_beauty);
  37.  
  38. return 0;
  39. }
  40.  
  41.  
Success #stdin #stdout 0.01s 5292KB
stdin
3 1
1 1
stdout
-1