Per risolvere il problema, sono arrivato a usare questo codice:
#include <cstdio>
#include <cstdlib>
#include <cstring>
int getto(char from, char to)
{
if(from == to)
{
return 1;
}
if(from == '0')
{
from = ';';
}
if(to == '0')
{
to = ';';
}
return ((from - '1' % 3) - (to - '1' % 3)) + ((from - '1' / 3) - (to - '1' / 3)) + 1;
}
int main()
{
FILE *Input, *Output;
char next = '\0', current = '0';
int jumps = 0;
Input = fopen("input.txt", "r");
Output = fopen("output.txt", "w");
while(next != EOF)
{
fscanf(Input, "%c", &next);
jumps += getto(current, next);
current = next;
}
fprintf(Output, "%d", jumps);
return 0;
}
Ma l’esecuzione dura 0.3 secondi anziché 0.2.
Come faccio a ridurre il tempo d’esecuzione??