TOJ1398 bfs混合dfs较慢
来源:互联网 发布:单片机系统可靠性 编辑:程序博客网 时间:2024/06/16 14:26
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
0 0
- TOJ1398 bfs混合dfs较慢
- poj3083(dfs/bfs混合)
- BFS-DFS
- BFS DFS
- DFS+BFS
- BFS DFS
- DFS BFS
- DFS & BFS
- BFS,DFS
- dfs bfs
- BFS&DFS
- DFS BFS
- BFS && DFS
- BFS DFS
- DFS & BFS
- DFS&BFS
- BFS&DFS
- dfs && bfs
- 基于webView的ZSSRichTextEditor源代码
- SpringMVC实现文件下载的两种方法
- [51nod 1304]字符串的相似度
- 微信公众号的三大分类及功能
- 官方示例 InfraredBasics-D2D的学习
- TOJ1398 bfs混合dfs较慢
- Android gc垃圾回收研究学习
- jeecms怎么修改后台访问路径?
- sap批量创建盘点凭证以及盘点凭证过账
- bootstrap中input添加.form-control类有何作用?
- i2c 驱动四:sysfs文件系统
- Docker学习笔记八:Swarm
- StopWatch计时器(java小工具)
- base64编码详解