Zadani: ------ Jill Bates hates climbing hills. Jill rides a bicycle everywhere she goes, but she always wants to go the easiest and shortest way possible. The good news is that she lives in Greenhills, which has all its roads laid out in a strictly rectangular gridÑeast-west roads are streets; north-south roads are avenues and the distance between any two adjacent grid points is the same. The bad news is that Greenhills is very hilly and has many one-way roads. In choosing a route between where she starts and where she ends, Jill has three rules: 1. Avoid any climb of more than 10 meters between adjacent grid points. 2. Never go the wrong way on a one-way road. 3. Always travel the shortest possible route. Your program should help Jill find an acceptable route. Input The input file contains data in the following form: * The first line contains two integers, separated by one or more spaces. The first integer n represents the number of streets, and the second integer m represents the number of avenues, 1² n ²20, 1² m ²20. * The next n lines contain the altitudes of grid points. Each line represents a street and contains a sequence of m integers separated by one or more spaces. These integers represent the altitude in meters of the grid points along that street. Even if a particular street and avenue have no intersection, the altitude is still given for that grid point. * One or more lines follow that define the one-way roads. Each road is represented by two pairs of integers, separated by one or more spaces, in the form: street avenue street avenue The first street and avenue define the starting point of the road and the second pair define the ending point. Since Greenhills is a strict grid, if the two points are not adjacent in the grid, the road passes through all the intervening grid points. For example, 5 7 5 10 represents roads 5-7 to 5-8, 5-8 to 5-9, and 5-9 to 5-10. Road definitions are terminated by a line containing four zeroes in the above format. * Finally, one or more lines will follow that contain pairs of grid points between which Jill wants to find an optimal path, in the form: street avenue street avenue As before, the integer pairs are separated by one or more spaces. The end of the input set is defined by a line containing four zeroes, formatted as before. You may assume that all street and avenue numbers are within the bounds defined by the first line of input, and that all road definitions are strictly north-south or east-west. Output For each path query in the input file, output a sequence of grid points, from the starting grid point to the ending grid point, which meets Jill's three rules. Output grid points as 'street-avenue' separated by the word 'to'. If there is more than one path that meets Jill's criteria, any such path will be acceptable. If no route satisfies all the criteria, or if the starting and ending grid points are the same, output an appropriate message to that effect. Output a blank line between each output set. Sample Input 3 4 10 15 20 25 19 30 35 30 10 19 26 20 1 1 1 4 2 1 2 4 3 4 3 3 3 3 1 3 1 4 3 4 2 4 2 1 1 1 2 1 0 0 0 0 1 1 2 2 2 3 2 3 2 2 1 1 0 0 0 0 Output for the Sample Input 1-1 to 1-2 to 1-3 to 1-4 to 2-4 to 2-3 to 2-2 To get from 2-3 to 2-3, stay put! There is no acceptable route from 2-2 to 1-1. Popis problemu: -------------- Jedna se o znamy problem - hledani nejkratsi cesty. Uloha je zjednodusena tim, ze vsechny cesty jsou stejne dlouhe. Ruzne vysky jednotlivych bodu nejsou na prekazku, naopak nam zredukuji pocet cest. Popis reseni: ------------ V programu jsem pouzil upraveny algoritmus "prohledavani stromu do sirky". Jeho princip je vseobecne znam, proto jej zde nebudu popisovat. Problem maximalniho stoupani resim jiz pri nacitani site do pole tim, ze je-li toto stoupani vetsi nez pripustna mez, tuto cestu do pole neulozim. Urceni slozitosti: ----------------- Operace vkladani maji slozitost O(1). Kazdy uzel je maximalne jednou FRESH. Celkovy pocet operaci s frontou je tedy O(|U|). Seznam sousedu se prochazi pro kazdy uzel max jednou. Sousede jsou max 4. Jedna se tedy o konstatni casovou slozitost. Celkova casova slozitost je tedy O(|U|+4). Zaver: ----- Bohuzel se programovaci jazyk C teprve ucim, takze program jiste neni optimalne naprogramovan. Presto je vsak plne funkcni. Vypis programu: -------------- #include #include FILE *fp; #define FRESH 1 #define CLOSED 0 #define OPEN 2 void Konec(void) { fclose(fp);exit(1); } void Init(char *fname) { if ((fp=fopen(fname,"rt")) == NULL) {printf("\nFile opening error.\n");exit(1);} } int NextToken(void) { char c; int i=0; c=fgetc(fp); if (c==EOF) {printf("\nBad input.\n");Konec();} while (c==' ' || c=='\t' || c=='\n') c=fgetc(fp); while (isdigit(c)) {i=i*10+(c-'0');c=fgetc(fp);} ungetc(c,fp); return(i); } int fronta[40]; int f=0,l=0; void InitFronta (void) { f=0;l=0; } void UlozFronta(int a, int b) { fronta[f++]=a;fronta[f++]=b; } int PrazdnaFronta(void) { if (f==l) return 1; else return 0; } int FrontaFirst(void) { int v=fronta[l++]; if (l>40) l=0; return v; } int main (int argc, char *argv[]) { int mx,my,x,y,x1,y1,x2,y2,i,sx,sy; int px[20][20],py[20][20],cesta[40]; int pole[20][20][5]; //0 - vyska, visited //1-4 - smer printf("\n\n"); if (argc==1) Init("test.txt"); else Init(argv[1]); mx=NextToken(); my=NextToken(); if (mx>20 || mx<1 || my>20 || my<0) {printf("Number of roads must be 1..20");Konec();} for (x=0; x0) { printf("%d-%d",cesta[i--]+1,cesta[i--]+1); if (i>0) printf(" to "); } } } x=NextToken()-1;y=NextToken()-1;x2=NextToken()-1;y2=NextToken()-1; } return(0); }