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

Coding Challege1

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

Coding Challege1

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

#include <iostream>

#include <vector>
#include <set>
#include <sstream>

using namespace std;

// Function to get the box index for the 3x3 subgrid


int getBoxIndex(int row, int col) {
return (row / 3) * 3 + (col / 3);
}

string isValidSudoku(vector<vector<int>>& grid, const vector<int>& allowedHints,


int K) {
vector<set<int>> rowSets(9), colSets(9), boxSets(9);
vector<pair<int, int>> modifications;

for (int r = 0; r < 9; ++r) {


for (int c = 0; c < 9; ++c) {
int cellValue = grid[r][c];
if (cellValue != 0) { // Ignore empty cells
bool isHint = (cellValue % 10 == 0);
int originalValue = isHint ? cellValue / 10 : cellValue;

// Check for duplicates


if (rowSets[r].count(originalValue) ||
colSets[c].count(originalValue) || boxSets[getBoxIndex(r, c)].count(originalValue))
{
// This cell is incorrect
if (!isHint) { // Only count modifications for Tina's filled
cells
modifications.emplace_back(r, c);
}
} else {
// Add to sets if it's a valid entry
rowSets[r].insert(originalValue);
colSets[c].insert(originalValue);
boxSets[getBoxIndex(r, c)].insert(originalValue);
}
}

// If it's a hinted cell, check if we need to replace it


if (cellValue % 10 == 0) { // This means it's a hinted cell
int hintedValue = cellValue / 10;
if (find(allowedHints.begin(), allowedHints.end(), hintedValue) ==
allowedHints.end()) {
modifications.emplace_back(r, c);
}
}
}
}

// Check the number of modifications


if (modifications.empty()) {
return "Won";
} else if (modifications.size() > K) {
return "Impossible";
} else {
stringstream result;
for (const auto& mod : modifications) {
result << mod.first << " " << mod.second << "\n";
}
return result.str();
}
}

int main() {
vector<vector<int>> grid(9, vector<int>(9));
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
cin >> grid[i][j];
}
}

vector<int> allowedHints;
int hint;
while (cin >> hint) {
allowedHints.push_back(hint);
}

int K;
cin >> K;

string result = isValidSudoku(grid, allowedHints, K);


cout << result;

return 0;
}

You might also like