Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define N 20
- /*
- Formato file
- n
- x0, y0
- matrice
- Nel file le righe e le colonne sono numerate da 1 a N e non da 0 a N-1
- */
- int valid(int x, int y, int n);
- int main() {
- int n,stop,i,j,x0,y0,x1,y1;
- int dx[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
- int dy[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
- float mat[N][N], max;
- char file_name[30];
- FILE* input;
- printf("Inserisci il nome del file da cui leggere la matrice: ");
- scanf("%s",file_name);
- input = fopen(file_name, "r");
- if(input == NULL) {
- printf("File non trovato.\n");
- return -1;
- }
- fscanf(input,"%d%d%d",&n,&x0,&y0);
- printf("(%d, %d)\n",x0,y0);
- x0--;
- y0--;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- fscanf(input,"%f",&mat[i][j]);
- fclose(input);
- stop = 0;
- while(!stop) {
- max=mat[x0][y0];
- for(i=0; i<8; i++) {
- if(valid(x0+dx[i],y0+dy[i],n) && mat[x0+dx[i]][y0+dy[i]]>max) {
- max=mat[x0+dx[i]][y0+dy[i]];
- x1=x0+dx[i];
- y1=y0+dy[i];
- }
- }
- if(max > mat[x0][y0]) {
- x0=x1;
- y0=y1;
- printf("(%d, %d)\n",x0+1,y0+1);
- } else
- stop=1;
- }
- return 0;
- }
- int valid(int x, int y, int n) {
- return (x>=0 && x<n && y>=0 && y<n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement