Advertisement
Alexvans

Untitled

Jun 2nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAXN 1005
  3. #define MAX 105
  4.  
  5. int m, n, pos = 0;
  6. int mat[MAX][MAX];
  7. int memo[MAX][MAX];
  8. int x[MAXN];
  9. int y[MAXN];
  10.  
  11. int main() {
  12.     scanf("%d %d", &m, &n);
  13.     scanf("%d %d", &x[pos], &y[pos]);
  14.     pos++;
  15.  
  16.     for(int i = 1; i <= m; i++) {
  17.         for(int j = 1; j <= n; j++) {
  18.             scanf("%d", &mat[i][j]);
  19.             memo[i][j] = 0;
  20.         }
  21.     }
  22.     //printf("Hola\n");
  23.     memo[x[pos-1]][y[pos-1]] = 1;
  24.  
  25.     while(pos) {
  26.         //NORTE
  27.         if(x[pos-1] > 1 && !memo[x[pos-1]-1][y[pos-1]] && mat[x[pos-1]-1][y[pos-1]] < mat[x[pos-1]][y[pos-1]]) {
  28.             memo[x[pos-1]-1][y[pos-1]] = 1;
  29.             x[pos] = x[pos-1]-1;
  30.             y[pos] = y[pos-1];
  31.             //printf("NORTE\n");
  32.             pos++;
  33.         }//SUR
  34.         else if(x[pos-1] < m && !memo[x[pos-1]+1][y[pos-1]] && mat[x[pos-1]+1][y[pos-1]] < mat[x[pos-1]][y[pos-1]]) {
  35.             memo[x[pos-1]+1][y[pos-1]] = 1;
  36.             x[pos] = x[pos-1]+1;
  37.             y[pos] = y[pos-1];
  38.             //printf("SUR\n");
  39.             pos++;
  40.         }//ESTE
  41.         else if(y[pos-1] < n && !memo[x[pos-1]][y[pos-1]+1] && mat[x[pos-1]][y[pos-1]+1] < mat[x[pos-1]][y[pos-1]]) {
  42.             memo[x[pos-1]][y[pos-1]+1] = 1;
  43.             x[pos] = x[pos-1];
  44.             y[pos] = y[pos-1]+1;
  45.             //printf("ESTE\n");
  46.             pos++;
  47.         }//OESTE
  48.         else if(y[pos-1] > 1 && !memo[x[pos-1]][y[pos-1]-1] && mat[x[pos-1]][y[pos-1]-1] < mat[x[pos-1]][y[pos-1]]) {
  49.             memo[x[pos-1]][y[pos-1]-1] = 1;
  50.             x[pos] = x[pos-1];
  51.             y[pos] = y[pos-1]-1;
  52.             //printf("OESTE\n");
  53.             pos++;
  54.         }//NOROESTE
  55.         else if(x[pos-1] > 1 && y[pos-1] > 1 && !memo[x[pos-1]-1][y[pos-1]-1] && mat[x[pos-1]-1][y[pos-1]-1] < mat[x[pos-1]][y[pos-1]]) {
  56.             memo[x[pos-1]-1][y[pos-1]-1] = 1;
  57.             x[pos] = x[pos-1]-1;
  58.             y[pos] = y[pos-1]-1;
  59.             //printf("NOROESTE\n");
  60.             pos++;
  61.         }//NORESTE
  62.         else if(x[pos-1] > 1 && y[pos-1] < n && !memo[x[pos-1]-1][y[pos-1]+1] && mat[x[pos-1]-1][y[pos-1]+1] < mat[x[pos-1]][y[pos-1]]) {
  63.             memo[x[pos-1]-1][y[pos-1]+1] = 1;
  64.             x[pos] = x[pos-1]+1;
  65.             y[pos] = y[pos-1]-1;
  66.             //printf("NORESTE\n");
  67.             pos++;
  68.         }//SUROESTE
  69.         else if(x[pos-1] < m && y[pos-1] > 1 && !memo[x[pos-1]+1][y[pos-1]-1] && mat[x[pos-1]+1][y[pos-1]-1] < mat[x[pos-1]][y[pos-1]]) {
  70.             memo[x[pos-1]+1][y[pos-1]-1] = 1;
  71.             x[pos] = x[pos-1]+1;
  72.             y[pos] = y[pos-1]-1;
  73.             //printf("SUROESTE\n");
  74.             pos++;
  75.         }//SURESTE
  76.         else if(x[pos-1] < m && y[pos-1] < n && !memo[x[pos-1]+1][y[pos-1]+1] && mat[x[pos-1]+1][y[pos-1]+1] < mat[x[pos-1]][y[pos-1]]) {
  77.             memo[x[pos-1]+1][y[pos-1]+1] = 1;
  78.             x[pos] = x[pos-1]+1;
  79.             y[pos] = y[pos-1]+1;
  80.             //printf("SURESTE\n");
  81.             pos++;
  82.         }
  83.         else {
  84.             pos--;
  85.         }
  86.     }
  87.     //printf("\n");
  88.     for(int i = 1; i <= m; i++) {
  89.         for(int j = 1; j <= n; j++) {
  90.             if(memo[i][j])
  91.                 printf("X ");
  92.             else
  93.                 printf("%d ", mat[i][j]);
  94.         }
  95.         printf("\n");
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement