TOJ1398 bfs混合dfs较慢

本文介绍了一种算法,用于判断一组整数序列是否可以组成一个正方形的四条等边。通过输入一系列整数并进行逻辑判断,该程序能够确定这些数值是否满足构成正方形的条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class IsSquare2 {

static int len;
static int avg;
static boolean[] isV;
static int[] seq;
static int flag;

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(new File("files/issquare"));
    int T = sc.nextInt();
    for (int t = 0; t < T; t++) {
        len = sc.nextInt();
        avg = 0;
        seq = new int[len];
        isV = new boolean[len];
        int max = 0;
        for (int i = 0; i < len; i++) {
            seq[i] = sc.nextInt();
            avg += seq[i];
            if (seq[i] > max)
                max = seq[i];
        }
        if (avg % 4 != 0) {
            System.out.println("no");
        } else if (len == 4) {
            if (seq[0] == seq[1] && seq[0] == seq[2] && seq[0] == seq[3])
                System.out.println("yes");
            else
                System.out.println("no");
        } else {
            avg /= 4;
            if (max > avg)
                System.out.println("no");
            else {
                flag=0;
                BFS(1, 1);
                if (flag == 1)
                    System.out.println("yes");
                else
                    System.out.println("no");
            }
        }
    }

}

private static void BFS(int c, int sn) {
    // TODO Auto-generated method stub
    if (flag == 1)
        return;
    if (c == 5) {
        flag = 1;
        return;
    }
    for (int i = sn; i < len - 2; i++) {
        int[] queue = new int[i];
        dfs(0, -1, i, queue, 0, c);
    }
}

private static void dfs(int s, int pre, int step, int[] q, int sum, int c) {
    // TODO Auto-generated method stub
    if (s == step) {
        if (sum == avg) {
            boolean[] tmpv = new boolean[len];
            CopyOf(tmpv, isV);
            for (int i = 0; i < step; i++) {
                isV[q[i]] = true;
            }
            BFS(c + 1, step);
            CopyOf(isV, tmpv);
        }
        return;
    }
    for (int i = pre + 1; i < len; i++) {
        if (!isV[i]) {
            q[s] = i;
            sum += seq[i];
            dfs(s + 1, i, step, q, sum, c);
            if (flag == 1)
                return;
            sum -= seq[i];
        }
    }
}

private static void CopyOf(boolean[] v1, boolean[] v2) {
    // TODO Auto-generated method stub
    for (int i = 0; i < len; i++) {
        v1[i] = v2[i];
    }
}

}

sample input:
10
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 161
10 15 4 1 11 3 2 8 8 8 20
5 20 30 20 30 24
7 6 2 1 7 3 8 9
6 1 2 10001 9999 10000 10001
20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 30
4 1 1 1 1
9 1 7 2 6 4 4 3 2 3
5 5 5 3 2 5
20 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
sample output:
no
yes
no
yes
yes
yes
yes
yes
yes
yes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值