0% found this document useful (0 votes)
16 views

Week6 Recursive Backtracking Cont

The document discusses two recursive backtracking problems: the Sudoku problem of filling in the remaining empty cells of a partially filled 9x9 grid according to rules, and the Queen problem of placing queens on a chessboard so that no two queens can attack each other. Pseudocode and complete code solutions are provided for the Sudoku problem to recursively try values for empty cells and check if they are valid.

Uploaded by

8a1.vugianam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week6 Recursive Backtracking Cont

The document discusses two recursive backtracking problems: the Sudoku problem of filling in the remaining empty cells of a partially filled 9x9 grid according to rules, and the Queen problem of placing queens on a chessboard so that no two queens can attack each other. Pseudocode and complete code solutions are provided for the Sudoku problem to recursively try values for empty cells and check if they are valid.

Uploaded by

8a1.vugianam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

C BASIC

C BASIC
RECURSIVE BACKTRACKING

3
CONTENT

• Sudoku problem (P.02.06.01)


• Queen problem (P.02.06.02)
• TSP problem (P.02.06.03)

4
SUDOKU PROBLEM (P.02.06.01)
1 0 0 4 0 0 7 0 9
• Let the 9 x 9 square board be divided into 81 sub-squares, and the board
is also divided into 9 sub-square panels each measuring 3 x 3 (see 0 5 0 0 0 0 0 2 0
Figure below). Some cells of the table have been filled with an integer 0 8 9 1 2 3 4 0 6
from 1 to 9. Fill in the remaining cells (cells with value 0), each cell
2 0 4 3 6 5 8 0 7
with a value from 1 to 9 to satisfy: numbers on each row, each column,
and each pair of 3 x 3 squares are different 0 6 5 8 0 0 2 1 4
• Data 8 9 7 2 1 4 3 6 5
• 9 lines, each line is 9 elements of 1 row on the table 0 0 0 6 0 2 9 0 8
• Result 6 0 0 9 7 8 5 0 1
• Write down the number of number filling options 0 0 0 0 0 1 6 0 0

5
SUDOKU PROBLEM
1 0 0 4 0 0 7 0 9
0 5 0 0 0 0 0 2 0

stdin stdout 0 8 9 1 2 3 4 0 6

003400089 64 2 0 4 3 6 5 8 0 7
006789023 0 6 5 8 0 0 2 1 4
080023456
004065097 8 9 7 2 1 4 3 6 5
060090014 0 0 0 6 0 2 9 0 8
007204365
030602078 6 0 0 9 7 8 5 0 1
000000000
000000000 0 0 0 0 0 1 6 0 0

6
SUDOKU PROBLEM – PSEUDOCODE

• Numbering 0 1 2 3 4 5 6 7 8
• The rows and columns of the table are numbered 0, 1, . . .,
0 1 0 0 4 0 0 7 0 9
8
• Each 3 x 3 subsquare table is characterized by an index in 1 0 5 0 0 0 0 0 2 0 0
row i and column j (each index in row i, column j 2 0 8 9 1 2 3 4 0 6
corresponds to 3 consecutive rows and 3 consecutive 2 0 4 3 6 5 8 0 7
3
columns of the table, i, j = 0 , 1, 2
• Representing solutions: X[0..8, 0..8]
4 0 6 5 8 0 0 2 1 4 1
5 8 9 7 2 1 4 3 6 5
• Marked array:
6 0 0 0 6 0 2 9 0 8
• markR[r, v] = 1: value v does appear in row r, with r = 0,
…, 8, v = 1,…, 9 7 6 0 0 9 7 8 5 0 1 2
• markC[c, v] = 1: value v does appear in column c, with c 8 0 0 0 0 0 1 6 0 0
= 0,…, 8, v = 1,…, 9
• MarkS[i, j, v] = 1: value v does appear in a subsquare 3 x
3 at coordinate (i, j), với i, j = 0, 1, 2, v = 1, …, 9 0 1 2

7
SUDOKU PROBLEM – PSEUDOCODE
try(r, c){
• Order to iterate: from top to down and from left if X[r, c] > 0 then {
to right if r = 8 and c = 8 then solution();
• Function try(r, c): try values for X[r, c] else { if c = 8 then try(r+1, 0); else try(r, c+1); }
• Consider values of v from 1 to 9 return;

• Function check(v, r, c): }


for v = 1 to 9 do {
• Value v is valid when it has not appeared
if check(v, r, c) then {
• Row r: markR[r, v] = 0
X[r, c] = v;
• Column c: markC[c, v] = 0
markR[r,v] = 1; markC[c,v] = 1; markS[r/3,c/3,v] = 1;
• Subsquare 3 x 3 at coordinate (r/3, c/3):
if r = 8 and c = 8 then solution();
markS[r/3, c/3, v] = 0
else { if c = 8 then try(r+1, 0); else try(r, c+1); }
markR[r,v] = 0; markC[c,v] = 0; markS[r/3,c/3,v] = 0;
X[r, c] = 0;
}
}
}

8
SUDOKU PROBLEM – COMPLETE CODE
#include <stdio.h> void Try(int r, int c){
int X[9][9]; if(X[r][c] > 0){
int markR[9][10]; if(r == 8 && c == 8) cnt++;
int markC[9][10]; else{ if(c == 8) Try(r+1,0); else Try(r,c+1); }
int markS[3][3][10]; return;
int cnt; }
int check(int v, int r, int c){ for(int v = 1; v <= 9; v++){
if(markR[r][v]) return 0; if(check(v,r,c)){
if(markC[c][v]) return 0; X[r][c] = v;
if(markS[r/3][c/3][v]) return 0; markR[r][v] = 1; markC[c][v] = 1; markS[r/3][c/3][v] = 1;
return 1; if(r == 8 && c == 8) cnt++;
} else{ if(c == 8) Try(r+1,0); else Try(r,c+1); }
markR[r][v] = 0; markC[c][v] = 0; markS[r/3][c/3][v] = 0;
X[r][c] = 0;
}
}
}

9
SUDOKU PROBLEM – COMPLETE CODE
void init(){ int main(){
for(int v = 1; v <= 9; v++){ init();
for(int r = 0; r <= 8; r++) for(int r = 0;r <= 8; r++){
markR[r][v] = 0; for(int c = 0; c <= 8; c++){
for(int c = 0; c <= 8; c++) int v; scanf("%d",&v);
markC[c][v] = 0; X[r][c] = v;
for(int i = 0; i <= 2; i++) if(v > 0){
for(int j = 0; j <= 2; j++) markR[r][v] = 1; markC[c][v] = 1;
markS[i][j][v] = 0; markS[r/3][c/3][v] = 1;
} }
} }
}
cnt = 0;
Try(0,0);
printf("%d",cnt);
return 0;
}

10
QUEEN PROBLEM (P.02.06.02)

• On an international chess board of size n x n, there are k queens (0 <= k < n). The state of the chessboard is
represented by the matrix Anxn in which A(i, j) = 1 means that row i, column j has a queen and A(i,j) = 0
means row i, column j does not have a queen. queen. Count the number Q of ways to place n - k other
queens on the chessboard so that no two queens can attack each other.
• Data
• Line 1: An integer n (1 <= n <= 15) stdin stdout
• Line i + 1 (i = 1, 2, …, n): row ith of A 8 3
• Result 00000000
00000000
• Write the value of Q 01000000
00000000
00000000
00010000
00000000
00000000

11
QUEEN PROBLEM – PSEUDOCODE
check(r, k){
• Representing solutions: x[1..n], where return (mark[r] = 0) and (markD1[n+k-r] = 0) and (markD2[k+r] = 0);
x[i] is the row index of the queen in }
column i
try(k){
• Constraints if x[k] > 0 then {
• x[i] ≠ x[j] if k = n then cnt = cnt + 1; else try(k+1);
• x[i] + i ≠ x[j] + j return;
• x[i] – i ≠ x[j] - j }

• Marked arrays: for r = 1 to n do {


if check(r, k) then {
• markR[r] = 1: row r has a queen
x[k] = r; mark[r] = 1; markD1[n+k-r] = 1; markD2[k+r] = 1;
• markD1[d] = 1: there is a queen in
row r, column k with d = n – k + r if k = n then cnt = cnt + 1;
else try(k+1);
• MarkD2[d] = 1: there is a queen in
row r column k with k + r = d x[k] = 0; mark[r] = 0; markD1[n+k-r] = 0; markD2[k+r] = 0;
}
}
}

12
QUEEN PROBLEM– CODE
#include <stdio.h> void Try(int k){
#define N 100 if(x[k] > 0){
int n; if(k == n) cnt += 1; else Try(k+1);
int x[N]; return;
int a[N]; }
int mark[N]; for(int r = 1; r <= n; r++){
int markD1[2*N]; if(check(r,k)){
int markD2[2*N]; x[k] = r; mark[r] = 1; markD1[n+ k - r] = 1; markD2[k + r] = 1;
int cnt; if(k == n) cnt += 1; else Try(k+1);
int check(int v, int k){ x[k] = 0; mark[r] = 0; markD1[n+ k - r] = 0; markD2[k + r] = 0;
if(mark[v] == 1) return 0; }
if(markD1[n + k - v]==1) return 0; }
if(markD2[k + v]==1) return 0; }
return 1;
}

13
QUEEN PROBLEM– COMPLETE CODE
void input(){ int main(){
for(int i = 1; i < N; i++) mark[i] = 0; input();
for(int i = 0; i < 2*N; i++){ markD1[i] = 0; markD2[i] = 0; } cnt = 0;
scanf("%d",&n); Try(1);
for(int i = 1; i <= n; i++) x[i] = 0; printf("%d",cnt);
for(int i = 1; i <= n; i++){ return 0;
for(int j = 1; j <= n; j++){ }
int e; scanf("%d",&e);
if(e == 1){
x[j] = i;
mark[i] = 1; markD1[n + j - i] = 1; markD2[i+j] = 1;
}
}
}
}

14
TSP PROBLEM (P.02.06.03)

• Given n points 1, 2, . . ., n. The travel distance from point i to point j is d(i, j), with i, j = 1, 2, …, n. Find the
trip starting from point 1, passing through other points, each point exactly once and returning to point 1 with
the smallest total length.
• Data
• Line 1: An integer n (1 <= n <= 20)
• Line i + 1 (i = 1, 2, …, n): Row i of the matrix d stdin stdout
• Result 4 7
• The length of the found trip 0119
1093
1902
9320

15
TSP PROBLEM - PSEUDOCODE
try(k){
• Representing solutions: x[1, . . ., n], where x[i] is the point ith of for v = 1 to n do {
the trip, i = 1, 2, …, n. The trip is: x[1]  x[2]  . . .  x[n]  if mark[v] = 0 then {
x[1]
x[k] = v;
• Marked array: f = f + d[x[k-1], v]; mark[v] = 1;
• mark[v] = 1: v does appear in the trip if k = n then {

• Branch and Bound if fmin > f + d[x[n],x[1]] then

• Cm: the minimal distance between two points fmin = f + d[x[n],x[1]];


}else{
x[1] if f + Cm*(n-k+1) < fmin then

x[n] x[2] try(k+1);


}
. .
. f = f – d[x[k-1], v]; mark[v] = 0;
.
. . }
}
x[k+1] x[k-1] }

x[k+1]

16
TSP PROBLEM - PSEUDOCODE
try(k){

The part has not been for v = 1 to n do {


The part has been
reached, contains n-k+1 reached, contain k-1 if mark[v] = 0 then {

stages with length ≥ stages, length f x[k] = v;


Cm*(n-k+1) f = f + d[x[k-1], v]; mark[v] = 1;
if k = n then {
x[1]
if fmin > f + d[x[n],x[1]] then
x[n] x[2] fmin = f + d[x[n],x[1]];
. . }else{
. . if f + Cm*(n-k+1) < fmin then
. . try(k+1);
}
x[k+1] x[k-1]
f = f – d[x[k-1], v]; mark[v] = 0;
x[k] }
}
Lower bound f + Cm*(n-k+1)
}

17
TSP PROBLEM – COMPLETE CODE
#include <stdio.h> void Try(int k){
int n;// number of cities for(int v = 1; v<= n; v++){
int d[100][100]; if(mark[v]==0){
int x[100]; x[k] = v;
int mark[100]; f = f + d[x[k-1]][v]; mark[v] = 1;
int f;// length of the current part of the trip if(k == n){
int f_min;// the record of the trip if(f_min > f + d[x[n]][x[1]])
int Cm; f_min = f + d[x[n]][x[1]];
}else{
if (f + Cm*(n-k+1) < f_min)
Try(k+1);
}
mark[v] = 0; f = f - d[x[k-1]][v];
}
}
}

18
TSP PROBLEM – COMPLETE CODE
void input(){ int main(){
scanf("%d",&n); input();
Cm = 10000000; for(int v = 1; v <= n; v++) mark[v] = 0;
for(int i = 1; i <= n; i++){ x[1] = 1; mark[1] = 1;
for(int j =1; j <= n; j++){ f = 0; f_min = 10000000;
scanf("%d",&d[i][j]); Try(2);
if(i != j && d[i][j] < Cm) Cm = d[i][j]; printf("%d",f_min);
} return 0;
} }
}

19
THANK YOU !

20

You might also like