fire net.txt
来自「以前ACM在哈工程HRBEU做的一些简单题目」· 文本 代码 · 共 64 行
TXT
64 行
#include <iostream>
using namespace std;
const int MAX_N = 4;
char map[MAX_N][MAX_N];
int n, nMaxCount;
bool CanPut(int row, int col)
{
int i;
for (i = row - 1; i >= 0; i--)
{
if (map[i][col] == 'O') return false;
if (map[i][col] == 'X') break;
}
for (i = col - 1; i >= 0; i--)
{
if (map[row][i] == 'O') return false;
if (map[row][i] == 'X') break;
}
return true;
}
void PutBlockhouses(int pos, int count)
{
if (pos == (n * n))
{
if (count > nMaxCount)
{
nMaxCount = count;
}
}
else
{
int row = pos / n;
int col = pos % n;
if ((map[row][col] == '.') && (CanPut(row, col)))
{
map[row][col] = 'O';
PutBlockhouses(pos + 1, count + 1);
map[row][col] = '.';
}
PutBlockhouses(pos + 1, count);
}
}
int main()
{
cin >> n;
while (n > 0)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> map[i][j];
nMaxCount = 0;
PutBlockhouses(0, 0);
cout << nMaxCount << endl;
cin >> n;
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?