CSES - Putka Open 2015 – 2/6 - Results
Submission details
Task:Sudoku
Sender:
Submission time:2015-08-16 08:34:51 +0300
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.07 sdetails

Code

#include <iostream>

using namespace std;

int a[9][9];


bool isValid(int row, int col, int x)
{
	// row
	for (int c = 0; c < 9; ++c)
		if (a[row][c] == x)
			return false;

	// col
	for (int r = 0; r < 9; ++r)
		if (a[r][col] == x)
			return false;

	// block
	int r = (row / 3) * 3;
	int c = (col / 3) * 3;
	if(a[r + 0][c + 0] == x) return false;
	if(a[r + 0][c + 1] == x) return false;
	if(a[r + 0][c + 2] == x) return false;
	if(a[r + 1][c + 0] == x) return false;
	if(a[r + 1][c + 1] == x) return false;
	if(a[r + 1][c + 2] == x) return false;
	if(a[r + 2][c + 0] == x) return false;
	if(a[r + 2][c + 1] == x) return false;
	if(a[r + 2][c + 2] == x) return false;

	return true;
}

int m = 0;

bool solve(int l) {
	if (l > m)
		m = l;
	if (l == 81)
		return true;
	
	int r = l / 9;
	int c = l % 9;
	
	for (int x = 1; x <= 9; ++x) {
		if (isValid(r, c, x)) {
			a[r][c] = x;
			if (solve(l + 1))
				return true;
			a[r][c] = 0;
		}
	}
	
	return false;
}


int main(int argc, char** argv)
{
	cin.sync_with_stdio(false);

	int k;
	cin >> k;

	for (int i = 8; i >= 0; --i, k /= 10)
		a[0][i] = k % 10;

	solve(9);

	for (int r = 0; r < 9; ++r) {
		for (int c = 0; c < 9; ++c)
			cout << a[r][c];
		cout << endl;
	}

	cout << m << endl;

	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
592836471

correct output
592836471
836471592
471592836
928364715
364715928
...

user output
592836471
134257689
678149235
213465798
456798123
...

Test 2

Verdict: ACCEPTED

input
672935418

correct output
672935418
935418672
418672935
729354186
354186729
...

user output
672935418
134268579
589147236
213456897
456789123
...

Test 3

Verdict: ACCEPTED

input
329174658

correct output
329174658
174658329
658329174
291746583
746583291
...

user output
329174658
145268379
678359124
213485796
456791283
...

Test 4

Verdict: ACCEPTED

input
376958421

correct output
376958421
958421376
421376958
769584213
584213769
...

user output
376958421
124367589
589124367
213475698
457689132
...

Test 5

Verdict: ACCEPTED

input
875694321

correct output
875694321
694321875
321875694
756943218
943218756
...

user output
875694321
123578469
469123578
214356897
356789142
...