Getting maze algorithm to work on open maze - c

first time using the site. I am trying to figure out how to solve a 'maze' using the shortest path. The code works for traditional mazes but the path I am trying to work on is essentially more open. When run the current path goes right, then down, then left and goes up then turns right before finally reaching B. My solution needs to go right then up then left to B. Any help would be appreciated!
9,11
xxxxxxxxxxx
x......B..x
x...xxxx..x
x...xxxx..x
x....A....x
x..xx.xx..x
x.........x
x.........x
xxxxxxxxxxx
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
char** maze;
int** checked;
int rows;
int cols;
int start_row;
int start_col;
enum area
{
space,
wall,
end,
trail,
};
void alloc_maze() {
maze = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
maze[i] = malloc(cols * sizeof(char*));
}
}
void alloc_checked() {
checked = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
checked[i] = malloc(cols * sizeof(char*));
}
}
void get_maze(const char* file_name)
{
char c;
char rows_t[3] = { '\0' };
char cols_t[3] = { '\0' };
int rows_i = 0;
int cols_i = 0;
int swap = 0;
FILE* file = fopen(file_name, "r");
if (file) {
while ((c = getc(file)) != EOF) {
if (c == '\n') {
break;
} else if (c ==',')
{
swap = 1;
}
else if (!swap) {
rows_t[rows_i] = c;
rows_i++;
}
else {
cols_t[cols_i] = c;
cols_i++;
}
}
}
rows = atoi(rows_t);
cols = atoi(cols_t);
alloc_maze();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c = getc(file);
if (c == '\n') {
c = getc(file);
}
maze[i][j] = c;
if (c == 'A') {
start_row = i;
start_col = j;
}
}
}
fclose(file);
}
void get_checked() {
alloc_checked();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] == 'x') {
checked[i][j] = wall;
}
else if (maze[i][j] == 'B') {
checked[i][j] = end;
}
else {
checked[i][j] = space;
}
}
}
}
int search(int row, int col) {
int* current = &checked[row][col];
if (*current == end) {
printf("\n congrats you found the shortest path is");
return 1;
}
if (*current == space) {
*current = wall;
if (search(row, col + 1)) {
*current = trail;
printf("E");
return 1;
}
if (search(row + 1, col)) {
*current = trail;
printf("N");
return 1;
}
if (search(row - 1, col)) {
*current = trail;
printf("S");
return 1;
}
if (search(row, col - 1)) {
*current = trail;
printf("W");
return 1;
}
}
return 0;
}
void add_trail() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] != 'A'){
if (checked[i][j] == trail) {
maze[i][j] = 'O';
}
}
}
}
}
void print_checked() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", checked[i][j]);
}
printf("\n");
}
printf("\n");
}
void print_maze() {
printf("\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
get_maze("quickest_route_4.txt");
get_checked();
print_maze();
search(start_row, start_col);
add_trail();
print_maze();
return 0;
}

You have implemented a DFS - Depth First Search maze searching algorithm. This means that your searching algorithm is going to explores as far as possible along a direction before trying another one.
In your code this means that it will always try all of the options going right and then all of the options that are going down and this causes your code to not find the shortest path but just finding a path.
If you do want to find the shortest path you should implement a BFS - Breadth First Search algorithm, it will find you the shortest path since it is progressing the search in all of the active nodes at the same time. It will be a bit harder to implement though since it uses a queue data structure.
Good luck
Also notice that the path you are printing is coming out in reverse order.

Related

Why when I try to run this code online it always generates runtime error/run error. but when I run it in command prompt it works [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
This code is a program that will move the index position of several names according to the size of the toga that each participant has, and according to the name that the user wants to change.
#include <stdio.h>
#include <string.h>
typedef struct
{
char nama[100];
char ukuran[100];
} toga;
int main()
{
int n, m;
scanf("%d%d", &n, &m);
toga matriks[n][m];
int jumlah[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%s%s", &matriks[i][j].nama, &matriks[i][j].ukuran);
if(strcmp(matriks[i][j].ukuran, "XS") == 0)
{
jumlah[i][j] = 32;
}
else if(strcmp(matriks[i][j].ukuran, "S") == 0)
{
jumlah[i][j] = 36;
}
else if(strcmp(matriks[i][j].ukuran, "M") == 0)
{
jumlah[i][j] = 37;
}
else if(strcmp(matriks[i][j].ukuran, "L") == 0)
{
jumlah[i][j] = 38;
}
else if(strcmp(matriks[i][j].ukuran, "XL") == 0)
{
jumlah[i][j] = 40;
}
else if(strcmp(matriks[i][j].ukuran, "XXL") == 0)
{
jumlah[i][j] = 42;
}
else if(strcmp(matriks[i][j].ukuran, "XXXL") == 0)
{
jumlah[i][j] = 45;
}
}
}
maybe this part is the problem?
toga ditukar1[50][50];
toga ditukar2[50][50];
int cek2 = 0;
for (int i = 0; i < 50; i++)
{
for(int j = 0; j < 1; j++)
{
scanf("%s", &ditukar1[i][0].nama);
if(strcmp(ditukar1[i][0].nama, "Selesai") == 0)
{
j = 1;
i = 50;
}
else
{
scanf("%s ", &ditukar1[i][1].nama);
// cek1++;
}
}
cek2++;
}
cek2 -= 1;
// printf("ini cek1 : %d\n", cek1);
// printf("ini cek2 : %d\n", cek2);
toga matriks_baru[m][n];
for (int i = 0, a = 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len[m];
max_len[0] = 0;
int terpanjang[m];
int a = 0;
for (int i = 0; i < m; i++)
{
max_len[a] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len[a])
{
max_len[a] = strlen(matriks_baru[i][j].nama);
}
}
a++;
}
printf("Posisi awal :\n");
for(int i = 0; i < n; i++)
{
a = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len[a]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
a++;
}
printf("\n");
}
printf("\n");
int baris1, baris2;
int kolom1, kolom2;
int k = 0;
int l = 0;
maybe this part is the problem?
here I will check whether the name entered by the user whose index you want to exchange is in the matrix
for(int l = 0; l < cek2; l++)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
for(int a = 0; a < n; a++)
{
for(int b = 0; b < m; b++)
{
if(strcmp(ditukar1[l][0].nama, matriks[i][j].nama) == 0)
{
baris1 = i;
kolom1 = j;
if(strcmp(ditukar1[l][1].nama, matriks[a][b].nama) == 0)
{
// printf("%d\n", l);
baris2 = a;
kolom2 = b;
if(baris1 < baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris1][kolom1] > jumlah[baris2][kolom2])
{
temp2 = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = temp2;
strcpy(temp1, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, temp1);
}
}
else if(baris1 > baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris2][kolom2] > jumlah[baris1][kolom1])
{
temp2 = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = temp2;
strcpy(temp1, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, temp1);
}
}
}
}
}
}
}
}
}
for (int i = 0, a= 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len2[m];
max_len2[0] = 0;
int s = 0;
for (int i = 0; i < m; i++)
{
max_len2[s] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len2[s])
{
max_len2[s] = strlen(matriks_baru[i][j].nama);
}
}
s++;
}
printf("Posisi akhir :\n");
for(int i = 0; i < n; i++)
{
s = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len2[s]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
s++;
}printf("\n");
}
return 0;
}

Backtracking crossword algorithm in c

So i made a backtracking algorithm in c, where i get a txt file filled with words in each line and another txt file just has coordinates for the black squares of a square crossword. I know my implementation is extremelly simple, but i just want to have the barebones algorithm, to make it much faster later on with constraint satisfaction, forward checking etc.
However i have no idea how to get the basic code to work without segmentation faults and wrong solutions! Ant advice would be very helpful!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORDS 150000
#define MAX_DIMENSION 200
#define MAX_BLACK_SQUARES 250
int is_valid_word(char *word, int dimension, char crossword[MAX_DIMENSION][MAX_DIMENSION])
{
int i, j, k;
int word_length = strlen(word);
// check if word is too long
if (word_length > dimension)
{
return 0;
}
// check if word overlaps existing letters
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '_')
{
for (k = 0; k < word_length; k++)
{
if ((i + k < dimension && crossword[i + k][j] != '_' && crossword[i + k][j] != word[k]) ||
(j + k < dimension && crossword[i][j + k] != '_' && crossword[i][j + k] != word[k]))
{
return 0;
}
}
}
}
}
return 1;
}
int solve_crossword(int dimension, char crossword[MAX_DIMENSION][MAX_DIMENSION], char words[MAX_WORDS][30], int num_words, int word_index)
{
int i, j, k;
if (word_index >= num_words)
{
// all words have been placed, so return true
return 1;
}
// try placing the current word horizontally
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension - strlen(words[word_index]); j++)
{
if (crossword[i][j] != '#')
{
// try placing the word here
int is_valid = 1;
for (k = 0; k < strlen(words[word_index]); k++)
{
if (crossword[i][j + k] != '_' && crossword[i][j + k] != words[word_index][k])
{
is_valid = 0;
break;
}
}
if (is_valid)
{
// place the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i][j + k] = words[word_index][k];
}
// recursive call to place the next word
if (solve_crossword(dimension, crossword, words, num_words, word_index + 1))
{
return 1;
}
// backtrack by removing the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i][j + k] = '_';
}
}
}
}
}
// try placing the current word vertically
for (i = 0; i < dimension - strlen(words[word_index]); i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '#')
{
// try placing the word here
int is_valid = 1;
for (k = 0; k < strlen(words[word_index]); k++)
{
if (crossword[i + k][j] != '_' && crossword[i + k][j] != words[word_index][k])
{
is_valid = 0;
break;
}
}
if (is_valid)
{
// place the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i + k][j] = words[word_index][k];
}
// recursive call to place the next word
if (solve_crossword(dimension, crossword, words, num_words,word_index + 1))
{
return 1;
}
// backtrack by removing the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i + k][j] = '_';
}
}
}
}
}
// if we get here, we were unable to place the word in either direction
return 0;
}
int main()
{
// open dictionary file
FILE *dict_file = fopen("dicts/EvenMoreWords.txt", "r");
if (dict_file == NULL)
{
printf("Unable to open dict.txt\n");
return 1;
}
// read dictionary into array
char words[MAX_WORDS][30];
int num_words = 0;
char buffer[30];
while (fgets(buffer, 30, dict_file) != NULL)
{
int word_length = strlen(buffer);
if (buffer[word_length - 1] == '\n')
{
buffer[word_length - 1] = '\0';
}
strcpy(words[num_words], buffer);
num_words++;
}
fclose(dict_file);
// open crossword file
FILE *crossword_file = fopen("crosswords/c1.txt", "r");
if (crossword_file == NULL)
{
printf("Unable to open crossword.txt\n");
return 1;
}
// read dimensions and black squares
int dimension, i, j, width, height;
fscanf(crossword_file, "%d", &dimension);
// initialize crossword
char crossword[MAX_DIMENSION][MAX_DIMENSION];
while (fscanf(crossword_file, "%d %d", &width, &height) != EOF)
{
crossword[width - 1][height - 1] = '#';
}
printf("\n\n\n");
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '#')
{
crossword[i][j] = '_';
printf("%c ", crossword[i][j]);
}
else
{
printf("%c ", crossword[i][j]);
}
}
printf("\n");
}
printf("\n\n\n");
// solve crossword
if (solve_crossword(dimension, crossword, words, num_words, 0))
{
// print solution
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
printf("%c ", crossword[i][j]);
}
printf("\n");
}
}
else
{
printf("Unable to find a solution\n");
}
return 0;
}

Is there a way to combine two different strings alphabetically using stack in C?

Here is the program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *stack;
int stackindex = 0;
int push(char in){
if (stack==NULL) {
stack = (char *) malloc (sizeof(char));
}
else {
stack = (char *) realloc(stack, sizeof(char)* ++stackindex);
}
stack[stackindex] = in;
return 1;
}
char pop(){
return stack[stackindex--];
}
void show() {
for (int i=stackindex; i>=0;i--) {
printf("%c", pop());
}
}
int main(int argc, char**argv) {
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
int j;
for (int j = 0; j < k ; j++) {
char in = argv [i] [j];
push( in );
}
for (j = i; j < k; j++){
char temp;
if (stack[i] > stack[j]) {
temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
if (i, argc-1) push(' ');
}
show();
return 0;
}
}
I've been trying to make an output from the string "im loving" alphabetically in descending order (ASCII based) using command line arguments like this: vonmliig, but the output was "m i" instead.
Looks like you are trying to sort as you build the stack. I would build the stack first, then sort.
int main(int argc, char**argv)
{
if (argc >= 3) {
// Build stack
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
for (int j = 0; j < k ; j++) {
push(argv[i][j]);
}
}
// Sort
for (int i = 0; i < stackindex; i++) {
for (int j = i; j < stackindex; j++){
if (stack[i] > stack[j]) {
char temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
}
}
show();
}
return 0;
}
Also, there is an error in the handling of stackindex in push().
int push(char in)
{
char *temp = realloc(stack, stackindex + 1);
if (!temp) {
// Handle error
return -1;
}
stack = temp;
stack[stackindex++] = in;
return 1;
}
char pop()
{
if (stackindex) return stack[--stackindex];
else return 0;
}
void show()
{
do {
printf("%c", pop());
} while (stackindex);
}

Possible mode error

I've made this program that computes the mean, the median and the mode from an array. Although I've tested with some examples, I found out there might be a case that I have forgotten as for many of the inputs I've tested it works but the testing program that my teacher is using gave me an error for a certain test, but I was not presented with its input. Maybe someone can have a look and see if I am making a mistake at the mode point of the code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
int main(int argc, char *argv[]) {
int n, i;
scanf("%d", &n);
int *array = safeMalloc(n * sizeof(int));
for (i = 0; i < n; i++) {
int value;
scanf("%d", &value);
array[i] = value;
}
//mean
double mean;
double sum = 0;
for (i = 0; i < n; i++) {
sum = sum + (double)array[i];
}
mean = sum / n;
printf("mean: %.2f\n", mean);
//median
float temp;
int j;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
printf("median: %d\n", array[n / 2]);
//mode
int val = array[0], noOfRepetitions = 1, valMax = array[0], maxRepetitions = 1, possibleMax = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
}
if (array[i] != val) {
val = array[i];
noOfRepetitions = 1;
}
if (noOfRepetitions == possibleMax) {
maxRepetitions = 1;
continue;
}
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
possibleMax = maxRepetitions;
}
}
if (maxRepetitions > 1) {
printf("mode: %d\n", valMax);
} else {
printf("mode: NONE\n");
}
return 0;
}
My idea for mode was because the numbers are sorted when just transverse it. If the next element is the same as the previous one, increase the noOfRepetitions. If the noOfRepetition is bigger than the maxRepetitions until now, replace with that. Also store the last maximum val needed if we have for example more than 2 numbers with the same number of repetitions.
EDIT: The mode of an array should return the number with the maximum number of occurrences in the array.If we have 2 or more number with the same number of maximum occurrences , there isn't a mode on that array.
I've discovered my mistake. I didn't think of the case when I have numbers with same maximum frequency and after that came one with lower frequency but still bigger than others. For example : 1 1 1 2 2 2 3 3 4 5 6.With my code , the result would have been 3 . I just needed to change the comparison of noOfRepetitions with oldMaxRepetition.
There seems to be no purpose for the variable possibleMax. You should just remove these lines:
if(noOfRepetitions==possibleMax){
maxRepetitions=1;
continue;
}
They cause maxRepetitions to be reset erroneously.
You could detect if the distribution is multimodal and print all mode values:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
int main(int argc, char *argv[]) {
int n, i;
if (scanf("%d", &n) != 1 || n <= 0)
return 1;
int *array = safeMalloc(n * sizeof(int));
for (i = 0; i < n; i++) {
if (scanf("%d", &array[i]) != 1)
return 1;
}
//mean
double sum = 0;
for (i = 0; i < n; i++) {
sum = sum + (double)array[i];
}
printf("mean: %.2f\n", sum / n);
//median
int j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
printf("median: %d\n", array[n / 2]);
//mode
int val = array[0], noOfRepetitions = 1, valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
} else {
val = array[i];
noOfRepetitions = 1;
}
}
if (maxRepetitions == 1) {
printf("mode: NONE\n");
} else {
printf("mode: %d", valMax);
val = array[0];
noOfRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
} else {
if (noOfRepetition == maxRepetitions && val != valMax) {
printf(", %d", val);
}
val = array[i];
noOfRepetitions = 1;
}
}
if (noOfRepetition == maxRepetitions && val != valMax) {
printf(", %d", val);
}
printf("\n");
}
return 0;
}
Your code to search a mode seems too complicated. Compare this:
//mode
int val = array[0], noOfRepetitions = 1,
valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
if (++noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
}
else
{
val = array[i];
noOfRepetitions = 1;
}
}
It's probably the simplest code to do what you need, but it overwrites maxVal and maxRepetitions much too often.
The following version overwrites the two 'max' variables only once per each new maximum found – at the cost of duplicating some part of code:
//mode
int val = array[0], noOfRepetitions = 1,
valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
++noOfRepetitions;
}
else
{
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
val = array[i];
noOfRepetitions = 1;
}
}
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}

C sudoku backtracking solver precheck function

This is my first post on StackOverflow, so I apologize if I'm doing something wrong. I'm relatively new to C, so I'm sure my code is fairly ugly and hacky in places, however the majority of the code does what I expect it to. I'm having trouble with a precheck method that I'm using to check a sudoku board before I begin feeding it through my solver-logic. I'm redirecting input from a text file with strings that look like
4.....8.5.34.........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......
4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.5......
4.....8.5.3..........7......2.....6.....8.4......1...x...6.3.7.5..2.....1.4......
417369825632158947958724316825437169791586432346912758289643571573291684164875293
417369825632158947958724316825437169791586432346912758289643.71573291684164875293
Each string is (ideally) 81 characters with just digits 1-9 and '.'s. I parse a string into a temp char array, and then use the method fillBoard to transfer the chars in the temp array into a 2d int array. Once this is complete, I call my precheck method. If the filled board doesn't pass the row, column, and box checks, the precheck method returns a one, indicating that the puzzle is not solvable (meaning an error message should be printed and that the program should move on to the next string). For some reason, my precheck method is returning one even for strings that should be solvable. I'm not sure why this is. Any help would be appreciated. Thanks.
#include <stdio.h>
#include <string.h>
int alphaError = 0;
struct Point findEmpty(int board[9][9]);
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num);
int positionSafe(int board[9][9], int row, int col, int num);
int usedInCol(int board[9][9], int col, int num);
int usedInRow(int board[9][9], int row, int num);
int solvePuzzle(int board[9][9]);
int precheck(int board[9][9]);
int main()
{
char c;
int charCount = 0;
int i = 0;
char tempStr[100000];
int board[9][9];
while((fscanf(stdin, "%c", &c)) != EOF)
{
printf("%c", c);
if(c != '\n')
{
if(isalpha(c))
{
alphaError = 1;
}
tempStr[i] = c;
i++;
charCount++;
}
else
{
if(charCount != 81 || alphaError == 1)
{
printf("Error\n\n");
i = 0;
charCount = 0;
alphaError = 0;
}
else
{
fillBoard(board, tempStr);
printBoard(board);
if(precheck(board) == 1)
{
printf("Error\n\n");
}
else
{
if(solvePuzzle(board) == 1)
{
printBoard(board);
}
else
{
printf("No solution\n\n");
}
}
i = 0;
charCount = 0;
}
}
}
return 0;
}
struct Point
{
int x;
int y;
} point;
struct Point findEmpty(int board[9][9])
{
struct Point point1;
point1.x = -1;
point1.y = -1;
int row, col;
for(row = 0; row < 9; row++)
{
for(col = 0; col < 9; col++)
{
if(board[row][col] == 0)
{
point1.x = col;
point1.y = row;
}
}
}
return point1;
}
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num)
{
int row, col;
for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
if(board[row + boxStartRow][col + boxStartCol] == num)
{
return 1;
}
}
}
return 0;
}
int positionSafe(int board[9][9], int row, int col, int num)
{
if((usedInRow(board, row, num)) == 0 && (usedInCol(board, col, num)) == 0 && (usedInBox(board, (row-row%3), (col-col%3), num)) == 0)
{
return 1;
}
else
{
return 0;
}
}
int usedInCol(int board[9][9], int col, int num)
{
int row;
for(row = 0; row < 9; row++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int usedInRow(int board[9][9], int row, int num)
{
int col;
for(col = 0; col < 9; col++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int solvePuzzle(int board[9][9])
{
int num;
struct Point point2;
point2 = findEmpty(board);
if(point2.x == -1)
{
return 1;
}
for(num = 1; num <= 9; num++)
{
if(positionSafe(board, point2.y, point2.x, num) == 1)
{
board[point2.y][point2.x] = num;
if(solvePuzzle(board) == 1)
{
return 1;
}
board[point2.y][point2.x] = 0;
}
}
return 0;
}
void printBoard(int board[9][9])
{
int i, j;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
printf("%d", board[i][j]);
}
}
printf("\n\n");
}
void fillBoard(int board[9][9], char tempStr[100000])
{
int i, j;
int k = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(tempStr[k] == '.')
{
board[i][j] = 0;
}
else
{
board[i][j] = (tempStr[k] - '0');
}
k++;
}
}
}
int precheck(int board[9][9])
{
int i, j, num;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(board[i][j] != 0)
{
num = board[i][j];
if(positionSafe(board, i, j, num) == 0)
{
return 1;
}
}
}
}
return 0;
}
So you are using precheck on an already filled board? That might be the problem because usedInCol, usedInRow and usedInBlock will return 1 if the value is already present. So precheck should be used only while filling the board, not after. It will always return 1 if you check values you take from the already filled board.

Resources