int	is_solvable(char *cp)
{
  char	*bin;
  char	*su;

  if ((su = cp_grid(cp)) == NULL || (bin = set_tab(1)) == NULL)
    return (0);
  prepare_tab(su, bin);
  while (!is_solved(su))
    if (!pass(su, bin) && !is_solved(su))
      return (free(bin), 0);
  return (free(bin), 1);
}
Example #2
0
void
Tux::try_start_walking()
{
  if (m_moving)
    return;
  if (m_input_direction == Direction::NONE)
    return;

  auto level = m_worldmap->at_level();

  // We got a new direction, so lets start walking when possible
  Vector next_tile;
  if ((!level || level->is_solved() || level->is_perfect()
      || (Editor::current() && Editor::current()->is_testing_level()))
      && m_worldmap->path_ok(m_input_direction, m_tile_pos, &next_tile)) {
    m_tile_pos = next_tile;
    m_moving = true;
    m_direction = m_input_direction;
    m_back_direction = reverse_dir(m_direction);
  } else if (m_ghost_mode || (m_input_direction == m_back_direction)) {
    m_moving = true;
    m_direction = m_input_direction;
    m_tile_pos = m_worldmap->get_next_tile(m_tile_pos, m_direction);
    m_back_direction = reverse_dir(m_direction);
  }
}
Example #3
0
//Affichage du sous-menu
void sub_menu(struct Labyrinthe lab, int choix){
	if (is_solved(lab)){
		printf("Que voulez-vous faire avec votre labyrinthe? (Entrer le numero de menu) : \n");
		if (choix != 1){
			printf("1-Afficher plusieurs chemins possibles s'ils existent\n");
		}
		if (choix != 2){
			printf("2-Afficher l'un des chemins le plus court\n");
		}
	} else {
		printf("\nIl n'y a pas de chemins possible pour ce labyrinthe\n");
	}
	printf("3-Retour au menu principal\n");
	printf("4-Quitter le programme\n");
	printf("0-Afficher le labyrinthe sans solutions\n");
}
Example #4
0
File: solve.c Project: MSaito/NumPl
/**
 * 再帰的解法(実体)
 * 人間的な解法アルゴリズムは使わずに機械的解法で解く
 * 機械的解法で解けないときは、複数候補のあるマスの候補から一つ選んで解けるか試す
 * ということを再帰的に実行する。
 * また、解が複数あるかどうかもチェックする。
 * 解けたときは array に解をセットする。
 * @param array ナンプレ盤面配列
 * @param data 再帰用持ち回しデータ
 * @return -1 矛盾があって解けない
 * @return 1 解けた
 * @return 2 解が複数存在する。
 */
static int recursion_solve_sub(numpl_array * array, recursion_solve_t *data)
{
    numpl_array work;
    work = *array;
    int solved = simple_solve(&work);
    if (solved < 0) {
        return solved;
    }
    solved = is_solved(&work);
    if (solved == 1) {
        if (!data->saved) {
            data->save = work;
            data->saved = 1;
        } else if (!equal(&data->save, &work)) {
            solved = 2;
        }
    }
    if (solved != 0) {
        *array = work;
        return solved;
    }
    int result = -1;
    for (int i = 0; i < ARRAY_SIZE; i++) {
        if (is_single(work.ar[i])) {
            continue;
        }
        cell_t s = work.ar[i];
        for (int j = 0; j < LINE_SIZE; j++) {
            work.ar[i] = s;
            unsigned int mask = s.symbol & (1 << j);
            if (!mask) {
                continue;
            }
            work.ar[i].symbol = mask;
            solved = recursion_solve_sub(&work, data);
            if (solved > 1) {
                *array = work;
                return solved;
            } else if (solved == 1) {
                *array = work;
                result = 1;
            }
        }
        break;
    }
    return result;
}
Example #5
0
int main(int argc, char **argv) {
    char *ptr;
    
    if (argc != 3)
        wrong_option(argv[0], "Falsche Paramenteranzahl");
    
    if(getopt(argv[1], "r") == TRUE) {

        if(strlen(argv[2]) == FIELD_SIZE)
            ptr = argv[2];
        else
            wrong_option(argv[0], "Falsche Feld-Groesse");

    }
    else if (getopt(argv[1], "f") == TRUE) {
        ptr = open_file(argv[2]);
    }

    // Umwandlung des linearen Feldes in ein mehrdimensionales Array
    int y=0, x=0;
    unsigned char multi_dim_field[9][9];
    for(; *ptr; ptr++) {
        multi_dim_field[y][x] = *ptr-'0';
        x++;
        if ((x % 9) == 0) {
            y++; x=0;
        }
    }

    printf("\nSudoku\n");
    print_field(multi_dim_field);

    solve(multi_dim_field);
    printf("\nRegel-Algorithmus\n"); 
    print_field(multi_dim_field);

    if ( is_solved(multi_dim_field) == FALSE ) {
        ADVANCED_MODE = TRUE;
        solve(multi_dim_field);
        printf("\nAdvanced-Algorithmus\n");
        print_field(multi_dim_field);
    }
    
    exit(EXIT_SUCCESS);

    return 0;
}
Example #6
0
int main(int argc, char** args) {
	if(argc != 2 || initialize(args[1])) {
		printf("Malformed or missing input!\nExemplary usage: sudoku 7.4.95...9.6.8...73.1.76.9.5...1.84613865472946.8......1.5..97.84.76.23..........\n\n");
		return -1;
	}

	while(!is_solved()) {
//		print_sudoku();

		if(simplify_nakedsingle())
			continue;

		if(simplify_hiddensingle())
			continue;

		if(simplify_nakedpair())
			continue;

		if(simplify_nakedtriple())
			continue;

		if(simplify_hiddenpair())
			continue;

		if(simplify_hiddentriple())
			continue;

		if(simplify_intersection())
			continue;

		if(simplify_xwing())
			continue;

		printf("THIS SUDOKU IS TOO HARD TO SOLVE FOR ME!\n");
		break;
	}

	print_sudoku();

	return 0;
}
Example #7
0
bool Sudoku::solve()
{
	if(is_solved())
		return true;
	if(!is_valid())
		return false;

	for(int i = 0; i < 9; ++i){
		for(int j = 0; j < 9; ++j){
			if(grid[i][j] == 0){
				for(int k = 0; k < 9; ++k){	
					grid[i][j] = k + 1;
					if(!solve())
						grid[i][j] = 0;
					else return true;
				}
				return false;
			}
		}
	}
	return true;
}
Example #8
0
File: solve.c Project: MSaito/NumPl
/**
 * 人間がナンプレを解くときの手法を使って解く
 * どの手法を使ったかをinfo に記録する。
 * @param array ナンプレ盤面配列
 * @param info どの解法を使ったかを記録する
 * @return 1 解けた
 * @return 0 解けていない
 * @return -1 矛盾が発生していて解けない
 */
int solve(numpl_array * array, solve_info * info)
{
    memset(info, 0, sizeof(solve_info));
    int c = 0;
    do {
        for (int i = 0; i < solvers[i] != NULL; i++) {
            c = solvers[i](array);
            if (c > 0) {
                set_info(info, c, solvers[i]);
#if defined(DEBUG)
                printf("info:");
                print_solve_info(info, 0);
                printf("\n");
#endif
                break;
            }
            if (c < 0) {
                break;
            }
        }
    } while (c > 0);
    if (c < 0) {
        info->solved = 0;
    } else {
        if (is_solved(array)) {
            info->solved = 1;
        } else {
            info->solved = 0;
        }
    }
    info->fx_count = count_fixed(array);
    if (c < 0) {
        return -1;
    } else {
        return info->solved;
    }
}
Example #9
0
void solve(int depth, int u, int d, int l, int r, int f, int b, int played) {

    solution[depth]=played;

    if (is_solved()) {
        printf("Rubik's cube solved in %d steps !!\n", depth);
        int k,j;
        for (k=1;k<=depth;k++) {
            optiMove();
            printf("move[%d]=",k);
            switch (sol[k])
            {
            case UP: { printf("UP\n"); break; }
            case DOWN: { printf("DOWN\n"); break; }
            case FRONT: { printf("FRONT\n"); break; }
            case BACK: { printf("BACK\n"); break; }
            case RIGHT: { printf("RIGHT\n"); break; }
            case LEFT: { printf("LEFT\n"); break; }
            default: break;
            }
        }
        getchar();
        return;
    }

    if (depth>=DEPTH_MAX) return;

    if (u<3) {
        move_U0();
        solve(depth+1, u+1, 0, 0, 0, 0, 0, UP);
        move_CU0();
    }

    if (d<3) {
        move_D0();
        solve(depth+1, 0, d+1, 0, 0, 0, 0, DOWN);
        move_CD0();
    }

    if (l<3) {
        move_L0();
        solve(depth+1, 0, 0, l+1, 0, 0, 0, LEFT);
        move_CL0();
    }

    if (r<3) {
        move_R0();
        solve(depth+1, 0, 0, 0, r+1, 0, 0, RIGHT);
        move_CR0();
    }

    if (f<3) {
        move_F0();
        solve(depth+1, 0, 0, 0, 0, f+1, 0, FRONT);
        move_CF0();
    }

    if (b<3) {
        move_B0();
        solve(depth+1, 0, 0, 0, 0, 0, b+1, BACK);
        move_CB0();
    }

}
Example #10
0
void test_whole(int matrix[HEIGHT][WIDTH], int expected_value) {
    test( is_solved(matrix) == expected_value ? "Pass" : "Fail" );
}
Example #11
0
bool Sudoku::solve(void)
{
	/*
	 * Variable declarations:
	*/
	bool failed = false;
	int i, j;
	/*
	 * STEP #1 // Fill suggestions:
	*/
	while (!failed)
	{
		fill_suggestions();
		dump();
		dump_suggestions();
		failed = true;
		for (i=0; i < 81; i++)
		{
			if (m_fields[i].get_suggestions_count() == 1 && m_fields[i].get_solution() == 0)
			{
				for (j=0; j < 9; j++)
				{
					if (m_fields[i].get_suggestions()[j] != 0)
					{
						m_fields[i].set_solution(m_fields[i].get_suggestions()[j]);
						m_fields[i].rm_suggestion(m_fields[i].get_suggestions()[j]);
						failed = false;
						break;
					};
				}
			};
		}
		m_stats_step1++;
	}
	/*
	 * Check if we solved the sudoku:
	*/
	if (is_solved())
	{
		std::cout << "SOLVED after Step#1 (Fill suggestions)" << std::endl;
		stats();
		return true;
	};
	/*
	 * STEP #2 // Try one number:
	*/
	while (try_numbers())
	{
		/*
		 * Fill suggestions:
		*/
		failed = false;
		while (!failed)
		{
			fill_suggestions();
			dump();
			dump_suggestions();
			failed = true;
			for (i=0; i < 81; i++)
			{
				if (m_fields[i].get_suggestions_count() == 1 && m_fields[i].get_solution() == 0)
				{
					for (j=0; j < 9; j++)
					{
						if (m_fields[i].get_suggestions()[j] != 0)
						{
							m_fields[i].set_solution(m_fields[i].get_suggestions()[j]);
							m_fields[i].rm_suggestion(m_fields[i].get_suggestions()[j]);
							failed = false;
							break;
						};
					}
				};
			}
		}
		if (is_solved())
		{
			std::cout << "SOLVED after Step#2 (Try one number)" << std::endl;
			stats();
			return true;
		};
		/*
		 * Restore fields:
		*/
		for (i=0; i < 81; i++)
			m_fields[i] = m_fields_bak[i];
		m_stats_step2++;
	}
	if (is_solved())
	{
		std::cout << "SOLVED after Step#2 (Try one number)" << std::endl;
		stats();
		return true;
	};
	/*
	 * STEP #3 // Try two numbers:
	*/
	for (i=0; i < 729; i++)
	{
		m_checked_suggestions[i].field = -1;
		m_checked_suggestions[i].suggestion = -1;
	}
	while (try_numbers())
	{
		/*
		 * Fill suggestions:
		*/
		failed = false;
		while (!failed)
		{
			fill_suggestions();
			dump();
			dump_suggestions();
			failed = true;
			for (i=0; i < 81; i++)
			{
				if (m_fields[i].get_suggestions_count() == 1 && m_fields[i].get_solution() == 0)
				{
					for (j=0; j < 9; j++)
					{
						if (m_fields[i].get_suggestions()[j] != 0)
						{
							m_fields[i].set_solution(m_fields[i].get_suggestions()[j]);
							m_fields[i].rm_suggestion(m_fields[i].get_suggestions()[j]);
							failed = false;
							break;
						};
					}
				};
			}
		}
		while (try_numbers_step3())
		{
			/*
			 * Fill suggestions:
			*/
			failed = false;
			while (!failed)
			{
				fill_suggestions();
				dump();
				dump_suggestions();
				failed = true;
				for (i=0; i < 81; i++)
				{
					if (m_fields[i].get_suggestions_count() == 1 && m_fields[i].get_solution() == 0)
					{
						for (j=0; j < 9; j++)
						{
							if (m_fields[i].get_suggestions()[j] != 0)
							{
								m_fields[i].set_solution(m_fields[i].get_suggestions()[j]);
								m_fields[i].rm_suggestion(m_fields[i].get_suggestions()[j]);
								failed = false;
								break;
							};
						}
					};
				}
			}
			if (is_solved())
			{
				std::cout << "SOLVED after Step#3 (Try two numbers)" << std::endl;
				stats();
				return true;
			};
			/*
			 * Restore fields:
			*/
			for (i=0; i < 81; i++)
				m_fields[i] = m_fields_bak2[i];
			m_stats_step3++;
		}
		/*
		 * Clear checked suggestions:
		*/
		for (i=0; i < 729; i++)
		{
			m_checked_suggestions_step3[i].field = -1;
			m_checked_suggestions_step3[i].suggestion = -1;
		}
		/*
		 * Restore fields:
		*/
		for (i=0; i < 81; i++)
			m_fields[i] = m_fields_bak[i];
	}
	if (is_solved())
	{
		std::cout << "SOLVED after Step#3 (Try two numbers)" << std::endl;
		stats();
		return true;
	};
	std::cout << "SOLVE FAILED (no success with trying two numbers)" << std::endl;
	stats();
	return false;
}
Example #12
0
int main(void){
	int i = 0, j = 0;
	int choix;
	char nomfichier[]="";
	char newLab=' ';
	struct Labyrinthe lab = {NULL,0,0};
	struct Labyrinthe lab_copy = { NULL, 0, 0 };
	struct Labyrinthe lab_copy2 = { NULL, 0, 0 };
	do{
		main_menu();
		scanf("%d",&choix);
		clear();
		switch(choix) {
			case 1 :
				//Création initiale du labyrinthe fixe, puis affichage
				lab = createFixedLab(lab, LAB_L_FIX, LAB_C_FIX, 0, 0, 3, 3);

				//allocation d'un autre tableau
				lab_copy.tab2D = tabAlloc(lab_copy.tab2D, LAB_L_FIX, LAB_C_FIX);

				researchPath(lab);
				choix = ' ';
				do{
					duplicate_struct(lab, &lab_copy);
					sub_menu(lab, choix);

					scanf("%d", &choix);
					clear();
					switch (choix) {
					case 0:
						printf("Mode Sans Solutions : \n");
						afficherLab(lab_copy, choix);
						break;
					case 1:
						display_multiple_paths(lab_copy);
						break;
					case 2:
						display_shortest_path(lab_copy);
						break;
					case 3:
						break;
					case 4:
						exit(0);
					default:
						printf("\nVous n'avez pas entrer la bonne valeur ! \n");
						break;
					}
				} while (choix!=3);
				free_memory(&lab);
				free_memory(&lab_copy);
				break;
			case 2 : 
				
				printf("Entrez le nom de votre fichier a charger (matrice.txt est le fichier a tester) : \n");
				scanf("%s", nomfichier);

				//initialisation des labyrinthes 
				lab = createLabFromFile(lab, nomfichier);
				lab_copy.tab2D = tabAlloc(lab_copy.tab2D, lab.l, lab.c);
				lab_copy2.tab2D = tabAlloc(lab_copy.tab2D, lab.l, lab.c);
				duplicate_struct(lab,&lab_copy2);

				researchPath(lab);
				choix = ' ';
				printf("\n");
				do{
					duplicate_struct(lab,&lab_copy);
					sub_menu(lab, choix);
					if (is_solved(lab)){
						if (choix != 5){
							printf("5-Afficher UN seul chemin possible [quelconque]\n\n");
						}
					}
					scanf("%d", &choix);
					clear();
					switch (choix) {
					case 0 : 
						printf("Mode Sans Solutions : \n");
						afficherLab(lab_copy, choix);
						break;
					case 1 :
						display_multiple_paths(lab_copy);
						break;
					case 2:
						display_shortest_path(lab_copy);
						break;
					case 3:
						break;
					case 4:
						exit(0);
					case 5:
						if (is_solved(lab)){
							display_one_path(lab_copy2);
						}
						break;
					default :
						printf("\nVous n'avez pas entrer la bonne valeur ! \n");
						break;
					}
				} while (choix!=3);
				//free_memory(&lab);
				free_memory(&lab_copy);
				free_memory(&lab_copy2);
				break;
			case 3 :
				printf("Quel est le nombre de ligne et de colonne? \n(exemple: taper '3 2' pour ligne = 3, colonne = 2)\n");
				scanf("%d %d", &lab.l, &lab.c);
				clear();
				lab = createRandomLab(lab);
				afficherLab(lab, 0);
				printf("\nEntrer les coordonees de l'entrer du labyrinthe (exemple : '3 2' pour ligne numero 3 et colonne numero 2) : \n");
				do{
					scanf("%d %d", &lab.xentrer, &lab.yentrer);
					if(lab.xentrer > (lab.l-1) || lab.yentrer > (lab.c-1) || lab.xentrer < 0 || lab.yentrer <0){printf("Erreur de saisie, ressaisir les coordonnees de l'entrer :\n" );}
				} while((lab.xentrer > (lab.l-1) || lab.yentrer > (lab.c-1)) || (lab.xentrer < 0 || lab.yentrer <0));
				clear();
				afficherLab(lab, 0);
				printf("\nEntrer les coordonees de la sortie du labyrinthe (exemple : '3 2' pour ligne numero 3 et colonne numero 2) :\n");
				do{
					scanf("%d %d", &lab.xsortie, &lab.ysortie);
					if(lab.xsortie > (lab.l-1) || lab.ysortie > (lab.c-1) || lab.xsortie < 0 || lab.ysortie <0){printf("Erreur de saisie, ressaisir les coordonnees de la sortie :\n" );}
				} while(lab.xsortie > (lab.l-1) || lab.ysortie > (lab.c-1) || lab.xsortie < 0 || lab.ysortie <0);
				clear();
				afficherLab(lab, 0);
				lab_copy.tab2D = tabAlloc(lab_copy.tab2D, lab.l, lab.c);
				researchPath(lab);
				choix = ' ';
				printf("\n");
				do{
					duplicate_struct(lab, &lab_copy);
					sub_menu(lab, choix);
					scanf("%d", &choix);
					clear();
					switch (choix) {
					case 0:
						printf("Mode Sans Solutions : \n");
						afficherLab(lab_copy, choix);
						break;
					case 1:
						display_multiple_paths(lab_copy);
						break;
					case 2:
						display_shortest_path(lab_copy);
						break;
					case 3:
						break;
					case 4:
						exit(0);
					default:
						printf("\nVous n'avez pas entrer la bonne valeur ! \n");
						break;
					}
				} while (choix != 3);
				free_memory(&lab);
				free_memory(&lab_copy);
				break;
			case 4 : 
				exit(0);
				break;
			default :
				printf("Le numero du menu est incorrect !\n");
				break;
		}
	}while(1);

}