Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #define N 20
  3.  
  4. /*
  5. Formato file
  6. n
  7. x0, y0
  8. matrice
  9.  
  10. Nel file le righe e le colonne sono numerate da 1 a N e non da 0 a N-1
  11. */
  12. int valid(int x, int y, int n);
  13.  
  14. int main() {
  15.     int n,stop,i,j,x0,y0,x1,y1;
  16.     int dx[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
  17.     int dy[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
  18.     float mat[N][N], max;
  19.     char file_name[30];
  20.     FILE* input;
  21.     printf("Inserisci il nome del file da cui leggere la matrice: ");
  22.     scanf("%s",file_name);
  23.     input = fopen(file_name, "r");
  24.     if(input == NULL) {
  25.         printf("File non trovato.\n");
  26.         return -1;
  27.     }
  28.     fscanf(input,"%d%d%d",&n,&x0,&y0);
  29.     printf("(%d, %d)\n",x0,y0);
  30.     x0--;
  31.     y0--;
  32.     for(i=0;i<n;i++)
  33.         for(j=0;j<n;j++)
  34.             fscanf(input,"%f",&mat[i][j]);
  35.     fclose(input);
  36.     stop = 0;
  37.     while(!stop) {
  38.         max=mat[x0][y0];
  39.         for(i=0; i<8; i++) {
  40.             if(valid(x0+dx[i],y0+dy[i],n) && mat[x0+dx[i]][y0+dy[i]]>max) {
  41.                 max=mat[x0+dx[i]][y0+dy[i]];
  42.                 x1=x0+dx[i];
  43.                 y1=y0+dy[i];
  44.             }
  45.         }
  46.         if(max > mat[x0][y0]) {
  47.             x0=x1;
  48.             y0=y1;
  49.             printf("(%d, %d)\n",x0+1,y0+1);
  50.         } else
  51.             stop=1;
  52.     }
  53.     return 0;
  54. }
  55.  
  56. int valid(int x, int y, int n) {
  57.     return (x>=0 && x<n && y>=0 && y<n);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement