1.29-寒假集训-5【深度优先搜索】
深度优先搜索(DFS)
-P2105 K皇后-
思路
本题实现参照八皇后
- 在标注的时,二维目标内判断对角线,行,列速度较慢,所以转换成一维的形式进行处理,但是时间复杂度也还是
O(n\times m) - 所以我们可以添加一个剪枝:
- 如果当前行已经填入了皇后,这一行就没有点可以作为答案
Code
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e5+7; int hang[N],lie[N],zhu[N],ci[N]; int n,m,k,ans; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n>>m>>k; for(int i=1,x,y;i<=k;i++) { cin>>x>>y; hang[x]=1; lie[y]=1; zhu[n+x-y]=1; ci[x+y]=1; } for(int i=1;i<=n;i++) { if(!hang[i]) { for(int j=1;j<=m;j++) { if(!lie[j] && !zhu[n+i-j] && !ci[i+j]) { ans++; } } } } cout<<ans; return 0; }
- 如果当前行已经填入了皇后,这一行就没有点可以作为答案
## [-T726810 搜索进阶1.29-](https://www.luogu.com.cn/problem/T726810?contestId=306336)
### 思路
- 首先将所有的非数字提前存储起来,然后进行一维 $DFS$ 并考虑每个点填的数字是否会影响行,列和对角线
### Code
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=10+10;
struct node
{
int x,y;
}path[N*N];
int n,cnt=0,ans=0;
char c[N][N];
bool check(int x,int y,char k)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==x && c[i][j]==k)
{
return false;
}
if(j==y && c[i][j]==k)
{
return false;
}
if(i-j==x-y && c[i][j]==k)
{
return false;
}
if(i+j==x+y && c[i][j]==k)
{
return false;
}
}
}
return true;
}
void dfs(int cur)
{
if(cur==cnt+1)
{
ans++;
return;
}
for(int i=1;i<=n;i++)
{
if(check(path[cur].x,path[cur].y,i+'0'))
{
c[path[cur].x][path[cur].y]=i+'0';
dfs(cur+1);
c[path[cur].x][path[cur].y]='*';
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>c[i][j];
if(c[i][j]=='*')
{
path[++cnt]={i,j};
}
}
}
dfs(1);
cout<<ans;
return 0;
}
-P1219 [USACO1.5] 八皇后 Checker Challenge-
思路
- 模板题(1/1)
- 列约束:
lie_i 标记第i 列是否已有棋子,保证每列唯一 - 主,副对角线约束:
zhu_{n+cur-i} 标记主对角线(斜率为1 ),保证每条主对角线最多一个棋子;ci_{i+cur} 标记副对角线(斜率为-1 ),并保证每条副对角线最多一个棋子 - 尝试在第
cur 行的每一列i 放置棋子,若满足约束条件则标记状态,记录列号并进入下一行;递归返回时回溯,并尝试下一个可能的列 - 最后输出答案即可
Code
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e3+10; int n,lie[N],zhu[N],ci[N],ans[N],cnt=0; void dfs(int cur) { if(cur>n) { if(cnt<3) { for(int i=1;i<=n;i++) { cout<<ans[i]<<" "; } cout<<"\n"; } cnt++; return ; } for(int i=1;i<=n;i++) { if(!lie[i] && !zhu[n+cur-i] && !ci[i+cur]) { lie[i]=1; zhu[n+cur-i]=1; ci[i+cur]=1; ans[cur]=i; dfs(cur+1); lie[i]=0; zhu[n+cur-i]=0; ci[i+cur]=0; } } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n; dfs(1); cout<<cnt; return 0; }-P7012 [CERC2013] Draughts-
思路
- 首先用
vis 数组标记棋盘状态,0 为空位,-1 为黑棋,1 为白棋(起点) - 从每个白棋起点出发,按
4 个对角线跳步方向递归;每次跳步需满足:落点在棋盘内,未访问,中间格是黑棋(vis_i==-1) - 遍历所有白棋起点后,输出最大吃子数即可
Code
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e2+10; int dx[4]={-2,2,2,-2}; int dy[4]={2,2,-2,-2}; int T,ans,vis[N][N]; char c[N][N]; void dfs(int x,int y,int step) { ans=max(ans,step); for(int i=0;i<4;i++) { int wx=x+dx[i];//白棋坐标 int wy=y+dy[i]; int hx=x+dx[i]/2;//黑棋坐标(因为判断黑棋是中间格所以除以2) int hy=y+dy[i]/2; if(wx>=1 && wx<=10 && wy>=1 && wy<=10 && vis[wx][wy]==0 && vis[hx][hy]==-1) { vis[hx][hy]=0; dfs(wx,wy,step+1); vis[hx][hy]=-1; } } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>T; while(T--) { ans=0; for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { cin>>c[i][j]; if(c[i][j]=='.' || c[i][j]=='#') { vis[i][j]=0; } else if(c[i][j]=='B') { vis[i][j]=-1; } else { vis[i][j]=1; } } } for(int i=1;i<=10;i++) { for(int j=1;j<=10;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 皇后-
思路
-
-
#### 位运算约束判断 - 列约束:
lie 掩码标记已占用列,对应位为1 表示列已被占 主对角线约束:zhu 掩码右移1 位,对应下一行主对角线的占用状态 副对角线约束:fu 掩码左移1 位,对应下一行副对角线的占用状态 合法位置:tmp = status & ~(vis[cur] | lie | zhu | fu),可得到当前行可放皇后的列位掩码DFS - 按行
(cur) 递归,用lowbit 函数提取tmp 中可放的列位 - 递归下一行时,更新列,主副对角线的掩码状态
- 当
cur>n 时,说明找到合法方案,答案计数+1 Code
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e7+10; int n,ans,status,vis[N]; int lowbit(int x) { return x & (-x); } void dfs(int cur,int lie,int zhu,int fu) { if(cur==n+1) { ans++; return; } int tmp=status & (~(vis[cur] | lie | zhu | fu)); for(int i=tmp;i;i-=lowbit(i)) { int x=lowbit(i); dfs(cur+1,lie+x,(zhu+x)>>1,(fu+x)<<1); } } signed main() { cin>>n; status=(1<<n)-1; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { char c; cin>>c; if(c=='.') { vis[i]|=(1<<(j-1)); } } } dfs(1,0,0,0); cout<<ans; return 0; }TMD写死我了