CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:asonnine
Submission time:2024-11-08 20:50:28 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5details
#20.00 s2, 3, 4, 5details
#30.00 s3, 4, 5details
#40.00 s4, 5details
#50.00 s5details
#60.00 s5details

Code

#include <bits/stdc++.h>
#include <vector>
using namespace std;

int extra = 0;

int char_to_int(char in) {
	return (int)in - 65;
}

int recursev(int n, int x, int dx, int y, int dy, vector<string> &field, vector<int> species, vector<vector<bool>> &used) {
	int out = 0;
	
	if (y == n - 1) {
		return 0;
	}
	
	if (used[y+1][dy-1]) {
		used[y+1][dy-1] = false;
		bool up = true;
		vector<int> new_species = species;
		for (int i = x; i <= x+dx; i++) {
			if (new_species[char_to_int(field[y][i])] != 1) {
				new_species[char_to_int(field[y][i])]--;
			} else {
				up = false;
				break;
			}
		}
		if (up) {
			out++;
			out += recursev(n, x, dx, y+1, dy-1, field, new_species, used);
		}
	}
	
	if (used[y][dy-1]) {
		used[y][dy-1] = false;
		bool down = true;
		vector<int> new_species = species;
		for (int i = x; i <= x+dx; i++) {
			if (new_species[char_to_int(field[y+dy][i])] != 1) {
				new_species[char_to_int(field[y+dy][i])]--;
			} else {
				down = false;
				break;
			}
		}
		if (down) {
			out++;
			out += recursev(n, x, dx, y, dy-1, field, new_species, used);
		}
	}
	
	return out;
}

int vertical(int n, int x, int dx, vector<string> &field, vector<int> species) {
	vector<vector<bool>> used(n);
	vector<bool> temp(n);
	fill(temp.begin(), temp.end(), true);
	fill(used.begin(), used.end(), temp);
	return recursev(n, x, dx, 0, n-1, field, species, used)+1;
}

int recurseh(int n, int x, int dx, vector<string> &field, vector<int> species, vector<vector<bool>> &used) {
	int out = 0;
	bool called = false;
	
	if (x == n - 1) {
		extra += vertical(n, x, dx, field, species);
		return -1;
	}
	
	
	if (used[x+1][dx-1]) {
		used[x+1][dx-1] = false;
		bool left = true;
		vector<int> new_species = species;
		for (int i = 0; i < n; i++) {
			if (new_species[char_to_int(field[i][x])] != 1) {
				new_species[char_to_int(field[i][x])]--;
			} else {
				left = false;
				break;
			}
		}
		if (left) {
			called = true;
			out++;
			out += recurseh(n, x+1, dx-1, field, new_species, used);
		}
	}
	
	if (used[x][dx-1]) {
		used[x][dx-1] = false;
		bool right = true;
		vector<int> new_species = species;
		for (int i = 0; i < n; i++) {
			if (new_species[char_to_int(field[i][x+dx])] != 1) {
				new_species[char_to_int(field[i][x+dx])]--;
			} else {
				right = false;
				break;
			}
		}
		if (right) {
			called = true;
			out++;
			out += recurseh(n, x, dx-1, field, new_species, used);
		}
	}
	
	if (!called) {
		extra += vertical(n, x, dx, field, species);
		out--;
	}
	return out;
}

int main() {
	int n;
	cin >> n;
	vector<string> field(n);
	vector<int> species(26);
	for (int i = 0; i < n; i++) {
		cin >> field[i];
	}
	for (auto i : field) {
		for (auto j : i) {
			species[char_to_int(j)]++;
		}
	}
	vector<vector<bool>> used(n);
	vector<bool> temp(n);
	fill(temp.begin(), temp.end(), true);
	fill(used.begin(), used.end(), temp);
	int h = recurseh(n, 0, n-1, field, species, used)+1;
	fill(temp.begin(), temp.end(), true);
	fill(used.begin(), used.end(), temp);
	int v = recursev(n, 0, n-1, 0, n-1, field, species, used)+1;
	//cout << h << endl << v << endl << extra << endl;
	cout << h*v+extra << "\n";
}

Test details

Test 1

Group: 1, 2, 3, 4, 5

Verdict:

input
54
4 4 0
3 1 3
3 2 2
4 0 4
...

correct output
0
0
0
0
0
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 2

Group: 2, 3, 4, 5

Verdict:

input
284
6 1 0
5 0 2
7 1 5
7 7 5
...

correct output
0
0
35280
0
36720
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 3

Group: 3, 4, 5

Verdict:

input
841
19 3 12
19 19 13
19 7 13
20 11 15
...

correct output
40291066
0
0
0
0
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 4

Group: 4, 5

Verdict:

input
1000
15 12 6
7 1 6
44 4 26
6 6 5
...

correct output
0
5040
494558320
0
340694548
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 5

Group: 5

Verdict:

input
1000
892 638 599
966 429 655
1353 576 1140
1403 381 910
...

correct output
0
0
0
249098285
0
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 6

Group: 5

Verdict:

input
1000
2000 1107 508
2000 1372 249
2000 588 65
2000 1739 78
...

correct output
750840601
678722180
744501884
159164549
868115056
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc