#include<stdio.h> #include<stdlib.h> void main(){ int n,m; scanf("%d%d",&n,&m); int i, j, arr[n][m]; for(i = 0; i < n; i ++){ for(j = 0; j < m; j ++){ scanf("%d", &arr[i][j]); } } int count = 0, flag,x = 0, y = 0; while(1){ flag = 0; //向右爬 if(x + 1 < n && arr[x + 1][y] == arr[x][y] + 1){ x = x + 1; flag = 1; count ++; } //向下爬 if(y + 1 < m && arr[x][y + 1] == arr[x][y] + 1){ y = y + 1; flag = 1; count ++; } //向左爬 if(x - 1 >= 0 && arr[x - 1][y] == arr[x][y] + 1){ x = x - 1; flag = 1; count ++; } //向上爬 if(y - 1 >= 0 && arr[x][y - 1] == arr[x][y] + 1){ y = y - 1; flag = 1; count ++; } if(flag == 0){ break; } } printf("%d\n", count); } |
Double click to view unformatted code.