Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

注意是‘#'可以放棋子

Posted by sshaohua at 2017-03-20 22:18:02 on Problem 1321
dfs、贴个代码:
#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;
}
/*
8 8
########
########
########
########
########
########
########
########
---------
40320
*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator