poj1321——棋盘问题
2010-10-16 14:16
363 查看
深搜问题,需剪枝!
思路:
从g[0][0]开始,统计出包含g[0][0]在内的所有情况,然后,接下来,统计包含g[0][1]但不包含g[0][0]的情况,再统计g[0][2]且排除了g[0][0],g[0][1]的情况数......以此类推!
思路:
从g[0][0]开始,统计出包含g[0][0]在内的所有情况,然后,接下来,统计包含g[0][1]但不包含g[0][0]的情况,再统计g[0][2]且排除了g[0][0],g[0][1]的情况数......以此类推!
#include<stdio.h> #include<string.h> char g[10][10]; bool pp[10][10],tempx[10],tempy[10]; int n,k,totalx,totaly,num,sum; void dfs(int flag) { int i,j,x; x=flag/10; if(num==k) { sum++; return; } if(num+n-1-x<k)//考虑其剩下的行是否还可以满足 return; for(i=x;i<n;i++) for(j=0;j<n;j++) { if(g[i][j]=='#'&&tempx[i]&&tempy[j]) { num++; tempx[i]=false;tempy[j]=false; dfs(i*10+j); num--; tempx[i]=true;tempy[j]=true; } } } int main() { int i,j; while(scanf("%d%d",&n,&k)!=EOF) { if(n==-1&&k==-1) break; memset(tempx,false,sizeof(tempx)); memset(tempy,false,sizeof(tempy)); for(i=0;i<n;i++) { scanf("%s",&g[i]); for(j=0;j<n;j++) if(g[i][j]=='#') { tempx[i]=true; tempy[j]=true; } } for(i=0;i<n;i++) { if(tempx[i]) totalx++; if(tempy[i]) totaly++; } if(totalx<k||totaly<k) { printf("0/n"); } else { sum=0; for(i=0;i<n;i++) for(j=0;j<n;j++) { num=0; memset(tempx,true,sizeof(tempx)); memset(tempy,true,sizeof(tempy)); if(g[i][j]=='#') { num++; tempx[i]=false;tempy[j]=false; dfs(i*10+j); } } printf("%d/n",sum); } } return 0; }
相关文章推荐
- POJ - 1321 棋盘问题(15.10.10 搜索专题)dfs
- POJ 1321棋盘问题(简单搜索)
- POJ 1321 棋盘问题
- POJ 1321 棋盘问题
- POJ 1321 棋盘问题
- 棋盘问题(POJ-1321)
- 棋盘问题 POJ-1321
- POJ 1321棋盘问题
- poj 1321 棋盘问题
- poj 1321 棋盘问题(简单搜索)
- POJ 1321 棋盘问题
- [简单dfs]poj1321棋盘问题
- [POJ](1321)棋盘问题 ---dfs
- poj1321棋盘问题DFS
- POJ 1321棋盘问题(简单搜索dfs回溯)
- poj1321棋盘问题(dfs)
- poj 1321 棋盘问题(n行中放任意k行)
- (简单) POJ 1321 棋盘问题,回溯。
- poj1321棋盘问题(深搜)
- POJ 1321 棋盘问题