View Code of Problem 17

#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 min = arr[0][0], x = 0, y = 0;
	for(i = 0; i < n; i ++){
		if(i == 0 || i == n - 1){
			for(j = 0; j < m; j ++){
				if(j == 0 || j == m - 1){
					if(min > arr[i][j]){
						min = arr[i][j];
						x = i;
						y = j;
					}
				}
			}
		}
	} 
	
	int count = 0, flag;
	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.


Back to problem 17