Create 1321_sshaohua.cpp

From http://poj.org/showmessage?message_id=353467
pull/45/head
Kirigaya Kazuto 2017-07-21 21:12:54 +08:00 committed by GitHub
parent 7d3fcd085a
commit 23dfdac7bb
1 changed files with 47 additions and 0 deletions

47
POJ/1321_sshaohua.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
//notice the '#' can be placed.
using namespace std;
const int MAXN = 10;
int n, m;
int ans = 0;
char my_map[MAXN][MAXN];
bool vis_row[MAXN]; //vis_row[i], i row is visited.
bool vis_col[MAXN]; //vis_col[j], j col is visited.
bool judge(int x, int y){
if(x<0 || x>=n || y<0 || y>=n || my_map[x][y]!='#') return false;
if(vis_row[x] || vis_col[y]) return false;
return true;
}
void DFS(int x, int y, int step){
//int xx, yy;
if(step == m) {ans++; return;}
for(int i=x; i<n; i++)
for(int j=0; j<n; j++){
if(judge(i, j)){
vis_row[i] = vis_col[j] = true;
DFS(i, j, step+1);
vis_row[i] = vis_col[j] = false;
}
}
}
int main(){
while(scanf("%d%d", &n, &m) && n!=-1 && m!=-1){
ans = 0;
memset(vis_row, false, sizeof(vis_row));
memset(vis_col, false, sizeof(vis_col));
for(int i=0; i<n; i++) scanf("%s", my_map[i]);
DFS(0, 0, 0);
printf("%d\n", ans);
}
return 0;
}