This commit is contained in:
2026-07-28 00:33:27 +08:00
parent 2b6dc69bde
commit 8bc1b22a1c
14 changed files with 529 additions and 124 deletions
+71
View File
@@ -0,0 +1,71 @@
#include <iostream>
#include <vector>
using namespace std;
void log_array(const vector<bool>& arr, const string& name) {
clog << name << ": ";
for (const auto& cell : arr) {
clog << cell << " ";
}
clog << endl;
}
vector<vector<bool>> get_grid(const int size) {
vector<vector<bool>> grid;
for (int _ = 0; _ < size; _++) {
string pattern;
vector<bool> row;
cin >> pattern;
for (const char c : pattern) { row.push_back(c != '.'); }
grid.push_back(row);
}
return grid;
}
long long dfs(const vector<vector<bool>>& grid,
const int remaining_chests,
const int curr_empty_row,
vector<bool>& col_occupation) {
const unsigned int size = grid[0].size();
if (remaining_chests == 0) { return 1; }
if (curr_empty_row >= size) { return 0; }
// clog << "curr_empty_row: " << curr_empty_row << endl;
// log_array(col_occupation, "col_occupation");
long long ans = 0;
for (int col = 0; col < size; col++) { // 这行放
if (!grid[curr_empty_row][col]) { continue; }
if (col_occupation[col]) { continue; }
// vector<bool> new_col_occupation(col_occupation);
col_occupation[col] = true;
ans += dfs(grid, remaining_chests - 1, curr_empty_row + 1, col_occupation);
col_occupation[col] = false;
}
ans += dfs(grid, remaining_chests, curr_empty_row + 1, col_occupation); // 不在这行放
return ans;
}
long long dfs(const vector<vector<bool>>& grid,
const int remaining_chests,
const int curr_empty_row = 0) {
vector<bool> col_occupation(grid[0].size(), false);
return dfs(grid, remaining_chests, curr_empty_row, col_occupation);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int size, chest_count;
while (cin >> size >> chest_count) {
if (size < 0) { break; }
vector<vector<bool>> grid = get_grid(size);
cout << dfs(grid, chest_count) << endl;
}
// cin >> size;
// chest_count = size;
// const vector<vector<bool>> grid = get_grid(size);
// cout << dfs(grid, chest_count) << endl;
return 0;
}