Solve Wordoku in C with Recursion

  • Thread starter wannawin
  • Start date
In summary, the conversation is about a person seeking help with a programming problem involving writing a C program to solve a wordoku puzzle recursively. The user enters a string of characters and a 9x9 grid representing the puzzle, and the program is expected to output the solution. The person seeking help also shares their attempt at a solution, but another person advises them not to post their code online due to the risk of plagiarism and academic offences.
  • #1
wannawin
14
0

Homework Statement



I'm fairly new to programming and I'm trying to write a program in C to solve wordoku (sudoku with letters) problems recursively, but I seem to be having some trouble with the recursion part of it.

The user enters in a string of characters which will make up the 9 allowable characters in the game. They then enter the 9 lines of the given puzzle, placing "#' in any blank spaces. In the end the solution is printed.

The Attempt at a Solution


Here is what I have done.
Code:
#include <stdio.h>
#include <stdlib.h>
bool chkRow(int i, int j, char c, char puzzle[][10])
{
	for(;j<9;j++)
		if(c==puzzle[i][j])
			return false;
	return true;
}

bool chkColumn(int i, int j, char c, char puzzle[][10])
{
	for(;i<9;i++)
		if(c==puzzle[i][j])
			return false;
	return true;
}

bool chkCell(int i, int j, char c, char puzzle[][10])
{
	int x=i-(i%3);
	int y=j-(j%3);
	for(i=x;i<x+3;i++)
		for(j=y;j<y+3;j++)
			if(c==puzzle[i][j])
				return false;
	return true;
}

void solver(char charBank[10], char puzzle[][10], int i, int j, int k)
{
	if(puzzle[i][j]=='#')	
	{
		char c=charBank[k];		

		if(i==9)
			for(i=0;i<9;i++)
			{
				printf("%s\n",puzzle[i]);	
			}
		if(chkRow(i, j, c, puzzle)
		&&chkColumn(i, j, c, puzzle)
		&&chkCell(i, j, c, puzzle))
		{
			puzzle[i][j]=c;
			if(j==8)
				solver(charBank, puzzle, i+1, 0, 0);
			else
				solver(charBank, puzzle, i, j+1, 0);	
		}
		else				
			solver(charBank, puzzle, i, j, k+1);
	}
	else if(j==8)
		solver(charBank, puzzle, i+1, 0, 0);
	else
		solver(charBank, puzzle, i, j+1, 0);
}

int main(void)
{
	int i=0,j=0,k=0;
	
	char charBank[10];
	gets(charBank);
	
	char puzzle[9][10];
	for(i=0;i<9;i++)
		gets(puzzle[i]);             
	printf("\n");
	                                 
	solver(charBank, puzzle, 0, 0, 0);
	return 0;
}
 
Physics news on Phys.org
  • #2
Lab 7, eh.

Please don't post your code for Labs online. You're giving anybody the chance to copy it, and posting it online is an academic offence. If I were you, I'd delete the code from here and try working at it again.

Losing 3 % of your mark isn't a big deal, losing 3 % of your mark AND getting an getting an academic offence is.
 

1. What is Wordoku?

Wordoku is a word-based variation of the popular sudoku puzzle. Instead of using numbers, players must use letters to complete the grid while ensuring that each row, column, and 3x3 square contains all the letters of a given word.

2. How does recursion help solve Wordoku?

Recursion is a programming technique where a function calls itself until a base case is reached. In the case of solving Wordoku, recursion allows the program to repeatedly check for valid solutions and backtrack if necessary until the puzzle is solved.

3. What is the benefit of using C for solving Wordoku?

C is a high-level programming language that allows for efficient and optimized code. It also has powerful memory management capabilities, which is crucial for solving complex puzzles like Wordoku.

4. Can Wordoku be solved without recursion?

Yes, it is possible to solve Wordoku without using recursion. However, recursion offers a more elegant and efficient solution, as it eliminates the need for complex loops and nested conditions.

5. Are there any limitations to using recursion for solving Wordoku?

One limitation of using recursion for solving Wordoku is that it can be computationally expensive for larger puzzles. Additionally, if the puzzle has multiple solutions, recursion may only find one of them, leading to an incorrect solution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
851
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
982
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top