Click here to Skip to main content
15,886,781 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Web Filter Pin
N a v a n e e t h16-Sep-09 18:38
N a v a n e e t h16-Sep-09 18:38 
QuestionClass or Library to support MFC Document Windows borders Pin
WylieCoyoteUsa16-Sep-09 17:21
WylieCoyoteUsa16-Sep-09 17:21 
QuestionRe: Class or Library to support MFC Document Windows borders Pin
CPallini16-Sep-09 21:06
mveCPallini16-Sep-09 21:06 
AnswerRe: Class or Library to support MFC Document Windows borders Pin
WylieCoyoteUsa17-Sep-09 2:58
WylieCoyoteUsa17-Sep-09 2:58 
QuestionPassing Objects UI threads VS worker threads Pin
ForNow16-Sep-09 14:57
ForNow16-Sep-09 14:57 
AnswerRe: Passing Objects UI threads VS worker threads Pin
Chris Losinger16-Sep-09 16:03
professionalChris Losinger16-Sep-09 16:03 
GeneralRe: Passing Objects UI threads VS worker threads Pin
ForNow16-Sep-09 16:31
ForNow16-Sep-09 16:31 
QuestionRecursive maze, so close, probably something stupid Pin
forensicgeek16-Sep-09 14:17
forensicgeek16-Sep-09 14:17 
I'm very new to C and I've been working so hard on this and I have no idea what is wrong with it, I think it is in the Move method, but I wasn't sure so I included the whole program, if anyone can figure out why whats wrong please let me know! Thanks!
My assignment is to get through a maze that can be up to 50 by 50, the sample maze I've been using looks like this: The 1s are walls and the 0s are where you can step.

1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 0 0 0 0 1
1 1 0 0 0 1 0 1 0 1
1 0 0 1 1 1 0 1 0 1
1 0 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 0 1 1
1 1 0 1 1 0 1 0 0 0 S
1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1
1 0 0 1 0 0 1 1 1 1
1 0 1 0 0 1 0 0 1 1
1 1 1 1 0 1 1 1 1 1
E
It prints X's on the places it has visited and it will go left, but it won't go up, down, or right. it starts at S and ends at E, but it will only go over the first 3 0s, replace them with X's, remove the last two X's and terminate. Please help!

#include <stdio.h>
#define M 50
#define N 50
int rows, columns, exitRow, exitCol, enterRow, enterCol;
void printArray(char a[M][N], int rows, int columns);
int moveLeft(int currCol, int currRow, char a[M][N]);
int moveRight(int currCol, int currRow, char a[M][N]);
int moveUp(int currRow, int currCol, char a[M][N]);
int moveDown(int currRow, int currCol, char a[M][N]);
int Move(int currCol, int currRow, char array[M][N], int first);
void removeX(int currRow, int currCol, char array[M][N]);
int fail=0;

main()
{
char comma;
char foundEnter='n';
char foundExit='n';
int first=1;

//takes in the size of the maze
printf("Please type in the size of the field as rows, columns\n");
scanf("%d", &rows);
scanf("%c", &comma);
scanf("%d", &columns);

/*Asks user for input file and adds the field from the file to an array*/
char array[M][N]={9};
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<rows; i++)
for (j=0; j<columns; j++){
c=fgetc(fptr);
while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
array[i][j]=c;
}
fclose(fptr);

//Copies maze into the failed array
char failArray[M][N]={9};
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
failArray[i][j]=array[i][j];
}

//finds the entrance and exit of the maze
for(j=0; j<columns; j++){
if(array[0][j] != '1' && array[0][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=0;
foundEnter='y';
}
else{ exitCol= j;
exitRow=0;
foundExit='y';
}
}
}

for(i=0; i<rows; i++){
if(array[i][columns-1] != '1' && array[i][columns-1]=='0'){
if(foundEnter=='n'){
enterCol= columns-1;
enterRow=i;
foundEnter='y';
}
else { exitCol= columns-1;
exitRow=i;
foundExit='y';
}
}
}

for(j=columns-1; j>=0; j--){
if(array[rows-1][j] != '1' && array[rows-1][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=rows-1;
foundEnter='y';
}
else { exitCol= j;
exitRow=rows-1;
foundExit='y';
}
}
}
for(i=rows-1; i>=0; i--){
if(array[i][0] !='1' && array[i][0]=='0'){
if(foundEnter=='n'){
enterCol= 0;
enterRow=i;
foundEnter='y';
}
else { exitCol= 0;
exitRow=i;
foundExit='y';
}
}
}

//prints the array
printArray(array, rows, columns);

int currRow=enterRow; //sets the current Row to the enter Row
int currCol=enterCol; //sets the current Column to the enter Column
int end;

end=Move(currCol, currRow, array, first);

//if theres no path found it prints the fail array
if(end){
printArray(failArray, rows, columns);
printf("Path not found.\n");
}
else{
//if the path is found it prints the path to the exit
printArray(array, rows, columns);
printf("Path found.\n");
}
return 0;
}

//prints the array
void printArray(char a[M][N], int rows, int columns){
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<columns; j++) {
if (j == 0) printf("\n");
printf("%c ",a[i][j]);
}
printf("\n");
}

//checks if move Left is possible
int moveLeft(int currCol, int currRow, char a[M][N]){
if(currCol-1>=-1)
if(a[currRow][currCol-1]=='0')
return 1;
return 0;
}

//checks if move Right is possible
int moveRight(int currCol, int currRow, char a[M][N]){
if(currCol+1<currCol)
if(a[currRow][currCol+1]=='0')
return 1;
return 0;
}

//checks if move Up is possible
int moveUp(int currRow, int currCol, char a[M][N]){
if(currRow-1>-1)
if(a[currRow-1][currCol]=='0')
return 1;
return 0;
}

//checks if move Down is possible
int moveDown(int currRow, int currCol, char a[M][N]){
if(currRow+1<currRow)
if(a[currRow+1][currCol]=='0')
return 1;
return 0;
}

//moves through the maze
int Move(int currCol, int currRow, char array[M][N], int first){
int fail;
int c;
array[currRow][currCol]='X';
failArray[currRow][currCol]='X';

//checks the exit condition as long as its not the first time through
if(!first){
if(currRow==exitRow && currCol==exitCol)
return 0;
if(currRow==enterRow && currCol==enterCol)
return 1;
}

//this is to track the array (temporary until code fixed)
first=0;
printArray(array, rows, columns);
printf("At Point row %d and col %d \n",currRow, currCol);
printf("fail=%d \n", fail);
getchar();

//goes left until it can't
if(moveLeft(currCol, currRow, array)){
c=--currCol;
printf("Moved left \n");
printf("Moving to col %d \n",c);
fail = !Move(c, currRow, array, first);
}
//if left fails goes up until it can't
if(moveUp(currCol, currRow, array) && fail){
c=--currRow;
printf("Moved up \n");
fail = !Move(currCol, c, array, first);
}
//if up fails goes down until it can't
if(moveDown(currCol, currRow, array) && fail){
c=++currRow;
printf("Moved down \n");
fail = !Move(currCol, c, array, first);
}
//if down fails goes right until it can't
if(moveRight(currCol, currRow, array) && fail){
c=++currCol;
printf("Moved right \n");
fail = !Move(c, currRow, array, first);
}
//removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;

//absolute failure
printf("Failed all \n");
return 0;
}

//removes the X and replaces it with a 0.
void removeX(int currRow, int currCol, char array[M][N]){
array[currRow][currCol]='0';
}
AnswerRe: Recursive maze, so close, probably something stupid Pin
«_Superman_»16-Sep-09 20:17
professional«_Superman_»16-Sep-09 20:17 
AnswerRe: Recursive maze, so close, probably something stupid Pin
David Crow17-Sep-09 2:43
David Crow17-Sep-09 2:43 
AnswerRe: Recursive maze, so close, probably something stupid Pin
Alan Balkany18-Sep-09 3:58
Alan Balkany18-Sep-09 3:58 
QuestionTime Pin
Bram van Kampen16-Sep-09 13:28
Bram van Kampen16-Sep-09 13:28 
AnswerRe: Time Pin
David Crow17-Sep-09 2:47
David Crow17-Sep-09 2:47 
GeneralRe: Time Pin
Bram van Kampen21-Sep-09 10:40
Bram van Kampen21-Sep-09 10:40 
QuestionAudio APIs for Windows XP [modified] [Solved] Pin
mk_416-Sep-09 12:43
mk_416-Sep-09 12:43 
AnswerRe: Audio APIs for Windows XP Pin
Jerry Evans16-Sep-09 13:44
Jerry Evans16-Sep-09 13:44 
GeneralRe: Audio APIs for Windows XP Pin
mk_416-Sep-09 21:58
mk_416-Sep-09 21:58 
QuestionHow can i check CString format for a especific mask Pin
SNArruda16-Sep-09 12:20
SNArruda16-Sep-09 12:20 
AnswerRe: How can i check CString format for a especific mask Pin
Chris Losinger16-Sep-09 15:22
professionalChris Losinger16-Sep-09 15:22 
AnswerRe: How can i check CString format for a especific mask Pin
«_Superman_»16-Sep-09 20:07
professional«_Superman_»16-Sep-09 20:07 
QuestionRe: How can i check CString format for a especific mask Pin
David Crow17-Sep-09 2:50
David Crow17-Sep-09 2:50 
QuestionGood book for C/C++/VC++2008 Pin
DarkSorrow3816-Sep-09 8:38
DarkSorrow3816-Sep-09 8:38 
AnswerRe: Good book for C/C++/VC++2008 Pin
N a v a n e e t h16-Sep-09 9:12
N a v a n e e t h16-Sep-09 9:12 
AnswerRe: Good book for C/C++/VC++2008 Pin
bob1697216-Sep-09 11:07
bob1697216-Sep-09 11:07 
QuestionCompliling trouble Pin
belleissexy33316-Sep-09 8:27
belleissexy33316-Sep-09 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.