您的位置:首页 > 其它

poj1321解题报告

2009-12-22 11:42 344 查看
dfs深搜,不需要剪枝。

http://acm.pku.edu.cn/JudgeOnline/problem?id=1321
package poj1321;

import java.util.Scanner;

public class Main {

private static int n;
private static int k;
private static int ans;
private static int[] column;
private static String[] chessboard;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {
n = sc.nextInt();
k = sc.nextInt();
if (n == -1 && k == -1)
break;
sc.nextLine();
chessboard = new String
;
column = new int
;
for (int i = 0; i < n; i++) {
chessboard[i] = sc.nextLine();
}
ans = 0;
dfs(0, 0);// 从第一行开始
System.out.println(ans);
}
}

private static void dfs(int pos, int i) {

if (pos == k) {
ans++;
return;
}
if (i >= n)
return;
for (int j = 0; j < n; j++) {
char ch = chessboard[i].charAt(j);
if (ch == '#' && column[j] == 0) {
column[j] = 1;
pos++;
dfs(pos, i + 1);
column[j] = 0;
pos--;
}
}
dfs(pos, i + 1);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: