GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — bmsce/sudoku.cpp at master · Jamuna1998/bmsce Expand file tree/ Copy pathsudoku.cppMore file actions 99 lines (92 loc) · 1.99 KB / Copy pathsudoku.cppFile metadata and controls99 lines (92 loc) · 1.99 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 #include<bits/stdc++.h> using namespace std; #define UNASSIGNED 0 #define N 9 #define SQN 2 bool Findunassignedlocation(int grid[N][N],int &row,int &col); bool isSafe(int grd[N][N],int row,int col,int num); bool UsedinRow(int grid[N][N],int row,int num); bool Usedincol(int grid[N][N],int col,int num); bool Usedinbox(int grid[N][N],int boxstartrow,int boxstartcol,int num); bool SolveSudoku(int grid[N][N]) { int row,col; if(!Findunassignedlocation(grid,row,col)) return true; for(int num=1;num<=N;num++) { if(isSafe(grid,row,col,num)) { grid[row][col]=num; if(SolveSudoku(grid)) return true; grid[row][col]=UNASSIGNED; } } return false; } bool Findunassignedlocation(int grid[N][N],int &row,int &col) { for(row=0;row<N;row++) { for(col=0;col<N;col++) { if(grid[row][col]==UNASSIGNED) return true; } } return false; } bool isSafe(int grid[N][N],int row,int col,int num) { return !UsedinRow(grid,row,num) && !Usedincol(grid,col,num) && !Usedinbox(grid,row-row%SQN,col-col%SQN,num) && grid[row][col]==UNASSIGNED; } bool UsedinRow(int grid[N][N],int row,int num) { for(int col=0;col<N;col++) { if(grid[row][col]==num) return true; } return false; } bool Usedincol(int grid[N][N],int col,int num) { for(int row=0;row<N;row++) { if(grid[row][col]==num) return true; } return false; } bool Usedinbox(int grid[N][N],int boxstartrow,int boxstartcol,int num) { for(int row=0;row<SQN;row++) { for(int col;col<SQN;col++) { if(grid[row+boxstartrow][col+boxstartcol]==num) return false; } } return false; } void printGrid(int grid[N][N]) { for(int row=0;row<N;row++) { for(int col=0;col<N;col++) cout<<grid[row][col]<<" "; cout<<endl; } } int main() { int grid[N][N]={{0,0,0,2,6,0,7,0,1},{6,8,0,0,7,0,0,9,0},{1,9,0,0,0,4,5,0,0},{8,2,0,1,0,0,0,4,0}, {0,0,4,6,0,2,9,0,0},{0,5,0,0,0,3,0,2,8},{0,0,9,3,0,0,0,7,4},{0,4,0,0,5,0,0,3,6},{7,0,3,0,1,8,0,0,0}}; if(SolveSudoku(grid)==true) printGrid(grid); else cout<<"no solution"; return 0; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |