00001
00005 #include "bestroute.h"
00006
00007
00008
00009 unsigned int bestroute( unsigned int *doorsRow,
00010 unsigned int *doorsCol,
00011 unsigned int numOfDoor,
00012 unsigned int *doorId
00013 )
00014 {
00015 D1(printf("\nBESTROUTE.bestroute() - START\n"));
00016 D4(printf("\nParameters in: %u %u %u %u %u\n",
00017 schemaData.numRow,
00018 schemaData.numCol,
00019 schemaData.startRow,
00020 schemaData.startCol,
00021 numOfDoor ));
00022
00023
00024
00025 unsigned int bestRouteRet = schemaData.numRow * schemaData.numCol + 1;
00026
00027 if(!bestroute_check_in_data(doorsRow,doorsCol,numOfDoor))
00028 return bestRouteRet;
00029
00030 unsigned int *vectRow = (unsigned int *)malloc( sizeof(unsigned int)*
00031 (schemaData.numRow*schemaData.numCol) );
00032 unsigned int *vectCol = (unsigned int *)malloc( sizeof(unsigned int)*
00033 (schemaData.numRow*schemaData.numCol) );
00034 long int *vectParent = (long int *)malloc( sizeof(long int)*
00035 (schemaData.numRow*schemaData.numCol) );
00036
00037 vectRow[0] = schemaData.startRow;
00038 vectCol[0] = schemaData.startCol;
00039 vectParent[0] = -1;
00040
00041 unsigned int numOfNodes = schema_round_search(vectRow, vectCol, vectParent);
00042
00043 unsigned int i = 0, j = 0;
00044
00045 for( i=0; i < numOfDoor; i++ )
00046 {
00047 assert( i < numOfDoor );
00048
00049 bool found = false;
00050 for( j=0; ( j <= numOfNodes ) && ( ! found ); j++ )
00051 {
00052 assert( (j <= numOfNodes ) && (!found) );
00053
00054 D4(printf("\nIs a door: (%u,%u) == (%u,%u)?",
00055 vectRow[j],vectCol[j],doorsRow[i],doorsCol[i] ));
00056
00057 if( ! ( ( vectRow[j] == doorsRow[i] ) &&
00058 ( vectCol[j] == doorsCol[i] ) )
00059 )
00060 continue;
00061
00062 assert( ( vectRow[j] == doorsRow[i] ) &&
00063 ( vectCol[j] == doorsCol[i] )
00064 );
00065
00066
00067 found = true;
00068
00069 unsigned int lenRoute = reverse_route_length( vectRow, vectCol,
00070 vectParent, j );
00071
00072 D2(printf("\ndoor = %u , route weigth= %u",i,lenRoute));
00073
00074 if(lenRoute<bestRouteRet){
00075 bestRouteRet = lenRoute;
00076 *doorId = i;
00077 }
00078 }
00079 }
00080
00081 free( vectRow );
00082 free( vectCol );
00083 free( vectParent );
00084
00085 D1(printf("\nBESTROUTE.bestroute() - END\n"));
00086
00087 return bestRouteRet;
00088 }
00089
00090
00091 bool bestroute_check_in_data( const unsigned int *doorsRow,
00092 const unsigned int *doorsCol,
00093 unsigned int numOfDoor
00094 )
00095 {
00096
00097 D1(printf("\nBESTROUTE.bestroute_check_in_data() - START\n"));
00098
00099 if( schemaData.numRow > MAXR ){
00100 D1(printf("\nSchema rows exceed maximum allowed (%u)\n",MAXR));
00101 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00102 return false;
00103 }
00104 assert( ! (schemaData.numRow > MAXR) );
00105
00106 if( schemaData.numRow < MINR ){
00107 D1(printf("\nSchema rows lower then minimum allowed (%u)\n",MINR));
00108 D1(printf("\nBESTROUTE.bestroute() - Error & END\n"));
00109 return false;
00110 }
00111 assert( ! ( schemaData.numRow < MINR ) );
00112
00113 if( schemaData.numCol > MAXC ){
00114 D1(printf("\nSchema columns exceed maximum allowed (%u)\n",MAXC));
00115 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00116 return false;
00117 }
00118 assert( ! ( schemaData.numCol > MAXC ) );
00119
00120 if( schemaData.numCol < MINC ){
00121 D1(printf("\nSchema columns lower then minimum allowed (%u)\n",MINC));
00122 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00123 return false;
00124 }
00125 assert( ! ( schemaData.numCol < MINC ) );
00126
00127 const unsigned short int INSUFFICIENT_CELL_NUMBER = 2;
00128 if( (schemaData.numRow * schemaData.numCol) < INSUFFICIENT_CELL_NUMBER ){
00129 D1(printf("\nNot enough cell to set a START point and a DOOR\n"));
00130 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00131 return false;
00132 }
00133 assert(! ((schemaData.numRow*schemaData.numCol)<INSUFFICIENT_CELL_NUMBER));
00134
00135 const unsigned short int INSUFFICIENT_DOOR_NUMBER = 1;
00136 if( numOfDoor < INSUFFICIENT_DOOR_NUMBER ){
00137 D1(printf("\nNeed at least one DOOR\n"));
00138 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00139 return false;
00140 }
00141 assert( ! ( numOfDoor < INSUFFICIENT_DOOR_NUMBER ) );
00142
00143 unsigned int it = 0;
00144 for(it=0; it<numOfDoor; it++){
00145 if( (doorsRow[it] >= schemaData.numRow ) ||
00146 (doorsCol[it] >= schemaData.numCol )
00147 )
00148 {
00149 D1(printf("\nDOOR %u (%u,%u) out of schema\n",
00150 it,doorsRow[it],doorsCol[it] ));
00151 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00152 return false;
00153 }
00154 assert( ! ( (doorsRow[it] >= schemaData.numRow ) ||
00155 (doorsCol[it] >= schemaData.numCol ) )
00156 );
00157 }
00158
00159 if( (schemaData.startRow >= schemaData.numRow) ||
00160 (schemaData.startCol >= schemaData.numCol)
00161 )
00162 {
00163 D1(printf("\nSTART point out of schema\n"));
00164 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00165 return false;
00166 }
00167 assert( ! ( (schemaData.startRow >= schemaData.numRow) ||
00168 (schemaData.startCol >= schemaData.numCol) )
00169 );
00170
00171 for(it=0; it<numOfDoor; it++){
00172 if( (doorsRow[it] == schemaData.startRow ) &&
00173 (doorsCol[it] == schemaData.startCol )
00174 )
00175 {
00176 D1(printf("\nDOOR %u (%u,%u) has the same coordinates of the"
00177 ,it,doorsRow[it],doorsCol[it]));
00178 D1(printf(" START POINT (%u,%u)\n"
00179 ,schemaData.startRow, schemaData.startCol));
00180 D1(printf("\nBESTROUTE.bestroute_check_in_data() - Error & END\n"));
00181 return false;
00182 }
00183 assert( ! ( (doorsRow[it] == schemaData.startRow ) &&
00184 (doorsCol[it] == schemaData.startCol ) )
00185 );
00186 }
00187
00188 D1(printf("\nBESTROUTE.bestroute_check_in_data() - END\n"));
00189
00190
00191 return true;
00192 }
00193
00194 unsigned int schema_round_search( unsigned int *vectRow,
00195 unsigned int *vectCol,
00196 long int *vectParent
00197 )
00198 {
00199
00200 D1(printf("\nBESTROUTE.schema_round_search() - START\n"));
00201
00202 unsigned int i = 0, j = 0;
00203
00204 bool mapCopy [ schemaData.numRow ] [ schemaData.numCol ];
00205
00206 for( i=0; i < schemaData.numRow; i++ )
00207 for( j=0; j < schemaData.numCol; j++ )
00208 mapCopy[i][j] = isaccessible( schemaData.gameMap[i][j] );
00209
00210 unsigned int findNodes = 0;
00211 i = 0;
00212
00213 while( i <= findNodes)
00214 {
00215 assert( i <= findNodes );
00216 D3(printf("\nWhile at %u of (%u)\n",i,(findNodes+1)));
00217 D3(printf("\nnode %u %u %li\n",vectRow[i],vectCol[i],vectParent[i]));
00218
00219 if( ( vectCol[i] > 0 ) &&
00220 ( mapCopy[vectRow[i]][vectCol[i]-1] ) )
00221 {
00222 findNodes++;
00223 vectRow[findNodes] = vectRow[i];
00224 vectCol[findNodes] = vectCol[i]-1;
00225 vectParent[findNodes] = i;
00226
00227 D5( printf("\nSX = schemaData.gameMap[%u][%u] = %c (int %i)",
00228 vectRow[i],
00229 (vectCol[i]-1),
00230 schemaData.gameMap[vectRow[i]][vectCol[i]-1],
00231 schemaData.gameMap[vectRow[i]][vectCol[i]-1]
00232 )
00233 );
00234
00235 mapCopy[vectRow[i]][vectCol[i]-1] = false;
00236 }
00237
00238 if( ( vectRow[i] > 0 ) &&
00239 ( mapCopy[vectRow[i]-1][vectCol[i]] ) )
00240 {
00241 findNodes++;
00242 vectRow[findNodes] = vectRow[i]-1;
00243 vectCol[findNodes] = vectCol[i];
00244 vectParent[findNodes] = i;
00245
00246 D5( printf("\nUP = schemaData.gameMap[%u][%u] = %c (int %i)",
00247 (vectRow[i]-1),
00248 vectCol[i],
00249 schemaData.gameMap[vectRow[i]-1][vectCol[i]],
00250 schemaData.gameMap[vectRow[i]-1][vectCol[i]]
00251 )
00252 );
00253
00254 mapCopy[vectRow[i]-1][vectCol[i]] = false;
00255 }
00256
00257 if( ( vectCol[i] < (schemaData.numCol-1) ) &&
00258 ( mapCopy[vectRow[i]][vectCol[i]+1] ) )
00259 {
00260 findNodes++;
00261 vectRow[findNodes] = vectRow[i];
00262 vectCol[findNodes] = vectCol[i]+1;
00263 vectParent[findNodes] = i;
00264
00265 D5( printf("\nDX = schemaData.gameMap[%u][%u] = %c (int %i)",
00266 vectRow[i],
00267 (vectCol[i]+1),
00268 schemaData.gameMap[vectRow[i]][vectCol[i]+1],
00269 schemaData.gameMap[vectRow[i]][vectCol[i]+1]
00270 )
00271 );
00272
00273 mapCopy[vectRow[i]][vectCol[i]+1] = false;
00274 }
00275
00276 if( ( vectRow[i] < (schemaData.numRow-1) ) &&
00277 ( mapCopy[vectRow[i]+1][vectCol[i]] ) )
00278 {
00279 findNodes++;
00280 vectRow[findNodes] = vectRow[i]+1;
00281 vectCol[findNodes] = vectCol[i];
00282 vectParent[findNodes] = i;
00283
00284 D5( printf("\nDW = schemaData.gameMap[%u][%u] = %c (int %i)",
00285 (vectRow[i]+1),
00286 vectCol[i],
00287 schemaData.gameMap[vectRow[i]+1][vectCol[i]],
00288 schemaData.gameMap[vectRow[i]+1][vectCol[i]]
00289 )
00290 );
00291
00292 mapCopy[vectRow[i]+1][vectCol[i]] = false;
00293 }
00294
00295 D3(printf("\nUpdates neighbours at iteration %u: %u\n",i,findNodes));
00296
00297 i++;
00298 }
00299
00300 D1(printf("\nBESTROUTE.schema_round_search() - END\n"));
00301
00302 return findNodes;
00303 }
00304
00305
00306 unsigned int reverse_route_length( unsigned int *vectRow,
00307 unsigned int *vectCol,
00308 long int *vectParent,
00309 long int doorIndex
00310 )
00311 {
00312
00313 D1(printf("\nBESTROUTE.reverse_route_length() - START\n"));
00314
00315 unsigned int lenRoute = 0;
00316
00317 D2(printf("\nEND->"));
00318
00319 do
00320 {
00321 lenRoute++;
00322
00323 D2(
00324 const unsigned int lineToShow = 10;
00325 if( ( lenRoute % lineToShow ) == 0 )
00326 printf("\n");
00327 printf("(%u,%u)->",vectRow[doorIndex],vectCol[doorIndex]);
00328 );
00329
00330 doorIndex = vectParent[ doorIndex ];
00331
00332 }
00333 while( doorIndex != -1 );
00334
00335 D2( printf("START\n") );
00336
00337 D1(printf("\nBESTROUTE.reverse_route_length() - END\n"));
00338
00339 return lenRoute;
00340 }
00341