Question
int CSP::updateDomain(const State state) { std::vector iterator updated_state; for (int i = 0; i < 81; i++) { int col = i % 9; int
int CSP::updateDomain(const State state) { std::vector
if (state.values[row][col] == 0) { variables[row][col].domain.clear();
for (int num = 1; num <= 9; num++) { variables[row][col].domain.push_back(num); } updated_state = variables[row][col].domain.begin(); while (updated_state != variables[row][col].domain.end()) { if (checkForRow(row, col, state, *updated_state) || checkForColumn(row, col, state, *updated_state) || checkForGrid(row, col, state, *updated_state)) { variables[row][col].domain.erase(updated_state); updated_state = variables[row][col].domain.begin(); } else { updated_state++; } } } } }
/*Arc consistency use*/ void CSP::arcConsistency(const State state) {
} CSP.h
#ifndef Sudoku_CSP_h #define Sudoku_CSP_h
#include
using namespace std;
/*This structure defines the variable format for the Sudoku problem*/ struct Variable{ vector
/*State structure: one state of assignment for the 9 by 9 variables*/ struct State{ int values[9][9]; //reload the equal check operator bool operator==(const State& s) const { for(int i = 0; i < 81; i++) { int y = i / 9; int x = i % 9; if(values[y][x] != s.values[y][x]) { return false; } } return true; } };
/*Struct of arrow, which is a pair of node with directions*/ struct Arrow{ int head; int tail; Arrow(); Arrow(int h, int t) { head = h; tail = t; } bool operator==(const Arrow& a) const { if(head == a.head && tail == a.tail) return true; else return false; } };
/*This is the class that solve the constraint problem by using the algroithms learned in the class, such as backtrack, forward checking,...*/ class CSP{ public: State cur_state; //it has 9*9 values inside the variable. Each value represents the assignment for the corresponding cell. If it is 0, it means no assignment for the cell yet. Variable variables[9][9]; //Each varible represents one cell of the sudocu table. Each variables[i][j] stack
public: CSP(); ~CSP(); int updateDomain(State state); //for forward checking used. Based on current setting, update the domain. void arcConsistency(const State state); //similar to the updateDomain() in the forward checking, this unction is stronger for arcChecking use bool goalCheck(const State state); bool checkForAssignment(int r, int s, State state, int number); bool checkForRow(int r, int s, State state, int number); bool checkForColumn(int r, int s, State state, int number); bool checkForGrid(int r, int s, State state, int number); void setData(int *data); int goBack(int *chosen_cell); bool backTrack(int *chosen_cell); bool forwardChecking(int *chosen_cell); bool forwardCheckingOrder(int *chosen_cell); bool arcChecking(int *chosen_cell); bool arcCheckingOrder(int *chosen_cell); void clearData(); // this is called everytime when the search algorithm (radio button) is switched to another one. void reshuffleDomain(); //make the domain values in random order void sortDomain(); //make the domain in increasing order };
Complete the function Arc Consistency. Iterator updated_state throws an error. Can you resolve that ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started