P1219 [USACO1.5] 八皇后 Checker Challenge
思考过程
仔细思考此题要求,我们不妨想到一个非二维的思路。先分析传统思路,重点便是 dfs 中通过枚举访问的方式判断该皇后所在的点位是否和法,(在不在被其余皇后的攻击范围之中)对于这种方式,明显会 TLE 。所以,需要考虑优化。分析原代码慢在哪里,不难发现,是那个判断该皇后是否合法的过程,那么,有没有一种 时复的方式快速检查是否合法呢?有,比如说对于列是否重复,我们可以使用一个 bool 数组存下当前列有没有重复,而行,似乎不用考虑,因为每次递归起始的传输变量既可以看作第几个皇后,也能看做第几行(因为每行最多一个皇后),然后再来看两条斜边,首先是主对角线,不难发现,每一次 的值始终不变,(例如从1,0 -> 2,1 -> 3,2 -> 4,3),再是副对角线,发现 的值始终不变。(例如4,1 -> 3,2 -> 2,3 -> 1,4) 由此我们得到了一份更快的代码( ):
#include<bits/stdc++.h>
using namespace std;
int n;
bool zhu[30],lie[30],fu[30];
int a[30],idx;
void dfs(int c)
{
if(c > n)
{
idx ++;
if(idx <= 3)
{
for(int i = 1;i <= n;i ++)
{
cout << a[i] << " ";
}
cout << "\n";
}
}
for(int i = 1;i <= n;i ++)
{
if(!lie[i] && !zhu[i + c] && !fu[c - i + n])
{
lie[i] = zhu[i + c] = fu[c - i + n] = 1;
a[c] = i;
dfs(c + 1);
lie[i] = zhu[i + c] = fu[c - i + n] = 0;
a[c] = 0;
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << idx;
return 0;
}
P2105 K皇后
思路
有了前面的基础,我们可以使用前面的标记方法,再用递归来统计总格子数。
#include<bits/stdc++.h>
using namespace std;
const int N = 4e4 + 7;
bool zhu[N],fu[N],lie[N],hang[N];
int n,m,k;
int cnt = 0;
void dfs(int c)
{
if(c > n)
{
return ;
}
for(int i = 1;i <= m;i ++)
{
if(!lie[i] && !zhu[i + c] && !fu[c - i + 20000] && !hang[c])
{
cnt ++;
}
}
dfs(c + 1);
}
int main()
{
cin >> n >> m >> k;
for(int i = 1;i <= k;i ++)
{
int x,y;
cin >> x >> y;
lie[y] = zhu[y + x] = fu[x - y + 20000] = hang[x] = 1;
}
dfs(1);
cout << cnt;
return 0;
}
/*
12 13 6
10 4
12 10
1 1
2 3
3 2
2 6
*/
T726810 搜索进阶1.29
思路
仔细研究题目,我们能得到一种非【选与不选】的做法:我们将总共要填的格子数统计起来,将这 个空存起来,(假设有 个空)每个格子有 种选法,如果将这 个空挑出来,单独放在一个数组里,这就有了一个类似全排列的解法,仅是排列条件从是否选过变为是否合法,而是否合法依旧能用前面所讲的快速求法进行判定。
#include<bits/stdc++.h>
using namespace std;
int n;
int vis[20][20];
int idx,ans;
struct node
{
int x,y;
}a[250];
bool check(int x,int y,int k)
{
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
if(i == x && vis[i][j] == k)
return 0;
if(j == y && vis[i][j] == k)
return 0;
if(x - y == i - j && vis[i][j] == k)
return 0;
if(x + y == i + j && vis[i][j] == k)
return 0;
}
}
return 1;
}
void dfs(int c)
{
if(c > idx)
{
ans ++;
return ;
}
for(int i = 1;i <= n;i ++)
{
if(check(a[c].x,a[c].y,i))
{
vis[a[c].x][a[c].y] = i;
dfs(c + 1);
vis[a[c].x][a[c].y] = 0;
}
}
}
int main()
{
cin >> n;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
char c;
cin >> c;
if(c != '*')
vis[i][j] = c - '0';
else
{
idx ++;
a[idx] = {i,j};
}
}
}
dfs(1);
cout << ans;
return 0;
}
P7012 [CERC2013] Draughts
思路
此题不难,唯一难点在于对题目的理解与一些细节,重点在于每一次吃子的过程。(包括判断是否能吃) 具体看代码。
#include<bits/stdc++.h>
using namespace std;
int n = 10,m,ans;
char a[15][15];
int vis[15][15];
int dx[] = {-2,2,2,-2};
int dy[] = {2,2,-2,-2};
bool bj(int x,int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= n) return 1;
return 0;
}
void dfs(int x,int y,int step)
{
ans = max(ans,step);
for(int i = 0;i < 4;i ++)
{
int tx = x + dx[i],ty = y + dy[i],ex = x + dx[i] / 2,ey = y + dy[i] / 2;
if(bj(tx,ty))
{
if(vis[tx][ty] == 0 && vis[ex][ey] == 1)
{
vis[ex][ey] = 0;
dfs(tx,ty,step + 1);
vis[ex][ey] = 1;
}
}
}
}
int main()
{
int t;
cin >> t;
while(t --)
{
ans = 0;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
cin >> a[i][j];
if(a[i][j] == 'B')
vis[i][j] = 1;
else if(a[i][j] == 'W')
vis[i][j] = -1;
else vis[i][j] = 0;
}
}
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
if(vis[i][j] == -1)
{
vis[i][j] = 0;
dfs(i,j,0);
vis[i][j] = -1;
}
}
}
cout << ans << "\n";
}
return 0;
}
P1562 还是 N 皇后
思路
因为是暴力,所以没什么好说的,用上先前的标记法直接暴搜。
#include<bits/stdc++.h>
using namespace std;
int n;
bool zhu[30],lie[30],fu[30],vis[30][30];
int a[30],idx;
void dfs(int c)
{
if(c > n)
{
idx ++;
return ;
}
for(int i = 1;i <= n;i ++)
{
if(!vis[c][i] && !lie[i] && !zhu[i + c] && !fu[c - i + n])
{
lie[i] = zhu[i + c] = fu[c - i + n] = 1;
a[c] = i;
dfs(c + 1);
lie[i] = zhu[i + c] = fu[c - i + n] = 0;
a[c] = 0;
}
}
}
int main()
{
cin >> n;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
char x;
cin >> x;
if(x == '.')
{
vis[i][j] = 1;
}
}
}
dfs(1);
cout << idx;
return 0;
}