Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #define MAX_N 51
- int map[MAX_N][MAX_N];
- int d[MAX_N][MAX_N][MAX_N*MAX_N];
- int N, M;
- int ans;
- bool loop = false;
- int dx[] = { 0, 0, 1, -1 };
- int dy[] = { 1, -1, 0, 0 };
- void f(int x, int y, int cnt)
- {
- if (loop) return;
- if (cnt > N*M) {
- loop = true;
- return;
- }
- if (cnt > ans) ans = cnt;
- for (int i = 0; i < 4; i++)
- {
- int nx = x + map[x][y] * dx[i];
- int ny = y + map[x][y] * dy[i];
- if (1 <= nx && nx <= N && 1 <= ny && ny <= M){
- if (d[nx][ny][cnt + 1] == 0 && map[nx][ny] != -1){
- d[nx][ny][cnt + 1] = 1;
- f(nx, ny, cnt + 1);
- }
- }
- }
- }
- int main(){
- //freopen("input.txt", "r", stdin);
- scanf("%d %d", &N, &M);
- char tmp[55];
- for (int i = 1; i <= N; i++){
- scanf("%s", tmp + 1);
- for (int j = 1; j <= M; j++)
- {
- if (tmp[j] == 'H') map[i][j] = -1;
- else map[i][j] = tmp[j] - '0';
- }
- }
- d[1][1][0] = 1;
- f(1, 1, 0);
- if (loop) printf("-1\n");
- else printf("%d\n", ans + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement