Open main menu

CDOT Wiki β

Changes

TriForce

16 bytes added, 17:02, 15 March 2019
Assignment 2
|-
|
<nowiki> #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> // CUDA header file #include "cuda_runtime.h" #include "device_launch_parameters.h"
// A Backtracking program in C++ to solve Sudoku problem #include <stdio.h>
// UNASSIGNED is used for empty cells in sudoku grid #define UNASSIGNED 0
// N is used for the size of Sudoku grid. Size will be NxN #define BOXWIDTH 3 #define N 9
// This function finds an entry in grid that is still unassigned bool FindUnassignedLocation(int grid[N][N], int &row, int &col); // Checks whether it will be legal to assign num to the given row, col
// Checks whether it will be legal to assign num to the given row, col   __global__ void makeGuess(int* d_a, int guess, int row, int col) {
__shared__ bool found;
bool control = !(threadIdx.x || threadIdx.y);
int tidx = 0;
if (threadIdx.y == 2) {
tidx = (((row / BOXWIDTH) * BOXWIDTH) + threadIdx.x / BOXWIDTH) * N + threadIdx.x % BOXWIDTH + (col / BOXWIDTH) * BOXWIDTH;
}
else if (threadIdx.y == 1) {
d_a[row * N + col] = guess;
/**/
}
/* Takes a partially filled-in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution (non-duplication across rows, columns, and boxes) */ bool SolveSudoku(int grid[N][N], int* d_a, int row, int col) {
// If there is no unassigned location, we are done
if (!FindUnassignedLocation(grid, row, col))
return true; // success!
// consider digits 1 to 9
dim3 block(N, 3);
cudaMemcpy(d_a + row * N + col, &zero, sizeof(int), cudaMemcpyHostToDevice);
return false; // this triggers backtracking
}
/* Searches the grid to find an entry that is still unassigned. If found, the reference parameters row, col will be set the location that is unassigned, and true is returned. If no unassigned entries remain, false is returned. */ bool FindUnassignedLocation(int grid[N][N], int &row, int &col) {
for (; row < N; ++row) {
for (; col < N; ++col)
}
return false;
}
/* A utility function to print grid */ void printGrid(int grid[N][N]) {
for (int row = 0; row < N; row++)
{
printf("\n");
}
}
/* Driver Program to test above functions */ int main() {
/* 0 means unassigned cells */
int grid[N][N] = { {3, 0, 6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14, 4, 1, 0, 0, 0} };
/**/
int* d_a;
/**/
return 0;
}</nowiki>
|}
This code is capable of solving the 9x9 matrix supplied