int main() { int x, y; WINDOW *win; win = initscr(); curs_set(0); getmaxyx(win, y, x); width = x / 2 - 1; height = y / 2 - 1; createMaze(); do { clear(); refresh(); initMaze(); buildMaze(0, 0); printMaze(); solveMaze(); move(height * 2 + 1, 0); printw("Press 'q' to quit or any other key to run again."); refresh(); } while (getchar() != 'q'); clear(); refresh(); endwin(); return EXIT_SUCCESS; }
// driver program to test above function int main() { for(i=0;i<m;i++) { for(j=0;j<n;j++) { sol[i][j]=0; } } int i,j; scanf("%d",&m); scanf("%d",&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&maze[i][j]); } } solveMaze(maze); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d",sol[i][j]); } printf("\n"); } return 0; }
/** * Read in an ascii maze, solve it and output it with the solution. * @return 1 if there is no path found. */ int main(int /* argc */ , char * /* argv[] */ ) { maze m; m.width = m.height = 0; m.rows.resize(2); // Read the maze read(&m); // Choose start and ending points for the maze m.destX = m.width - 1; m.destY = 0; uint startX = 0; uint startY = m.height; // Attempt to find the solution int isSolvable = solveMaze(&m, startX, startY, EMPTY); if (isSolvable) write(&m); else fprintf(stderr, "No path found through maze.\n"); // Memory cleanup for (uint row = 0; row <= m.height; row++) delete[](m.rows[row]); return isSolvable; }
main() { int row, col, solved; /* Input validation */ do{ printf("Please enter the size of the maze.\n" "Values must be between 5 and 100, inclusive. (e.g. 10,15)\n"); scanf("%d,%d", &row, &col); while(getchar() != '\n'); } while( !(isValid(row, MIN_SIZE, MAX_SIZE) && isValid(col, MIN_SIZE, MAX_SIZE)) ); char maze[row * col], file_name[20]; /* Check if file_name exists */ do{ printf("Type in the name of the file containing the maze\n"); scanf("%s", file_name); } while(access(file_name, F_OK) == -1); readMaze(maze, row, col, file_name); solved = solveMaze(maze, row, col); /* Output */ printMaze(maze, row, col); if (solved) printf("\nPath found.\n"); else printf("\nNo path found.\n"); }
void main() { int maze[4][4] = {{1, 0, 0, 0}, {1, 1, 0, 1},{0, 1, 0, 0},{1, 1, 1, 1}}; int solution[4][4] = {{0}}; int x=0,y=0; if(solveMaze(maze,solution,x,y)) printMatrix(solution); }
int main(){ init(); //setups up robot variables and peripherals mapMaze(); //Commences mapping process while(solveMaze()) ; //Solves maze over and over again until an error occurs return 1; }
bool solveMaze(int maze[4][4], int solution[4][4], int x, int y) { // Maze ends at [3][3] or [N-1][N-1] if(x==N-1 && y==N-1) { solution[x][y]=1; return true; } if(maze[x][y] == 1) { solution[x][y]=1; if (solveMaze(maze,solution,x+1,y)) return true; if (solveMaze(maze,solution,x,y+1)) return true; solution[x][y]=0; } return false; }
// driver program to test above function int main() { int maze[N][N] = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; solveMaze(maze); return 0; }
/** * Check if the next cell at point (x,y) coming from the previous * cell parent the direction from to see if it is the end of maze m. * If it isn't try the cells other directions to see if they lead there. * * @param x - the starting xcord * @param y - the starting ycord * @param from - direction just came from * @return true if x,y is part of the solution. */ bool solveMaze(maze * m, uint x, uint y, int from) { // This if shouldn't be needed if read() performed correctly //if( m->height < y || x > m->width ) // return; short cell = m->rows[y][x]; #ifdef CHECKEDFORBRAIDS if (cell & CHECKED) return false; m->rows[y][x] |= CHECKED; #endif if (x == m->destX && y == m->destY) { // Don't bother checking its children solutionCell(m, x, y); return true; } bool foundEnd = false; // Don't go back the way you just came if (from != RIGHT && cell & LEFT) foundEnd = solveMaze(m, x - 1, y, LEFT); if (!foundEnd && (from != DOWN && (cell & UP))) foundEnd = solveMaze(m, x, y - 1, UP); if (!foundEnd && (from != UP && (cell & DOWN))) foundEnd = solveMaze(m, x, y + 1, DOWN); if (!foundEnd && (from != LEFT && (cell & RIGHT))) foundEnd = solveMaze(m, x + 1, y, RIGHT); // If this is on the path mark it if (foundEnd) solutionCell(m, x, y); return foundEnd; }
// driver program to test above function int main() { int maze[N][N] = { {1, 0, 0, 0}, {1, 1, 1, 1}, {0, 1, 0, 1}, {1, 1, 1, 1} }; solveMaze(maze); int k=countSol(maze, 0, 0); printf ("No. of slutions is %d cnt1= %d cnt2= %d",k,cnt1,cnt2); getchar(); return 0; }
/*creates a maze and attempts to find a path to the exit. */ void main() { int pRow=0, pCol=0; printf("Please enter the number of rows "); scanf("%d", &pRow); printf("Please enter the number of columns "); scanf("%d", &pCol); /*code for reading the file */ char array[200][200]={0}; FILE *fptr; char c; char file_name[20]; int i,j; printf("Type in the name of the file containing the Field\n"); scanf("%s",file_name); fptr=fopen(file_name,"r"); for (i=0; i<pRow; i++) for (j=0; j<pCol; j++){ c=fgetc(fptr); while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr); array[i][j]=c; } fclose(fptr); for (i=0; i<pRow; i++) for (j=0; j<pCol; j++) { if (j == 0) printf("\n"); printf("%c ",array[i][j]); } printf("\n"); for(i=0; i<pRow;i++){ for(j=0;j<pCol;j++){ puzzle[i][j]=array[i][j]; } } solveMaze(puzzle,pRow,pCol); traverse(entRow, entCol); for(i=0; i<pRow; i++){ for(j=0; j<pCol; j++){ if(j==0) printf("\n"); printf("%c ", puzzle[i][j]); } } printf("\n"); }
int main(int argc, const char* argv[]) { if( argc != 2 ) //checks for the input file name { printf( "error; no input file name\n" ); return 1; } FILE *filePointer; filePointer = fopen( argv[1], "r" ); char **arrayPointer; //char maze[MAX_MAZE_SIZE][MAX_MAZE_SIZE] = { 0 }; int numberOfTestCases = 0; fscanf( filePointer, "%d\n", &numberOfTestCases ); for( int testCaseNumber = 0; testCaseNumber < numberOfTestCases; testCaseNumber++ ) { int size = 0; int * xy; int x; int y; fscanf( filePointer, "%d\n", &size ); printf( "ENTER\n" ); allocateArray(size, &arrayPointer); initializeArray(size, arrayPointer, filePointer); xy = findEntrance(size, arrayPointer); x = xy[0]; y = xy[1]; solveMaze(size, arrayPointer, x ,y); deallocateArray(size, arrayPointer); printf( "EXIT\n***\n" ); } fclose( filePointer ); return 0; }
DWORD WINAPI SocketHandler(void* lp){ int *csock = (int*)lp; char recvbuf[1024]; char MNrecvbuff[3]; int recvbuf_len = 1024; int recv_byte_cnt; int m, n; //memset(recvbuf, 0, recvbuf_len); if ((recv_byte_cnt = recv(*csock, MNrecvbuff, sizeof(MNrecvbuff), 0)) == SOCKET_ERROR){ fprintf(stderr, "Error receiving data %d\n", WSAGetLastError()); free (csock); return 0; } m = GetInt(MNrecvbuff[0]); n = GetInt(MNrecvbuff[1]); int packet_no=0; int **maze = (int**)calloc(m, sizeof(int)*m); for (int j = 0; j < n; j++){ maze[j] = (int*)calloc(n, sizeof(int)); } while (1){ if ((recv_byte_cnt = recv(*csock, recvbuf, sizeof(recvbuf), 0)) == SOCKET_ERROR){ fprintf(stderr, "Error receiving data %d\n", WSAGetLastError()); free(csock); return 0; } packet_no += 1; Populate(recvbuf, recv_byte_cnt, maze, m, n); if ((m*n) * 2 < 1024*packet_no) break; } //printf("Received bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf); solveMaze(maze, m, n); printpath(m,n,csock); //process_input(MNrecvbuff, recv_byte_cnt, csock); return 0; }
main() { printf("How many rows does the maze contain?\n"); scanf("%d", &rows); if (rows > 200 || rows < 5){ printf("Sorry, that's not a valid entry. Please rerun the program and try a number between 5 and 200."); return(1); }// end if printf("How many columns does the maze contain?\n"); scanf ("%d", &columns); if (columns > 200 || rows < 5){ printf("Sorry, that's not a valid entry. Please rerun the program and try a number between 5 and 200.\n"); return (1); }// end if printf("The maze you specified contains %d rows and %d columns.\n", rows,columns); printf("What file can the specified maze be found in?\n"); scanf("%s", filename); printf("Looking for the file:'%s' on your computer. Please wait.\n", filename); filepointer = fopen(filename, "r"); if(filepointer == NULL){ printf("There was an error finding the file. Please try running the program again.\n"); return 1; }// end if printf("There were no errors finding the program.\n"); printf("The file contained the following maze:\n"); //read in the file and remove all of the whitespace while((file = fgetc( filepointer))!= EOF){ numCharacter = numCharacter + 1; if (file=='1' || file=='0'){ maze[i][j]= file; j++; }// end if if (file =='\n'){ j=0; i++; }// end if }// end while //print out maze for (i = 0; i < rows; i++){ for (j =0; j < columns; j++){ printf("%c", maze[i][j]); }// end for printf("\n"); }// end for if (numCharacter == (rows * columns * 2 + rows)){ printf("The maze is correct.\n"); }// end if else { printf("The maze does not match the diminsions you've specified.\n"); printf("The program will now terminate.\n"); return 1; }// end else fclose(filepointer); printf("Now to solve the maze... Computing solution.\n"); /* Find entrance and exit */ int iEntrance, iExit, jEntrance, jExit; // checks the first column for an entrance while (entrance == 0 && mazeExit == 0){ for (i = 0; i < rows; i++){ if (maze[i][0]=='0'){ if (entrance ==0){ entrance = 1; printf("The entrance was found in the first column.\n"); entrancePoint = maze[i][0]; iEntrance = i; jEntrance = 0; } // end if else { printf("The exit was found in the first column.\n"); mazeExit = 1; exitPoint = maze[i][0]; iExit = i; jExit = 0; mazeExit = 1; } // end else } // end if }// end for // checks the last row for an entrance for ( j = 0; j < columns; j ++){ if( maze[rows-1][j] == '0'){ if (entrance == 0){ entrance = 1; entrancePoint = maze[rows-1][j]; iEntrance = rows-1; jEntrance = j; printf("The entrance was found in the last row.\n"); }//end if else { printf("The exit was found in the last row.\n"); exitPoint = maze[rows-1][j]; iExit = rows-1; jExit = j; mazeExit = 1; }// end else }// end if }// end for // checks the last column for an entrance for (i = rows; i > 0 ; i--){ if (maze[i][columns-1]=='0'){ if (entrance == 0){ entrance = 1; entrancePoint = maze[i][columns-1]; iEntrance = i; jEntrance = columns-1; printf("The entrance was found in the last column.\n"); }// end if else { printf("The exit was found in the last column.\n"); exitPoint = maze[i][columns-1]; iExit = i; jExit = columns-1; mazeExit = 1; }// end else }// end if }// end for // checks the first row for an entrance for (j = columns; j > 0; j--){ if (maze[0][j] == '0'){; if (entrance == 0){ printf("The entrance was found in the first row.\n"); entrance = 1; iEntrance = 0; jEntrance = j; entrancePoint = maze[0][j]; }// end if else { printf("The exit was found in the first row.\n"); mazeExit = 1; iExit = 0; jExit = j; exitPoint = maze[0][j]; }// end else }// end if }// end for }// end while solveMaze(iEntrance,jEntrance, iExit, jExit); return 0; /* indicate program ended successfully */ }// end Main
PNG * SquareMaze::drawMazeWithSolution () { PNG * outPNG = drawMaze(); vector< int > path = solveMaze(); int x = 5; int y = 5; for(size_t i = 0; i < path.size(); i++) { if(path[i] == 0) { for(int j = 0; j < 11; j++) { (*outPNG)(x + j, y)->red = 255; (*outPNG)(x + j, y)->green = 0; (*outPNG)(x + j, y)->blue = 0; } x += 10; } else if(path[i] == 1) { for(int j = 0; j < 11; j++) { (*outPNG)(x, y + j)->red = 255; (*outPNG)(x, y + j)->green = 0; (*outPNG)(x, y + j)->blue = 0; } y += 10; } else if(path[i] == 2) { for(int j = 0; j < 11; j++) { (*outPNG)(x - j, y)->red = 255; (*outPNG)(x - j, y)->green = 0; (*outPNG)(x - j, y)->blue = 0; } x -= 10; } else { for(int j = 0; j < 11; j++) { (*outPNG)(x, y - j)->red = 255; (*outPNG)(x, y - j)->green = 0; (*outPNG)(x, y - j)->blue = 0; } y -= 10; } } /*draw the exit*/ int mazex = x /10; int mazey = height - 1; for(int k = 1; k < 10; k++) { (*outPNG)(mazex * 10 + k, (mazey + 1) * 10)->red = 255; (*outPNG)(mazex * 10 + k, (mazey + 1) * 10)->green = 255; (*outPNG)(mazex * 10 + k, (mazey + 1) * 10)->blue = 255; } return outPNG; }