Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7487997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:42:39+00:00 2026-05-29T14:42:39+00:00

So i have this program that should solve a futoshiki puzzle in C wich

  • 0

So i have this program that should solve a futoshiki puzzle in C
wich is loaded from a text file having this formatting :

5
0 | 0 | 0 | 0 | 0
- - - - v - - - - 
0 > 0 | 0 | 0 | 3
- - - - - - - - - 
0 | 0 < 2 | 0 | 0
- - - - v - - - - 
0 | 0 | 0 | 0 | 4
^ - v - - - - - - 
0 | 0 | 0 | 0 | 0

where 5 is the size of the matrix, and the numbers adjcent to the operators <, >, ^, v must satisfy the condition imposed by them, from the file all the characters on rows are divided by spaces
eg 0 |…
So I’ve managed to load the file, to check if it satisfies the math operators conditions, but I’m stuck on the recursive function

What I’d like to know:

Did i choose the right way to store the matrix or I’ve should have divided the numbers from the logical operators ?

How could I perform an recursive expansion on the matrix and how could I track the used number in a certain step(in case I would have to backtrack)?

eg. let’s say I arrive at index[j][j] where j<n (size of matrix) , starting from there I would have to decrement j (“touching”) only numbers and check if the sub-matrix satisfies the conditions

Here’s what I’ve managed to code so far.

where :

char **readmat(int *n); //reads the matrix from the file eliminating the spaces between chars

void print(char **mat,int n); //prints the stored matrix

int check(char **mat,int n); //checks if items of a matrix of size n satisfies the math operators

int expand (char **mat,int n,int i); //this should be the recursive functions that gets an element at a time and checks if there’s any condition to be satisfied, if so, increments it

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char **readmat(int *n);
void print(char **mat,int n);
int check(char **mat,int n); 
int expand (char **mat,int n,int i);

int main(int argc, char *argv[])
  {
  char **mat;
  int n, j;

  mat=readmat(&n);

  if(mat == NULL)
     return 1;

  if(check(mat,n)){
     print(mat,n);
  }
  else if(expand(mat,n,0)==1){
      print(mat,n);
  }
  else {
      printf("Nessuna soluzione trovata.\n");
  }

  for(j=0; j<=n;j++)
       free(mat[j]);
  free(mat);

  system("PAUSE");  
  return 0;
}

char **readmat(int *n){
     FILE *fp;
     char *line,nome[100];
     int i,j,k;
     char **mat;

     printf("Inserire il nome del file: ");
     scanf("%s",nome);
     fp=fopen(nome,"r");
     if(fp==NULL){
     printf("Errore apertura file");
     return NULL;
 }

 if(fgets(nome,100,fp)==NULL){
     printf("Formato file non valido\n");
     fclose(fp);
     return NULL;
 }
 if(sscanf(nome,"%d",n)!=1){
     printf("Errore nei parametri del file\n");
     fclose(fp);
     return NULL;    
 }

 (*n)=(((*n)*2)-1);


 mat=(char**)malloc((*n)*sizeof(char*));
 for(i=0;i<=(*n);i++)
    mat[i]=(char*)malloc((*n)*sizeof(char));

 line=(char*)malloc(2*(*n)*sizeof(char));

 i=0;

 while(i<=2*(*n) && fgets(line,2*(*n)+2,fp)!=NULL){
    j=0;
    k=0;
    while(j<=2*(*n)){
        if(line[j]!=' '){
           mat[i][k]=line[j];
           k++;
        }  
        j++;
    }
    i++;
 }   
 return mat;
 //print(mat, (*n));  
}

void print(char **mat,int n){
    int i=0,j=0;
    for (i=0; i<n; i++) {
     for (j=0; j<n; j++) {
        printf("%c", mat[i][j]);
     }
     printf("\n");
    }
}

int check(char **mat,int n) {

    int i,j;
    int k=1;

    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(mat[i][j]=='<'){
                if(mat[i][j-1] >= mat[i][j+1])
                    k=0;                               
            }
            else if(mat[i][j]=='>'){
                if(mat[i][j-1] <= mat[i][j+1])
                    k=0;
            }   
            else if(mat[i][j]=='^'){
                if(mat[i-1][j] >= mat[i+1][j])
                    k=0;    
            }
            else if(mat[i][j]=='v'){
                if(mat[i-1][j] <= mat[i+1][j])
                    k=0;                      
            }                            
        }
    }
    return k;                                
}
int expand (char **mat,int n,int i){

    int j=i/n;
    int k=i%n;
    int p;

    if(i>=n*n){

        return 1;
    }       
    else{
        if((mat[j][k]>47)&&(mat[j][k]<58)){
            if(mat[j][k]=='0'){     
                expand(mat,n,i+2);
            }   
            for (p=(mat[j][k]-48); p<(10-(mat[j][k]-48)); p++) {              
                mat[j][k]=48+p;                        
                if (check(mat,i)) {
              if (expand(mat, n, i+2)) {
                   return 1;
                    }
                }
            }
            i-=2;
            mat[j][k]='0';
        }
    }           
    return 0;
}

solution of the example : As you can see the logical conditions area clearly satisfied

0 | 0 | 1 | 0 | 0
- - - - v - - - - 
1 > 0 | 0 | 0 | 3
- - - - - - - - - 
0 | 0 < 2 | 0 | 0
- - - - v - - - - 
0 | 1 | 0 | 0 | 4
^ - v - - - - - - 
1 | 0 | 0 | 0 | 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:42:41+00:00Added an answer on May 29, 2026 at 2:42 pm

    The way you store the matrix shouldn’t matter too much. You can store it however you like, as long as you can easily get/set the numerical value of each spot, and evaluate whether the operators are satisfied.

    Very broadly, you can solve problems of this type by using an algorithm like this:

    //returns true if this function solved the puzzle, false otherwise.
    //gameData will be changed to the solved form of the puzzle if a solution exists, or remain unchanged if no solution exists.
    //(so, whatever language you're using, ensure gameData is passed by reference here)
    bool solve(gameData){
        if (!isValid(gameData)){return false;}  //oops, puzzle is unsolvable!
        if (isComplete(gameData)){return true;} //puzzle is already solved; no further modification needed.
    
        //choose a spot on the game board that hasn't been filled in yet.
        int x;
        int y;
        getEmptySpot(gameData, &x, &y);
    
        //iterate through all the possible values that could go into the empty spot.
        //you don't need anything fancy here to generate legal values for i;
        //if you accidentally supply an invalid value, then isValid()
        //will notice in the next solve() call.
        for (int i = 1; i <= 5; i++){
            //try putting i in the empty spot.
            setValue(gameData, x, y, i);
            if (solve(gameData)){ //putting i in the spot led to a solution!
                return true;
            }
        }
        //didn't find a solution :(
        //return gameData to its original state.
        setValue(gameData, x, y, 0);
        return false;
    }
    

    This algorithm does a brute-force recursive search, trying every possible value for each spot, and backtracking if it enters an illegal state. In the super-worst case, it runs in exponential time, but in practice, the isValid() call at the beginning will short-circuit any obviously infeasible branches, so it should finish reasonably quickly for a 5×5 input.

    Implementation of isValid, isComplete, getEmptySpot, and setValue will depend on how you defined gameData.

    isValid should check to see that the game data isn’t in an illegal state – in your case, it should check that all the greater-than comparisons are correct, and check that every number appears only once in each row and column. These checks should ignore spots whose value is 0, since they are just a placeholder meaning “not filled in yet”.

    isComplete should check to see that no spots have a “not filled in yet” placeholder. (isValid(gameData) && isComplete(gameData)) implies that gameData is solved.

    getEmptySpot should find a spot that hasn’t been filled in yet. If you’re concerned about speed, it should find a spot with the least number values that can be legally entered. This will reduce the width of the search tree pretty considerably.

    Finally, setValue should set the given spot to the given value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program that should execute a piece of code base on the
I have a program that generates a .shp file, and exports this as a
I have a python program that does something like this: Read a row from
So I have this program that I really like, and it doesn't support Applescript.
I have a C/ncurses program that I'm debugging/maintaining. This program does ripoffline twice: first,
I have a program that looks something like this: public partial class It {
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have written a program that uses qhttp to get a webpage. This works
I have an executable that is started by a windows service, this program will
This is my problem. I have a program that has to run in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.