poj 1321 棋盘问题
题目链接:http://poj.org/problem?id=1321
题意: n*n的棋盘,放k个棋子,#可以放棋子 .不可以放棋子 每行每列都只能有一个棋子,求方案数
分类:DFS
注意点:2的31次方是2147483648,是超过int范围的
代码:
///#include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<string.h> using namespace std; #define LL __int64 #define PI 3.1415926535898 const LL inf=1000000007; int n,k; char ch[10][10]; bool hang[10]; bool lie[10]; int sum_k,temp_ans; LL ans; void dfs(int x,int temp_k) { if(temp_k==k) { ans++; } for(int i=x;i<=n;i++) { for(int j=1;j<=n;j++) { if(ch[i][j]=='.') continue; if(hang[i]==1||lie[j]==1) continue; if(ch[i][j]=='#') { hang[i]=1; lie[j]=1; temp_k++; dfs(i+1,temp_k); temp_k--; hang[i]=0; lie[j]=0; ch[i][j]='#'; } } } } int main() { while(scanf("%d %d",&n,&k)&&!(n==-1&&k==-1)) { ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>ch[i][j]; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { memset(hang,0,sizeof(hang)); memset(lie,0,sizeof(lie)); int sumk=0; if(ch[i][j]=='#') { ch[i][j]='.'; hang[i]=1; lie[j]=1; sumk++; dfs(i+1,sumk); } } } cout<<ans<<endl; } return 0; }
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步