Advertisement
Guest User

Untitled

a guest
May 25th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define rows 7
  4. #define cols 6
  5.  
  6. void read(int terrain[rows][cols]){
  7.     int i,j;
  8.     for(i = 0; i < rows; i ++)
  9.         for(j = 0; j < cols; j++)
  10.             scanf("%d",&terrain[i][j]);
  11. }
  12.  
  13. int isValid(int terrain[rows][cols],int x,int y){
  14.     if(x>=0 && x<rows && y>=0 && y<cols && terrain[x][y]==-1)
  15.         return 1;
  16.     else
  17.         return 0;
  18. }
  19.  
  20. void follow(int terrain[rows][cols]){
  21.     int x=0, y=0;
  22.     int nextX=1, nextY=1;
  23.     int step = 1;
  24.    
  25.     while(x != nextX || y != nextY){
  26.         x = nextX;
  27.         y = nextY;
  28.        
  29.         terrain[x][y] = step;
  30.         step ++;
  31.        
  32.         if(isValid(terrain,x,y+1) == 1)
  33.             nextY = y+1;
  34.         else if(isValid(terrain,x,y-1) == 1)
  35.             nextY = y-1;
  36.         else if(isValid(terrain,x+1,y) == 1)
  37.             nextX = x+1;
  38.         else if(isValid(terrain,x-1,y) == 1)
  39.             nextX = x-1;
  40.     }
  41. }
  42.  
  43. void print(int terrain[rows][cols]){
  44.     int i,j;
  45.     for(i=0; i<rows; i++){
  46.         for(int j=0; j<cols; j++)
  47.             printf("%d ",terrain[i][j]);
  48.         printf("\n");
  49.     }
  50. }
  51.  
  52. int main(){
  53.     int terrain[rows][cols];
  54.    
  55.     read(terrain);
  56.     follow(terrain);
  57.     print(terrain);
  58.    
  59.     getchar();
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement