用深搜加回溯來實現求解數獨的所有解

思路:

1.對於當前的數獨狀態,我們可以把每個未填的位置的候選數字全部找出來,可以將候選數字放到一個數組或在vector中

vector <int> find(int x, int y); //找出soudu[x][y]的候選數字

2.如果有一些未填位置的候選數字爲一個,那麼這個就是確定項,我們可以先確定填補這個位置,如果所有的未填位置的候選數字都不唯一,那麼選擇候選數字最少的那麼位置,遍歷所有的可能性

3.怎麼判斷已解出答案,以及無法繼續進行下去,要回溯前一步的狀態呢,
搜先,設計一個 更新函數,每填補一個位置,就更新所有的未填位置的候補數字, void update(); 其次,設計幾個全局變量, 一個用來判斷,當前狀態是否有更新,如果沒有更新,認定爲所有的位置已經填滿,已解出答案,則輸出結果, bool can_update; 還有用bool can_continue來判斷是否存在某個未填位置沒有候選數字可填,這種情況, 無法繼續填數字 ; 另外, 在設計一個pair

#include<iostream>
#include<vector>
#include<utility>
using namespace std;

//初始化數獨
int soudu[9][9] = { 0,0,0,0,6,0,7,0,0,
                    0,8,0,0,0,0,0,2,0,
                    0,0,0,9,0,7,0,0,1,
                    0,0,0,0,0,0,1,0,0,
                    0,3,6,0,0,0,4,8,0,
                    0,0,0,0,7,0,0,0,0,
                    7,0,0,2,0,0,0,0,0,
                    0,5,0,0,0,0,0,6,0,
                    0,0,2,0,4,6,0,0,0};

//用來記錄每個位置的候選數字, index = row*9 + col
vector <int > pos[81];

// 記錄是否更新,若無更新,說明已填滿所有數字
bool can_update = true;

// 判斷是否還可以繼續下去,若ok=false, 那麼說明存在一個未填位置,沒有任何候選數字
bool ok = true;
pair < int, int > chosen_item;  // (chose_position, the_possible_chose_number)
int step = 0;
int sol = 0;
int is_over = false;

vector<int> find(int x, int y);
void update();
void print();

void sol_soudu() {
  step++;
  update();
  if (!can_update) {
    sol++;
    if (sol >= 10) is_over = true;
    // this is a solution
    print();
  } else if (!ok) {
    // there is a blank can not fill any number
    return ;
  } else if (!is_over) {
    int position = chosen_item.first;
    int x = position / 9;
    int y = position - x * 9;
    //選擇用size和k記錄下大小和vector,是因爲在往下深搜時,chose你_item會發生改變
    int size = chosen_item.second;
    vector <int> k = pos[position];
    for (int i = 0; i < size; i++) {
      soudu[x][y] = k[i];
      sol_soudu();
      // 回溯, 還原數據, 注意要還原ok, 因爲有可能前面的深搜,都沒有正確答案,導致ok=false
      soudu[x][y] = 0;
      ok = true;
    }
  }
}

vector <int> find(int x, int y) {
  int a[10] = {0};
  vector <int> ret;
  for (int i = 0; i < 9; i++){
    if (soudu[x][i] > 0) a[soudu[x][i]] = 1;
    if (soudu[i][y] > 0) a[soudu[i][y]] = 1;
  }
  int block_row = x / 3, block_col = y / 3;
  for (int i = block_row*3; i < block_row*3 + 3; i++)
    for (int j = block_col * 3; j < block_col*3 + 3; j++) {
      if (soudu[i][j] > 0) a[soudu[i][j]] = 1;
    }
  for (int i = 1; i <= 9; i++) if (!a[i]) ret.push_back(i);
  return ret;
}

void update() {
  for (int i = 0; i < 81; i++) pos[i].clear();
  chosen_item = make_pair(0, 10);
  can_update = false;
  for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++) if (!soudu[i][j]) {
      int k = i * 9 + j;
      pos[k] = find(i, j);
      if (pos[k].empty()) ok = false;
      if (pos[k].size() < chosen_item.second) chosen_item = make_pair(k, pos[k].size());
      can_update = true;
    }
}
void print() {
  cout << "-----------------------" << endl;
  for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++) {
      cout << soudu[i][j] << " ";
      if (j == 8) cout << endl;
    }
  cout << "-----------------------" << endl;
}

int main() {
  sol_soudu();
  cout << "step:" << step << endl;
  cout << "sol:" << sol << endl;
  return 0;
}
發佈了31 篇原創文章 · 獲贊 23 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章