#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int Row = 11, Col = 11;
int n, m, ans;
char matrix[Row][Col];
int vis[4][24];
bool IsOver()
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == '.') continue;
//所在行 列 主对角线 副对角线
if (!vis[0][i] && !vis[1][j] && !vis[2][j - i + m] && !vis[3][j + i]) return false;
}
}
return true;
}
bool dfs(int depth,int x,int y) {
if (IsOver()) return true;
if (depth == 0) return false;
for (int i = x; i < n; i++) {
for (int j = (i == x)?y:0; j < m; j++) {
int a = vis[0][i], b = vis[1][j], c = vis[2][j - i + m], d = vis[3][j + i];
if(a && b && c && d) continue;
int nx = i + (j + 1) / m;
int ny = (j + 1) % m;
vis[0][i] = vis[1][j] = vis[2][j - i + m] = vis[3][j + i] = 1;
if (dfs(depth - 1, nx, ny)) return true;
vis[0][i] = a;
vis[1][j] = b;
vis[2][j - i + m] = c;
vis[3][j + i] = d;
}
}
return false;
}
int main()
{
int kCase = 0;
while (cin >> n && n) {
cin >> m;
memset(matrix, '.', sizeof(matrix));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
ans = max(n, m);
for (int i = 0; i < ans; i++) {
if (dfs(i,0,0)) {
ans = i;
break;
}
}
cout << "Case " << ++kCase << ": " << ans << endl;
}
return 0;
}
习题7-10(uva-11214)
最新推荐文章于 2021-07-01 11:56:40 发布