import java.util.HashSet;
public class Contest_7 {
public static boolean find(int[] path){
int cal[]={1,-1,5,-5};
HashSet<Integer> chain=new HashSet<Integer>();
HashSet<Integer> bag=new HashSet<Integer>();
chain.add(path[0]);
for(int i=1;i<5;i++)
bag.add(path[i]);
while(!bag.isEmpty()){
boolean marked=false;
for(int ball:bag){
for(int plus:cal){
int hand=ball+plus;
if(hand<0||hand>14||hand==10||hand==5)
continue;
if(chain.contains(hand)) {chain.add(ball);bag.remove(ball);marked=true;break;}
}
if(marked==true)
break;
}
if(marked==false)
return false;
}
return true;
}
public static void main(String[] args) {
int count=0;
int arr[]={1,2,3,4,6,7,8,9,11,12,13,14};
for(int a=0;a<8;a++)
for(int b=a+1;b<9;b++)
for(int c=b+1;c<10;c++)
for(int d=c+1;d<11;d++)
for(int e=d+1;e<12;e++){
int path[]={arr[a],arr[b],arr[c],arr[d],arr[e]};
if(find(path))
count++;
}
System.out.println(count);
}
}
虽然我是第一次参加这个比赛,但是还是说一下我做去年题目的一些体会吧
这个问题我研究了好几天,最后一次做对的时候我看自己的答案错了,我还在想是哪里错了,结果其实没有任何问题,只是运行的按钮不知道为什么没按下去,显示的是上次错误的结果.为这还郁闷了半天
看了不少说这题目的,但是不是用Java写的,我虽然是用java写的,但是效果差不多,用了一个HashSet集合
思路:1.把所有元素进行组合2.对所有进行组合过后的路径进行find()方法检测,看是否连通,连通就count++3.find方法(把第一个元素看作一个起始点,剩下其他元素要是与第一个元素相连就加上,所有的剩下的元素都不相连就return false)
关于部分思路参考了点击打开链接
点击链接就能看到原题了,他把数字变了,我也把数字变了
但是我没看懂他写的,我写的应该好懂一点