我已经完成了计算机科学课的作业。我已经考虑了两个多星期的答案而现在没有成功,所以我决定在这里尝试一下。
该游戏由4名中央情报局特工组成,使用4个相邻的电话亭。他们按照一定的规则一次一个地到达一组展位:
1 - 代理商选择一个展位,其两个相邻的展位未被占用(包括展位边缘的展位);
2 - 如果不符合上述条件,代理人选择只有一个相邻展位未占用的展位;和
3 - 如果两个条件都失败,代理商会选择任何展位。
根据规则,有4种不同的方式可以占用4个代理商和4个展位。例如:代理#1占据第一个展位(左边一个),然后代理#2占据第三个展位(从左到右),然后代理#3占据第四个展位,最后代理#4占据第二个展位。
根据这些规则,我们被要求编写一个代码,用于计算19个代理商占据19个展位的方式数量。
我对此问题的自然选择是创建一个布尔数组,其中false表示空置的展位,而true表示占用的展位:
public class AgentGame {
private boolean[] booths;
private int numberOfBooths, counter;
public AgentGame(int nb) {
counter = 0;
numberOfBooths = nb;
booths = new boolean[numberOfBooths];
}
我一直试图想办法实施两种方法,一种是根据规则填写展位,另一种是计算摊位可以占用的所有不同组合。但我还没有弄清楚,还没有。这里有什么想法?提前谢谢。
答案 0 :(得分:0)
有点低效,但我希望明确的解决方案。我无法保证这会输出正确的答案(因为我喜欢犯愚蠢的错误),但这应该会让你知道如何解决它。
package whyNotZoidberg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Agents {
public static final int N = 5; // amount of total agents / booths
static int position[] = new int[N]; // index = agent, value = boot
static boolean occupied[] = new boolean[N];
static ArrayList<Integer> answers = new ArrayList<>();
public static void main(String[] args) {
for (int i = 0; i < N; i++) {
position[i] = -1;
}
System.out.println("Solutions: ");
int answer = getWays(0);
System.out.println();
System.out.println("The answer is ");
System.out.println(answer);
System.out.println();
Collections.sort(answers,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) {
return 1;
}
if (o2 > o1) {
return -1;
}
System.out.println("Repetition between answers detected. Quiting.");
System.exit(0);
return 0;
}
});
System.out.println("The solutions are following (the number is agent number)");
for (int i = 0; i < answers.size(); i++) {
System.out.println(answers.get(i));
}
}
static int getWays(int agentNr) {
if (agentNr == N-1) {
position[N-1] = viableBootOptions().get(0);
int number = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (position[j] == i) {
number += (int) ((j+1) * Math.pow(10, N - i - 1));
}
}
}
answers.add(number);
return 1;
}
int result = 0;
ArrayList<Integer> options = viableBootOptions();
for (int i = 0; i < options.size(); i++) {
position[agentNr] = options.get(i);
for (int j = agentNr + 1; j < N; j++) {
position[j] = -1;
}
updateOccupied();
result += getWays (agentNr + 1);
}
return result;
}
static ArrayList<Integer> viableBootOptions() {
int ruleSelected = 3;
ArrayList<Integer> result = new ArrayList<>();
if (!occupied[0] && !occupied[1]) {
ruleSelected = 1;
result.add(0);
}
if (!occupied[N-2] && !occupied[N-1]) {
ruleSelected = 1;
result.add(N-1);
}
if (!occupied[0]) {
if (ruleSelected != 1) {
ruleSelected = 2;
result.add(0);
}
}
if (!occupied[N-1]) {
if (ruleSelected != 1) {
ruleSelected = 2;
result.add(N-1);
}
}
for (int i = 1; i < N-1; i++) {
if (!occupied[i]) {
if (!occupied[i - 1] && !occupied[i + 1]) {
if (ruleSelected > 1) {
ruleSelected = 1;
result.clear();
}
result.add(i);
continue;
}
if ((!occupied[i - 1] || !occupied[i + 1]) && ruleSelected != 1) {
if (ruleSelected > 1) {
if (ruleSelected == 3) {
result.clear();
}
ruleSelected = 2;
result.add(i);
continue;
}
}
if (ruleSelected == 3) {
result.add(i);
}
}
}
return result;
}
static void updateOccupied() {
for (int i = 0; i < N; i++) {
occupied[i] = false;
}
for (int i = 0; i < N; i++) {
if (position[i] > -1) {
occupied[position[i]] = true;
}
}
}
}
编辑:程序现在还会打印找到的所有答案,并在任何答案重复时终止。您可以通过更改N的值来更改代理数量。
答案 1 :(得分:0)
我将制作这个快速的单一代码(适用于任何代码,如果你知道该怎么做)
Int x,z=1
Int numberofagents;
Int numberofbooths;
Int emptynext; //this value allows you to control how many empty booths need to be next to one in order for an agent to pic
Func sortagents (numberofagents,numberofbooths,emptynext) {
If(booth[emptybooth].numbertotherightt>=emptyadj||booth[emptybooth].numbertotheleft>=emptyadj,z+1)
Return (agent[z].boothnumber=emptybooth);//this statement will put agents in booths with 2 empty adjacent ones first If(booth[emptybooth].numbertotheright>=1||booth[emptybooth].numbertotheleft>=1,z+1)
Return (agent[z].boothnumber=emptybooth);//that one will place them into booths with one empty adjacent};
Int randomnumber=numberofbooths/2 //I forgot the function for rounding but round up
Int Boothstaken;
Int Boothstaken=0;
Class Booth[numberofbooths]{
Bool Occupied;
Int Unoccupiedonright;
Int Unoccupiedonleft;
}
Class Agent[numberofagents]{
Int boothnumber;
}
Func Boothsoccupied (x=1)
If (booth[x].y=true&&x<Numberofbooths,x+1)
{int Boothstaken+1
};
Func BoothPick //Agent picks booth
If(Boothstaken=0,z+1)
{agent[z].boothnumber=randomnumber}
If(Boothstaken>0)
{Func sortagents};
我真的希望这会有所帮助这只会给你每个人去哪里找到某人去的地方简单 代理[你想要的号码] .Boothnumber 将其更改为Java并输出它是您的工作 我将在一分钟后回来,并告诉你功能,找出左边或右边有多少空位需要吃饭,所以我会在一分钟后回来
Hopes this helps :) Good Luck :)