Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #define MAX_N 51
  4.  
  5. int map[MAX_N][MAX_N];
  6. int d[MAX_N][MAX_N][MAX_N*MAX_N];
  7. int N, M;
  8. int ans;
  9. bool loop = false;
  10.  
  11. int dx[] = { 0, 0, 1, -1 };
  12. int dy[] = { 1, -1, 0, 0 };
  13.  
  14. void f(int x, int y, int cnt)
  15. {
  16. if (loop) return;
  17.  
  18. if (cnt > N*M) {
  19. loop = true;
  20. return;
  21. }
  22.  
  23. if (cnt > ans) ans = cnt;
  24.  
  25. for (int i = 0; i < 4; i++)
  26. {
  27. int nx = x + map[x][y] * dx[i];
  28. int ny = y + map[x][y] * dy[i];
  29. if (1 <= nx && nx <= N && 1 <= ny && ny <= M){
  30. if (d[nx][ny][cnt + 1] == 0 && map[nx][ny] != -1){
  31. d[nx][ny][cnt + 1] = 1;
  32. f(nx, ny, cnt + 1);
  33. }
  34. }
  35. }
  36. }
  37.  
  38. int main(){
  39. //freopen("input.txt", "r", stdin);
  40. scanf("%d %d", &N, &M);
  41.  
  42. char tmp[55];
  43.  
  44. for (int i = 1; i <= N; i++){
  45. scanf("%s", tmp + 1);
  46. for (int j = 1; j <= M; j++)
  47. {
  48. if (tmp[j] == 'H') map[i][j] = -1;
  49. else map[i][j] = tmp[j] - '0';
  50. }
  51. }
  52.  
  53. d[1][1][0] = 1;
  54. f(1, 1, 0);
  55.  
  56. if (loop) printf("-1\n");
  57. else printf("%d\n", ans + 1);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement