Sudoku Backtracking - sudoku

Below is my solve method. When I call it in my main method, nothing happens and all the succeeding it is not executed but no error is reported by eclipse.
public boolean solve(int r, int c){
if(c>8){
c=0;
r++;
}
if(r>8){
return true;
}
while(table[r][c].value!=0){
c++;
if(c>8){
c=-0;
r++;
}
if(r>8){
return true;
}
}
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
solve(r,c);
}
}
table[r][c].value=0;
return false;
}
Will this algorithm backtrack? If not, why?

It looks like a logical error and hence eclipse does not report anything.
In for loop section of your code, you should have something like this
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
if(solve(r,c)){
return true;
}
table[r][c].value=0;
}
}
In your case you are un-assigning the table outside the for loop, which prevents the code from backtracking.
Here is my code for solving sudoku. Hope it helps.

Related

Why "control reaches end of non-void function"?

I'm cross-compiling using ARM/GNU C Compiler:6.3.1 with optimizations -O0. A simple function like this:
static uint8_t xyzzy(void) {
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
}
results in an error message:
Error control reaches end of non-void function [-Werror=return-type]
Is it really the case that the compiler doesn't know that one of those return statements must always be executed? Or is it me that is confused? And what is a recommended fix?
(P.S.: No, this is not a duplicate of Control reach end of non-void function, since I do have an else as the last clause.)
Simply add a return statement at the end of your function. GCC considers that your function doesn't have a return statement.
static uint8_t xyzzy(void)
{
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
return 0; // This will never be reached
}
You can get rid of unnecessary {}s and the elses:
static uint8_t xyzzy(void)
{
if (<predicate one>)
return 1;
if (<predicate two>)
return 2;
return 4; // Executed if none of the above conditions are met.
}

Why Numbers are getting printed in the form like "12-256" , "13-256" rather than 12,13 etc.?

I'm solving one of the algorithms problem from university to implemet queue using stacks.
I've got my logic right i guess but the numbers are getting printed in the form of 12-256, 13-256, 14-256 instead of 12,13,14.
Here's my C++ Code,
#include <iostream>
using namespace std;
class Stack{
private:
int arr[200];
int tos = -1;
public:
bool empty(){
return (tos == -1)?true:false;
}
void push(int element){
arr[++tos] = element;
}
int pop(){
return arr[tos--];
}
void show(){
if(tos == -1){
cout<<"stack empty";
}else{
for(int i=tos;i>0;i--)
cout<<arr[i]<<"\t";
}
}
};
class Queue{
private:
Stack s1,s2;
public:
void enQueue(int x){
//just using s1 to add new elements
s1.push(x);
}
int deQueue(){
if(s1.empty())
throw 'e';
else{
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
cout<<"\nelement to be removed:"<<s2.pop();
if(s2.empty())
throw 'f';
else{
int e;
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
}
}
}
};
int main()
{
try{
Queue q1;
q1.enQueue(12);
q1.enQueue(13);
q1.enQueue(14);
q1.enQueue(15);
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
}catch(char c){
cout<<"\nstack empty!";
}
return 0;
}
I'm basically a Python Guy so i'm not able to figure out what's wrong with this code.
I'm new to C++, so please guide me through this.
Thanks in advance!
deQueue suffers from the following problems.
It doesn't return anything.
It's OK for s2 to be empty after its top has been popped.
Here's an updated version that should work.
int deQueue(){
if(s1.empty())
throw 'e';
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
int ret = s2.pop();
cout<<"\nelement dequeued:"<< ret;
// This is not correct.
// It's OK for s2 to be empty after its top has been popped.
// if(s2.empty())
// throw 'f';
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
return ret;
}
Suggestion for further improvement
Queue does not need two Stack objects as member variables. s2 can be a function local variable in deQueue.
class Queue
{
private:
Stack s;
...
};
If you decide to make that change, you'll have to update enQueue and deQueue accordingly.

if/else performance difference depending on code location

Say I have an if/else statement and also some code that should execute in either case. I can put the "common" code in each block, or I can put it after the statement, e.g.:
if (x)
{
i++;
k++;
return;
}
else
{
j++;
k++;
return;
}
or
if (x)
{
i++;
}
else
{
j++;
}
k++;
return;
Is there a difference in performance between the two methods? If so, which one is faster, and why?

for loop Arduino/C

My aim is to create a for loop that iterates through numbers and once it reaches the maximum, it stops printing. So far I managed to create a piece of code that stops printing the x but it keeps printing zeroes. How can I stop Serial.print() function to be executed once the iteration reached the maximum value?
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
void loop(){
for(x=0;x<8;x++){
Serial.println(x);
delay(300);
if(x==7){
f = true;
}
if(f){
break;
}
}
}
Something like below should serve. Btw, I like to name my vars something meaningful to avoid potential confusion and make the code more intelligible.
(In general you are better off posting questions to the Arduino forum. More traffic and more knowledgeable/helpful people == more likelihood of getting an answer.)
int current;
int limit;
boolean complete;
void setup(){
Serial.begin(9600);
current = 0;
limit = 8;
complete = false;
}
void loop(){
if (!complete){
while (true){
Serial.println(current);
current++;
if (current >= limit){
complete = true;
break;
}
delay(300);
}
}
}
The key is the word loop - that function is called repetitively!
If you want something to happen once, do it in setup(), or (as another answer suggests), have a flag to keep track of the fact that you've done it already.
Another way (since x is global) would be:
void loop() {
if (x < 8) {
Serial.println(x);
x++;
}
}
or, getting rid of the global variable:
void loop() {
static int x = 0;
if (x < 8) {
Serial.println(x);
x++;
}
}
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
Very similar to your code, just put the print statement in the if statement and you're set.
void loop(){
for(x=0;x<8;x++){
if(!f) {
Serial.println(x);
}
delay(300);
if(x==7){
f = true;
}
}
}

How to traverse this tree in reverse order

I found an implementation of the Morris tree traversal,
It works fine,,, but having a bit of a problem trying
to traverse the tree in reverse order.. -
void MorrisTraversal(struct Node *root)
{
struct Node *p,*pre;
if(root==0) { return; }
for(p=root;p!=0;)
{
if(p->Left==0) { printf(" %d ",p->Data); p=p->Right; continue; }
for(pre=p->Left;pre->Right!=0 && pre->Right!=p;pre=pre->Right) { }
if(pre->Right==0)
{ pre->Right=p; p=p->Left; continue; }
else
{ pre->Right=0; printf(" %d ",p->Data); p=p->Right; continue; }
}
}
By reverse order, I'm assuming you mean reverse inorder traversal. You have at least two options:
You could modify the code and swap all the ->Right pointer references with ->Left ones.
You could replace the two printf statements with pushes onto a stack. Once the algorithm completes, you would then pop off the data from the stack to print them. This however defeats the whole purpose of the Morris traversal algorithm, which was to be stackless.
This related SO thread might help you understanding.
I hope this helps.
Here is one sample in Java. Click Here for one more sample using iteration instead of recursion.
public int depthOfTree(TreeNode node){
if(node == null){
return 0;
}
int leftDepth = depthOfTree(node.left);
int rightDepth = depthOfTree(node.right);
if(leftDepth > rightDepth){
return leftDepth+1;
} else {
return rightDepth+1;
}
}
//Reverse Level with recursion
public void reverseLevelOrder(TreeNode node){
int depth = depthOfTree(node);
for(int i=depth;i>0;i--){
printTree(node,i);
}
}
public void printTree(TreeNode node, int level){
if(node == null){
return;
}
if(level == 1){
System.out.print(node.data+" ,");
} else if(level>1){
printTree(node.left, level-1);
printTree(node.right, level-1);
}
}

Resources