Otion @ 2023-02-03 16:29:22
从0,0开始dfs
check之后如果ok就下一列继续搜
如果拉了就搜同行的下一列
#include <iostream>
#include <vector>
using namespace std;
// 只需要在nxn的表格中放n个棋子
char board[15][15] = {'0'};
vector<int> line;
vector<vector<int>> ans;
int scale;
inline bool check(int col, int row)
{
// 检查列
for (int i = 0; i < scale; i++)
{
if (board[i][col] == 'Q')
{
return false;
}
}
// 检查行
for (int i = 0; i < scale; i++)
{
if (board[row][i] == 'Q')
{
return false;
}
}
// 检查左上
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (board[i][j] == 'Q')
{
return false;
}
}
// 检查右上
for (int i = row, j = col; i >= 0 && j < scale; i--, j++)
{
if (board[i][j] == 'Q')
{
return false;
}
}
// 检查左下
for (int i = row, j = col; i < scale && j >= 0; i++, j--)
{
if (board[i][j] == 'Q')
{
return false;
}
}
// 检查右下
for (int i = row, j = col; i < scale && j < scale; i++, j++)
{
if (board[i][j] == 'Q')
{
return false;
}
}
return true;
}
void dfs(int col, int row)
{
if (line.size() == scale)
{
ans.push_back(line);
line.clear();
return;
}
if (col >= scale || row >= scale)
{
return;
}
if (col >= scale && row >= scale)
{
line.clear();
return;
}
if (check(col, row))
{
line.push_back(col);
board[row][col] = 'Q';
for (int i = 0; i < scale; i++)
{
dfs(i, row + 1);
}
board[row][col] = '0';
line.pop_back();
}
else
{
dfs(col + 1, row);
}
}
int main()
{
cin >> scale;
//把第一行的所有点进行dfs
//这里写歪了,第一个参数是col,第二个参数是row
for (int i = 0; i < scale; i++)
{
dfs(i, 0);
}
int count = 0;
for (auto p : ans)
{
if (count == 3)
{
break;
}
for (auto q : p)
{
cout << q << " ";
}
cout << endl;
count++;
}
cout << ans.size() << endl;
return 0;
}
by Sprague_Garundy @ 2023-02-03 16:35:42
什么牛逼标题
by dist_22r @ 2023-02-03 16:36:25
tlqtj,jbl
by lonely_cyx @ 2023-02-03 16:38:08
@ZZR0930
这算nm tlqtj
by Otion @ 2023-02-03 16:38:32
好像每次搜完要把棋盘memset一下,加上memset还是不太对
by zzyxl_qaq @ 2023-02-03 16:40:38
@ZZR0930 别学了个新词就乱用好吧,lz是代码求调,跟题解有什么关系
另外说一句,这标题是真逆天(
by Happy_Orca @ 2023-02-03 16:42:37
@ZZR0930 一看就知道是求调的,你语文是谁教的,这都看不出来
by RP_INT_MAX @ 2023-02-03 16:44:08
btd(
by SmileMask @ 2023-02-03 16:46:01
这标题nb
by Otion @ 2023-02-03 16:46:02
大家都在说什么我怎么看不懂哇呜呜呜呜呜
全是dalao 球球看一眼叭
by expnoi @ 2023-02-03 16:50:12
@ZZR0930 您不懂tlqtj是什么就不要乱发表言论,OK?