quetzelcoatlus

12b.c

Dec 12th, 2022 (edited)
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_WIDTH 200
  6. #define MAX_HEIGHT 50
  7. #define BIG_NUMBER 2137
  8.  
  9. char tab[MAX_HEIGHT][MAX_WIDTH];
  10. int values[MAX_HEIGHT][MAX_WIDTH];
  11. int h=0,w;
  12.  
  13. void resolve(int y, int x, int val){
  14.     if(x<0 || y<0 || x==w || y==h) return;
  15.     val++;
  16.     if(values[y][x] == -1 || val < values[y][x]){
  17.         values[y][x] = val;
  18.  
  19.         for(int i=-1;i<=1;i++)
  20.             for(int j=-1;j<=1;j++)
  21.                 if(abs(i)+abs(j)==1)
  22.                     if(tab[y+j][x+i] >= tab[y][x]-1)
  23.                         resolve(y+j,x+i,val);
  24.     }
  25. }
  26.  
  27. void main(){
  28.     int ex,ey;
  29.  
  30.     while(scanf("%s\n",tab[h++]) == 1);
  31.     w=strlen(tab[0]);
  32.  
  33.     for(int i=0;i<h;i++){
  34.         for(int j=0;j<w;j++){
  35.             if(tab[i][j] == 'S'){
  36.                 tab[i][j]='a';
  37.             } else if (tab[i][j] == 'E'){
  38.                 ey=i;
  39.                 ex=j;
  40.                 tab[i][j]='z';
  41.             }
  42.  
  43.             values[i][j]=-1;
  44.         }
  45.     }
  46.  
  47.     resolve(ey,ex,-1);
  48.  
  49.     int min=BIG_NUMBER;
  50.  
  51.     for(int i=0;i<h;i++)
  52.         for(int j=0;j<w;j++)
  53.             if(tab[i][j] == 'a' && values[i][j] != -1 && values[i][j] < min)
  54.                 min = values[i][j];
  55.  
  56.     printf("min = %d\n", min);
  57. }
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment