1002 Fire Net

好久没有做ACM的题目了,今天用Java来回顾一下,花了1个多小时做了ZJU的1002 Fire Net

 

用了类似八皇后问题的回溯。

现在已经通过。

 

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2

 

 

2011-02-26 12:40:51Accepted1002Java

 

另求高手指教

 

package acm.be.persist;

import java.util.Scanner;
/*
 * FireNet 1002
 * 2011/2/26
 * Jiang
 */

public class Main {

	public int result = 0, n;
	public char[] tem[];
	
	public void put(int x, int y, int count) {
		if (x == n-1 && y == n-1) {
			if(count > result){
				result = count;
			}
			return;
		}
		if (y == n-1) {
			y = -1;
			x = x + 1;
		}
		if(tem[x][y+1] == '.' && judge(x, y+1)){
			tem[x][y+1] = ' ';
			put(x, y+1, count + 1);
			tem[x][y+1] = '.';
		}
		put(x, y+1, count);		
	}

	public boolean judge(int x, int y) {
		for (int i = x - 1; i >= 0; i--) {
			if (tem[i][y] == 'X') {
				break;
			}
			if (tem[i][y] == ' ')
				return false;
		}
		for (int i = y - 1; i >= 0; i--) {
			if (tem[x][i] == 'X')
				break;
			if (tem[x][i] == ' ')
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main fireNet = new Main();
		fireNet.n = Integer.parseInt(sc.next());
		while(fireNet.n != 0){
			fireNet.tem = new char[fireNet.n][fireNet.n];
			for (int i = 0; i < fireNet.n; i++) {
				fireNet.tem[i] = sc.next().toCharArray();
			}
			for (int i = 0; i < fireNet.n; i++) {
				for (int j = 0; j < fireNet.n; j++) {
					if (fireNet.tem[i][j] == '.') {
						fireNet.tem[i][j] = ' ';
						fireNet.put(i, j, 1);
						fireNet.tem[i][j] = '.';
					}
				}
			}
			System.out.println(fireNet.result);
			fireNet.result = 0;
			fireNet.n = Integer.parseInt(sc.next());
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值