Economics Related

Economist

The Wall Street Journal

The Becker-Posner Blog

Greg Mankiw's Blog

Economist Blog

Prof Jeff Smith

Statistics Related

Schools

Raffles Institution (Raffles Junior College)

University of Michigan - Go BLUE!

University of Wisconsin-Madison Economics

 

 

  

  

Friday, April 06, 2007

Sudoku

Well, I have been cutting the sudoku puzzles on Michigan Daily and using that to kill time during boring lectures... But, after this project, I swear I will never see sudoku as an entertainment??!!

anyway...

just look at what I wrote for my sudoku C++code, this is not the code, just the comment for one function, how to generate a random suduko puzzle... Argh!

/*
Firstly we randomly generate the upper left subgrid(which will remain constant throughout).
There are BOARD_SIZE!(in this case 9! = 362880) different permutations of the first subgrid.
We can write out the entire sudoku from manipulating the first subgrid

***************************

Example:

first subgrid (also known as subgrid A00)
123
456
789

so we write down the entire sudoku grid as


123 | 456 | 789 - column 0
456 | 789 | 123 - column 1
789 | 123 | 456 - column 2
----------------------
234 | 567 | 891 - column 3
567 | 891 | 234 - column 4
891 | 234 | 567 - column 5
----------------------
345 | 678 | 912 - column 6
678 | 912 | 345 - column 7
912 | 345 | 678 - column 8


***************************

After that, we modify this entire sudoku grid so that a certain number is not always at the same place.
We will not modify the subgrid A00 as any changes has already been permutated in the first step.

We do the modification by swapping any corresponding colums
Column 3 and 6 are corresponding columns
Column 4 and 7 are corresponding columns
Column 5 and 8 are corresponding columns
there are BOARD_N*BOARD_N*BOARD_N (in this case 2^3=8) possible colums swappings which will generate a different sudoku board

and swapping any rows that are within the same horizontal subgrids
Rows in A01 A11 and A21
and/or
Rows in A02 A12 and A22
and there are (BOARD_N-1)*(BOARD_N!)*(BOARD_N!) (in this case 72) possible row swappings which will generate a different sudoku board

In total, our code will be able to generate
(BOARD_SIZE!)*(BOARD_N*BOARD_N*BOARD_N )*((BOARD_N-1)*(BOARD_N!)*(BOARD_N!))(in a 9*9 sudoku, it will be 209,018,880) different sudoku puzzles


In order to get random unknown space,
we force the value of rand()%2 into board[][].permanent
then reset the value of board[][].number to 0 if board[][].permanent is false

*/

0 Comments:

Post a Comment

<< Home

  web counter code
frontpage hit counter

Photobucket

 

 

 

 

 

 

 

People have asked me why my name is so damn bloody hard to pronounce, I would say it's not my problem. Just in case anyone who is interested to find out what the name actually means, here it is:

萱 pronounces as xuan, is the flower called daylily pictured above;

仲 pronounces as zhong, means I am the second child in the family

 Photobucket

弦弦诉人意,弓弓道世情

可以高贵,可以平凡
可以不问英雄出处
可以痴情风流的二胡
一弦可以大江东去
一弦可以月游西湖
一曲未了竟有琴弦启开江南
让俏丽的姑娘
用浓浓的茉莉花语
从红袖里伸出酥手
把一个河山香透

My Erhu, My Forever Love