Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAXN 1005
- #define MAX 105
- int m, n, pos = 0;
- int mat[MAX][MAX];
- int memo[MAX][MAX];
- int x[MAXN];
- int y[MAXN];
- int main() {
- scanf("%d %d", &m, &n);
- scanf("%d %d", &x[pos], &y[pos]);
- pos++;
- for(int i = 1; i <= m; i++) {
- for(int j = 1; j <= n; j++) {
- scanf("%d", &mat[i][j]);
- memo[i][j] = 0;
- }
- }
- //printf("Hola\n");
- memo[x[pos-1]][y[pos-1]] = 1;
- while(pos) {
- //NORTE
- 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]]) {
- memo[x[pos-1]-1][y[pos-1]] = 1;
- x[pos] = x[pos-1]-1;
- y[pos] = y[pos-1];
- //printf("NORTE\n");
- pos++;
- }//SUR
- 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]]) {
- memo[x[pos-1]+1][y[pos-1]] = 1;
- x[pos] = x[pos-1]+1;
- y[pos] = y[pos-1];
- //printf("SUR\n");
- pos++;
- }//ESTE
- 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]]) {
- memo[x[pos-1]][y[pos-1]+1] = 1;
- x[pos] = x[pos-1];
- y[pos] = y[pos-1]+1;
- //printf("ESTE\n");
- pos++;
- }//OESTE
- 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]]) {
- memo[x[pos-1]][y[pos-1]-1] = 1;
- x[pos] = x[pos-1];
- y[pos] = y[pos-1]-1;
- //printf("OESTE\n");
- pos++;
- }//NOROESTE
- 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]]) {
- memo[x[pos-1]-1][y[pos-1]-1] = 1;
- x[pos] = x[pos-1]-1;
- y[pos] = y[pos-1]-1;
- //printf("NOROESTE\n");
- pos++;
- }//NORESTE
- 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]]) {
- memo[x[pos-1]-1][y[pos-1]+1] = 1;
- x[pos] = x[pos-1]+1;
- y[pos] = y[pos-1]-1;
- //printf("NORESTE\n");
- pos++;
- }//SUROESTE
- 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]]) {
- memo[x[pos-1]+1][y[pos-1]-1] = 1;
- x[pos] = x[pos-1]+1;
- y[pos] = y[pos-1]-1;
- //printf("SUROESTE\n");
- pos++;
- }//SURESTE
- 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]]) {
- memo[x[pos-1]+1][y[pos-1]+1] = 1;
- x[pos] = x[pos-1]+1;
- y[pos] = y[pos-1]+1;
- //printf("SURESTE\n");
- pos++;
- }
- else {
- pos--;
- }
- }
- //printf("\n");
- for(int i = 1; i <= m; i++) {
- for(int j = 1; j <= n; j++) {
- if(memo[i][j])
- printf("X ");
- else
- printf("%d ", mat[i][j]);
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement