Backtracking in C - c

I've heard about backtracking and I've searched a little bit .I thought I got the idea and wrote this piece of code to solve sudoku , but it seems to give wrong solutions (for example repeated numbers in a row) , what exactly i am doing wrong here ?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 9
bool not_in_row(int temp , int i , int grid[N][N]){
int f ;
for(f = 0 ; f < N ; f++)
if(grid[i][f] == temp)
return false ;
return true ;
}
bool not_in_column(int temp , int j , int grid[N][N]){
int f ;
for(f = 0 ; f < N ; f++)
if(grid[f][j] == temp)
return false ;
return true ;
}
bool not_in_sq(int i , int j , int grid[N][N] , int temp){
int k , t ;
int s = i - (i % 3) + 3, v = j - (j % 3) + 3;
for(k = i - (i % 3) ; i < s ; i++)
for(t = j - (j % 3) ; j < v ; j++)
if(grid[k][t] == temp)
return false ;
return true ;
}
bool sudoku_Solver(int grid[N][N] , int position){
if(position == 81)
return true ;
int i = position / 9 , j = position % 9 ;
if(grid[i][j] != 0)
return sudoku_Solver(grid , position + 1) ;
else{
int temp ;
for(temp = 1 ; temp <= N ; temp++){
if(not_in_row(temp , i , grid) && not_in_column(temp , j , grid) && not_in_sq(i , j , grid , temp))
{
grid[i][j] = temp ;
if(sudoku_Solver(grid , position + 1))
return true ;
}
}
}
grid[i][j] = 0 ;
return false ;
}
int main(int argc, char *argv[]) {
int i , j ;
int grid[9][9] = {{0,1,0,0,4,0,0,0,0}
,{6,0,0,0,0,0,0,1,8}
,{0,0,0,1,0,9,0,3,2}
,{2,0,5,0,0,3,8,0,0}
,{0,0,0,0,0,0,0,0,0}
,{0,0,4,7,0,0,1,0,5}
,{8,6,0,2,0,5,0,0,0}
,{4,2,0,0,0,0,0,0,9}
,{0,0,0,0,3,0,0,7,0}
} ;
sudoku_Solver(grid , 0) ;
for(i = 0 ; i < 9 ; i++){
for(j = 0 ; j < 9 ; j++){
printf("%d " , grid[i][j]) ;
}
printf("\n") ;
}
return 0;
}

Your loops in not_in_sq are wrong:
for(k = i - (i % 3) ; i < s ; i++)
for(t = j - (j % 3) ; j < v ; j++)
Look closely at i < s, i++, j < v, and j++. They are obviously incorrect. Here they are fixed:
for(k = i - (i % 3) ; k < s ; k++)
for(t = j - (j % 3) ; t < v ; t++)

Related

What is the problem with passing 2D array in the below question like this ? Showing an error , use of parameter outside function body before + token

CODE:-
int check(int a[] , int n , int sum , bool d[][sum+1]){
for(int p = 0 ; p<= n ;p++){
d[p][0] = 1 ;
}
for(int j = 1 ; j<=sum ;j ++){
d[0][j] = 0 ;
}
for(int p = 1 ; p<=n ;p ++){
for(int j = 1 ; j<=sum ;j ++){
if(a[p-1] > j) d[p][j] = d[p-1][j] ;
else d[p][j] = d[p-1][j] || d[p-1][j-a[p-1]] ;
}
}
int mini = sum ;
for(int p = 0 ; p <= total ;p ++){
if(d[n][p]) mini = min(mini , abs(sum-2*p)) ;
}
return mini ;
}
int differ(int a[], int n) {
int sum = 0 ;
for(int p = 0 ; p < n ;p ++) sum += a[p] ;
bool d[n+1][sum+1] ;
return check(a , n , sum , d) ;
}
This is showing an error: use of parameter outside function body before + token
int check(int a[] , int n , int sum , bool d[][sum+1]){
^
prog.cpp: In member function int Solution::check(...):
prog.cpp:10:28: error: n was not declared in this scope
for(int p = 0 ; p<=n ;p++){
^
prog.cpp:11:13: error: dp was not declared in this scope
d[p][0] = 1 ;
I even passed some constant value in the d array as in 2d arrays we cannot pass variable size but still , it is not working and throwing some error. So how should we pass 2d array in c++.

Out of memory error in c program for shortest path in a grid

I want to create a program that computes the path of minimum cost between 2 elements “S” and “E”,while carrying a pizza. We have a grid (2d array) that consists of ., W, X, S and E. “X” are walls-obstacles. If we move while carrying the load, every move (L,R,U,D) costs 2. However, we can leave the pizza in a wormhole “W” and take it again from another “W” in our grid, reducing the cost of our moves to 1. Leaving the pizza and taking it again costs 1.
I have written a program, but it consumes a lot of memory and the grader doesn’t accept it. I use a priority queue to store every next situation. The array prev is 3d, because we want to store, because prev[i][j][0] means we don’t carry the pizza and prev[i][j][1], means we carry it.
Here is a simple input :
S..........XX...X.X.X..XX.
.XXXXXXX.X.XX.X.X....X....
.X.....WXX..X.X.X..X...X.X
...XXXWWWWX...X...X..X.X..
XXX...XXXXXXXXXXXXXXXXXXX.
.W..XE....................
Can anyone help?
#define MAX_ROWS 1000
#define MAX_COLUMNS 1000
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
char path[1000000] ;
char grid[MAX_ROWS][MAX_COLUMNS];
char line[MAX_COLUMNS] ;
int row_coordinate ;
int column_coordinate ;
bool loaded ;
typedef struct {
bool loaded ;
int row_coordinate ;
int column_coordinate ;
}situation ;
typedef struct {
int row_c ;
int column_c ;
char move ;
}previous ;
typedef struct coordinates{
int row ;
int column ;
}position ;
typedef struct {
int priority ;
situation *data ;
} node_t ;
typedef struct {
node_t *nodes ;
int len ;
int size ;
} heap_t ;
void push (heap_t *h , int priority , situation *data) {
if (h->len + 1 >= h->size) {
h->size = h->size ? h->size * 2 : 4;
h->nodes = (node_t *)realloc(h->nodes, h->size * sizeof (node_t));
}
int i = h->len + 1;
int j = i / 2;
while (i > 1 && h->nodes[j].priority > priority) {
h->nodes[i] = h->nodes[j];
i = j;
j = j / 2;
}
h->nodes[i].priority = priority;
h->nodes[i].data = data ;
h->len++;
}
situation *pop (heap_t *h) {
int i, j, k;
if (!h->len) {
return NULL;
}
situation *data = h->nodes[1].data;
h->nodes[1] = h->nodes[h->len];
h->len--;
i = 1;
while (1) {
k = i;
j = 2 * i;
if (j <= h->len && h->nodes[j].priority < h->nodes[k].priority) {
k = j;
}
if (j + 1 <= h->len && h->nodes[j + 1].priority < h->nodes[k].priority) {
k = j + 1;
}
if (k == i) {
break;
}
h->nodes[i] = h->nodes[k];
i = k;
}
h->nodes[i] = h->nodes[h->len + 1];
return data;
}
int isEmpty(heap_t *ptr_heap){
if (ptr_heap == NULL){
return FALSE ;
}
if (ptr_heap->size == 0){
return TRUE ;
} else {
return FALSE ;
}
}
previous prev[1000][1000][2] ;
int main()
{
FILE *FilePtr;
int columns=0,rows=0,priority,i,j,k,cost,min_cost,length_of_path,l,c;
position start ;
FilePtr = fopen("text2.txt" , "r");
if(FilePtr == NULL)
{
printf("Can't open file");
exit(EXIT_FAILURE);
}
while (fgets(line,sizeof line,FilePtr)!= NULL)
{
for (columns=0; columns<(strlen(line)-1); ++columns){
grid[rows][columns]=line[columns] ;
//initialization of start
if (grid[rows][columns] == 'S'){
start.row=rows ;
start.column=columns ;
}
}
grid[rows][columns]='\0' ;
++rows ;
}
//until here cool !
//initialization of init(initial situation)
situation *init = &(situation){ .loaded = true , .row_coordinate = start.row , .column_coordinate = start.column} ;
heap_t *h = (heap_t *)calloc(1,sizeof (heap_t)) ;
//push init in queue with priority = 0
priority = 0 ;
push(h,priority,init) ;
//initialize array of situations prev
//prev is a 3d array !
for (i=0; i<rows; ++i){
for (j=0; j<columns; ++j)
for (k=0; k < 2; ++k) {
prev[i][j][k].move = 'O' ;
prev[i][j][k].row_c = -1 ;
prev[i][j][k].column_c = -1 ;
}
}
//prev[init] = "none"
prev[0][0][1].move = 'n' ;
//initializations
min_cost = 0 ;
cost = 0 ;
i = 0 ;
j = 0 ;
k = 1 ;
c = 0 ;
l = 0 ;
length_of_path = 0 ;
//initialize the current situation
situation *current = &(situation){ .loaded = true , .row_coordinate = 0 , .column_coordinate = 0 } ;
while(!isEmpty(h)){
current = pop(h) ;
i = current->row_coordinate ;
j = current->column_coordinate ;
if(current->loaded == true){k = 1 ;}
else {k = 0 ;}
//printf("%d\n",i) ;
//printf("%d\n",j) ;
//printf("%d\n",k) ;
//exit(1) ;
//until here cool
if ((grid[i][j] == 'E' )&&(k == 1)) {
min_cost = cost ;
printf("%d\n" , min_cost) ;
while (prev[i][j][k].move != 'n'){//as long as we are not in the initial situation
printf("%d\n" , min_cost) ;
//exit(1) ;
path[length_of_path] = prev[i][j][k].move ;//we store the path in reverse order
length_of_path++ ;
if (prev[i][j][k].move == 'L'){j-- ; }
else if (prev[i][j][k].move == 'R'){j++ ;}
else if(prev[i][j][k].move == 'U'){i-- ;}
else if(prev[i][j][k].move == 'D'){i++ ;}
else continue ;//we are in warmhole no change of coordinates
}
//exit(1) ;
for (l=length_of_path; l>=0; l--){//prints the solution/best path
printf("%d\n" , min_cost) ;
printf("%c" , path[l]) ;
}
//exit(1) ;
}
else {
//m = "L"
j-- ;
//we find the next situation
if (j>=0){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){ // if next doesn't exist in prev
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'L' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
j++ ; // go back to the begging
//m = "R"
j++ ;
//we find the next situation
if (j<columns){
situation *next = &(situation){ .loaded = true , .row_coordinate = 0 , .column_coordinate = 0} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'R' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
j-- ;
//m = "U"
i-- ;
//we find the next situation
if (i>=0){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'U' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
i++ ;
//m = "D"
i++ ;
//we find the next situation
if (i<rows){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'D' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost ;
push(h,priority,next) ;
}
i--;
// if m = "W"
c = 1 ; //lakis takes or leaves the pizza
cost = cost + c ;
priority = cost ;
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
push(h,priority,next) ;
}
}
fclose(FilePtr) ;
return 0;
}

why do I get wrong answer in c code for some of the input?

This time your task is simple.
Given two integers X and K , find the largest number that can be formed by changing digits at atmost K places in the number x.
Input:
First line of the input contains two integers X and K
K separated by a single space.
Output:
Print the largest number formed in a single line.
Constraints:
1 < X < 10^18
0 < K < 9
code -
int main()
{
long long x ;
scanf("%llu" , &x);
int k ;
scanf("%d" , &k);
long long max = (int)log10(x) + 1 ;
int arr[max] ;
long long temp = x ;
long long i ;
for(i = max -1 ; i >= 0 ; i-- )
{
arr[i] = temp % 10 ;
temp = temp / 10 ;
}
i = 0 ;
int cnt = k ;
while(cnt != 0)
{
if(arr[i] != 9)
{
arr[i] = 9 ;
cnt = cnt - 1 ;
}
i = i + 1 ;
}
int power = max -1 ;
long long answer = 0 ;
for(i = 0 ; i < max ; i++)
{
answer = answer + arr[i] * pow(10,power) ;
power = power -1 ;
}
printf("%llu" , answer );
return 0;
}
Getting partially correct ouput
wrong outputs for given input -
242358001399388784 9
169232736841900368 4
correct output for -
14500679550767648 1
8330936799410214 9
There's no need to compute answer. You can simply bypass it by printing the array.
#include<bits/stdc++.h>
int main()
{
long long int x ;
scanf("%lld" , &x);
int k ;
scanf("%d", &k);
long long int max = (int)log10(x) + 1 ;
int arr[max] ;
long long int temp = x ;
long long int i ;
for(i = max -1 ; i >= 0 ; i-- )
{
arr[i] = temp % 10 ;
temp = temp / 10 ;
}
i = 0 ;
int cnt = k ;
while(cnt != 0)
{
if(arr[i] != 9)
{
arr[i] = 9 ;
cnt = cnt - 1 ;
}
i = i + 1 ;
}
// JUST PRINT THE ARRAY
for(i=0;i<max;i++)
printf("%d",arr[i]);
return 0;
}
This program seems to solve your problem. You just add the right number of 9:s from the left of a string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
int loop = atoi(&(line[strlen (line) - 2]));
for (int i = 0; i< loop; i++) {
if (line[i] == '9')
loop++;
else line[i] = '9';
}
line[strlen(line)-2] = '\0';
printf("%s\n", line);
}
free (line);
return 0;
}
Test
./a.out
169232736841900368 4
999992736841900368

Learning Algorithm not working?

Learning Algorithm of Single layer precptons just works for only given training data, not for new input.Is there any way to correct this?
private void traning(){
while(true){
int errorCount = 0;
for( int i = 0 ; i < input_set_count ; i++ ){
float weightSum = 0;
for( int j = 0 ; j < no_input ; j++ ){
weightSum = weightSum + weight[j] * train_data[i][j];
}
int toutput = 0;
if( weightSum >= threshold )
toutput = 1;
int error = output[i] - toutput;
if( error != 0 )
errorCount++;
for( int j = 0 ; j < no_input ; j++ ){
weight[j] = weight[j] + error * train_data[i][j] * learning_rate;
}
}
if( errorCount == 0 )
break;
}
System.out.println("Traning Completed...");
}

How to produce the following output?

Since i'm new in the programming world ,i'm facing little problem while writting program for this pattern .I tried many times but the result is not what i wanted ?
The pattern is :
1
23
456
78910
What i have written is :-
#include<stdio.h>
#include<conio.h>
void main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch() ;
}
#include <stdio.h>
int main()
{
printf("1\n23\n456\n78910\n");
return 0;
}
produces the output you desire
You need to print a newline after the inner loop:
#include<stdio.h>
#include<conio.h>
int main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
printf("\n");
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch();
return(0);
}
There is another example:
int main()
{
int i, j, num = 1, line = 4;
for(i = 1; i <= line ; i++)
{
for(j = 0; j < i; j++)
{
printf("%d", num);
num++;
}
printf("\n");
}
return 0;
}
With single loop:
# include<stdio.h>
# define LIMIT 100
int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
printf("%d", i);next++;
if(diff == next-prev){
printf("\n");
diff++;prev = next = 0;
}
}
}

Resources