Пример #1
0
int is_solvable()
{
	// consider some row and column and num (which runs from 1 to 9)
	int row, col, num;
	
	// if there is any unassigned cell, fetch the co-ordinates and store in row and column
	// else puzzle is done
	if(!find_next_unassigned(&row, &col))
		return 1;
	
	// start with num = 1 for any cell
	for(num = 1; num <= N; num++)
	{
		if(is_safe(row, col, num))
		{
			// if num can be assigned to this cell store it assuming it to be true
			sudoku[row][col] = num;
			
			// if storing num in that cell leads to solution continue
			if(is_solvable())
				return 1;
			
			// else remove the num from that cell and backtrack
			sudoku[row][col] = UNASSIGNED;
		}
	}
	
	return 0;
}
Пример #2
0
void solve()
{
	if(is_solvable())
		print_stylized();
	else
		printf("No Solution Exists!\n");
}
Пример #3
0
int main(){
	int puzzle[3][3];
	int i,j;
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
			scanf("%d",&puzzle[i][j]);
	(is_solvable(puzzle))?printf("YES\n"):printf("NO\n");
	return 0;
}
Пример #4
0
void Game::update() {
    if (master_state == STATE_ANIMATION) {
        frame_counter++;
        if (skip || frame_counter >= to_frame(frame_delay)) {
            frame_counter = 0;
            if (detailed_state == STATE_ANIMATION_INITIALIZING_DECK) {
                if (card_counter <= 27) {
                    //destination of card
                    int col = col_tracker.x;
                    sf::Vector2f destination = field_rect[PLAY_FIELD][col].getPosition();
                    destination.y += VERT_CARD_SPACING * col_tracker.y;
                    int animation_type = STATE_ANIMATION_MOVING_CARD;
                    if (col == col_tracker.y) {
                        animation_type = STATE_ANIMATION_MOVE_AND_FLIP_CARD;
                    }
                    Position p(destination, sf::Vector3i(DECK, 0, field[DECK][0].size()), sf::Vector3i(PLAY_FIELD, col, col_tracker.y));
                    field[DECK][0].back().init_animation(animation_type, p, to_frame(0.3f));
                    transit.push_back(field[DECK][0].back());
                    field[DECK][0].pop_back();
                    card_counter++;
                    col_tracker.y++;
                    if (col_tracker.y > col_tracker.x) {
                        col_tracker.x++;
                        col_tracker.y = 0;
                    }
                }
            } else if (detailed_state == STATE_ANIMATION_RETURN_CARD) {
                if (!cursor.empty()) {
                    Card c = cursor.front();
                    Position p = c.get_position_data();
                    p.coord.second = p.coord.first;
                    p.loc.second = p.loc.first;
                    c.init_animation(STATE_ANIMATION_MOVING_CARD, p, to_frame(0.2f));
                    transit.push_back(c);
                    cursor.erase(cursor.begin());
                }
            } else if (detailed_state == STATE_ANIMATION_FLIP_DECK) {
                if (!field[DECK][1].empty()) {
                    Card c = field[DECK][1].back();
                    Position p = c.get_position_data();
                    p.coord.second = field_rect[DECK][0].getPosition();
                    p.loc.second = sf::Vector3i(DECK, 0, field[DECK][0].size());
                    c.init_animation(STATE_ANIMATION_MOVING_CARD, p, to_frame(0.3f));
                    transit.push_back(c);
                    field[DECK][1].pop_back();
                }
            } else if (detailed_state == STATE_ANIMATION_SOLVE_DECK) {
                //locate card with lowest value on left most position
                Position p(sf::Vector3i(-1, -1, -1));
                int min_card_val = -1;
                for (int i = 0; i < field[PLAY_FIELD].size(); i++) {
                    if (field[PLAY_FIELD][i].empty()) continue;
                    if (min_card_val == -1 || field[PLAY_FIELD][i].back().get_value() < min_card_val) {
                        p.loc.first = sf::Vector3i(PLAY_FIELD, i, field[PLAY_FIELD][i].size() - 1);
                        p.coord.first = field[PLAY_FIELD][i].back().get_center();
                        min_card_val = field[PLAY_FIELD][i].back().get_value();
                    }
                }
                //possibility that there are no cards on field
                //check if we found a card
                if (p.loc.first.x != -1) {
                    //card to move
                    Card c = field[PLAY_FIELD][p.loc.first.y].back();
                    //locate home location to move it
                    int col;
                    for (col = 0; col < field[HOME].size(); col++) {
                        //check if the column is empty
                        if (field[HOME][col].empty() && ace_locations[col] == -1) {
                            if (c.get_value() == 1) { //if we have an ace, place it
                                ace_locations[col] = c.get_suit();
                                break;
                            }
                        } else if (ace_locations[col] == c.get_suit()) {
                            //found pile of same suit
                            break;
                        }
                    }
                    //launch moving animation
                    p.loc.second = sf::Vector3i(HOME, col, field[HOME][col].size() - 1);
                    p.coord.second = field_rect[HOME][col].getPosition();
                    c.init_animation(STATE_ANIMATION_MOVING_CARD, p, to_frame(0.5f));
                    transit.push_back(c);
                    field[PLAY_FIELD][p.loc.first.y].pop_back();
                }
            }
        }
        //update aimations
        for (int i = 0; i < transit.size(); i++) {
            //check if we should skip the animation or not
            if (skip) {
                transit[i].finish();
            } else {
                transit[i].next();
            }
            //removed animations that are finished, add it to its new location
            if (transit[i].animation_finish()) {
                Position p = transit[i].get_position_data();
                sf::Vector3i loc = p.loc.second;
                field[loc.x][loc.y].push_back(transit[i]);
                transit.erase(transit.begin() + i--);
                //check if the animation sequence is over
                if (transit.empty()) {
                    if (detailed_state == STATE_ANIMATION_INITIALIZING_DECK) {
                        if (card_counter > 27) {
                            master_state = STATE_PLAYING;
                        }
                    } else if (detailed_state == STATE_ANIMATION_FLIP_DECK) {
                        if (field[DECK][1].empty()) {
                            master_state = STATE_PLAYING;
                        }
                    } else if (detailed_state == STATE_ANIMATION_SOLVE_DECK) {
                        if (has_won()) {
                            master_state = STATE_PLAYING;
                        }
                    } else if (cursor.empty()) {
                        master_state = STATE_PLAYING;
                    }
                    //this is here because any card moving action requires animation
                    //during transition, cards are not all present on the field
                    if (!solvable && is_solvable()) {
                        solvable = true;
                    }
                }
            }
        }
    }
    if (!won && has_won()) {
        won = true;
    }
}