POJ1321 棋盘问题
生活随笔
收集整理的這篇文章主要介紹了
POJ1321 棋盘问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
??????????????????????????????????????????????????????????????????????????????????????? 棋盤問題
| Time Limit: 1000MS | ? | Memory Limit: 10000K |
| Total Submissions: 8895 | ? | Accepted: 4278 |
Description
在一個給定形狀的棋盤(形狀可能是不規(guī)則的)上面擺放棋子,棋子沒有區(qū)別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請編程求解對于給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案C。Input
輸入含有多組測試數(shù)據(jù)。每組數(shù)據(jù)的第一行是兩個正整數(shù),n k,用一個空格隔開,表示了將在一個n*n的矩陣內(nèi)描述棋盤,以及擺放棋子的數(shù)目。 n <= 8 , k <= n
當為-1 -1時表示輸入結(jié)束。
隨后的n行描述了棋盤的形狀:每行有n個字符,其中 # 表示棋盤區(qū)域, . 表示空白區(qū)域(數(shù)據(jù)保證不出現(xiàn)多余的空白行或者空白列)。
Output
對于每一組數(shù)據(jù),給出一行輸出,輸出擺放的方案數(shù)目C (數(shù)據(jù)保證C<2^31)。Sample Input
2 1#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
21
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <string.h> 6 #include <cmath> 7 using namespace std; 8 9 char map[10][10]; 10 int c; 11 12 int check(int n,int i) 13 { 14 int j; 15 for(j=1;j<=n;j++) 16 if(map[j][i]=='0') 17 return 0; 18 return 1; 19 } 20 21 int dfs(int n,int num,int s) 22 { 23 int i,j,k; 24 25 if(num==0) 26 {c++;return 1;} 27 if((n-s+1)<num) 28 return 0; 29 30 31 for(i=s;i<=n;i++) 32 for(j=1;j<=n;j++) 33 { 34 if(map[i][j]!='#') 35 continue; 36 if(!check(i,j)) 37 continue; 38 39 map[i][j]='0'; 40 dfs(n,num-1,i+1); 41 map[i][j]='#'; 42 } 43 44 return 1; 45 } 46 47 48 49 50 int main(int argc, char *argv[]) 51 {//freopen("C:/Users/shp/Desktop/in.txt","r",stdin); 52 //freopen("C:/Users/shp/Desktop/out.txt","w",stdout); 53 int n,k,i,j; 54 while(scanf("%d%d",&n,&k)!=EOF) 55 { 56 if((n==-1)&&(k==-1)) 57 break; 58 getchar(); 59 for(i=1;i<=n;i++) 60 {for(j=1;j<=n;j++) 61 scanf("%c",&map[i][j]); 62 getchar(); 63 } 64 65 c=0; 66 67 dfs(n,k,1); 68 cout<<c<<endl; 69 70 } 71 72 73 74 75 76 77 78 system("PAUSE"); 79 return EXIT_SUCCESS; 80 }
轉(zhuǎn)載于:https://www.cnblogs.com/zjushuiping/archive/2012/05/31/2528169.html
總結(jié)
以上是生活随笔為你收集整理的POJ1321 棋盘问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET系统 + Access数据
- 下一篇: 使用PLSQL导入导出数据库