Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define rows 7
- #define cols 6
- void read(int terrain[rows][cols]){
- int i,j;
- for(i = 0; i < rows; i ++)
- for(j = 0; j < cols; j++)
- scanf("%d",&terrain[i][j]);
- }
- int isValid(int terrain[rows][cols],int x,int y){
- if(x>=0 && x<rows && y>=0 && y<cols && terrain[x][y]==-1)
- return 1;
- else
- return 0;
- }
- void follow(int terrain[rows][cols]){
- int x=0, y=0;
- int nextX=1, nextY=1;
- int step = 1;
- while(x != nextX || y != nextY){
- x = nextX;
- y = nextY;
- terrain[x][y] = step;
- step ++;
- if(isValid(terrain,x,y+1) == 1)
- nextY = y+1;
- else if(isValid(terrain,x,y-1) == 1)
- nextY = y-1;
- else if(isValid(terrain,x+1,y) == 1)
- nextX = x+1;
- else if(isValid(terrain,x-1,y) == 1)
- nextX = x-1;
- }
- }
- void print(int terrain[rows][cols]){
- int i,j;
- for(i=0; i<rows; i++){
- for(int j=0; j<cols; j++)
- printf("%d ",terrain[i][j]);
- printf("\n");
- }
- }
- int main(){
- int terrain[rows][cols];
- read(terrain);
- follow(terrain);
- print(terrain);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement