-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiltrator.java
More file actions
65 lines (62 loc) · 1.73 KB
/
Infiltrator.java
File metadata and controls
65 lines (62 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
public class Infiltrator {
//cur = current position
//function which decide whether to move or not
static boolean next_move(int cur, double p, int w){
/*
s0,s1,s2,s3 represent prensent sensor and
forward three adjacent sensors
*/
Sensor s0,s1,s2,s3;
//if outside the border
if(cur==0) {
s0 = new Sensor(0);//no sensor
s1 = new Sensor(p);s1.set_state();//find state on/off
s2 = new Sensor(p);s2.set_state();
s3 = new Sensor(p);s3.set_state();
}
//if in last row
else if(cur==w) {
s0 = new Sensor(p);s0.set_state();//find state on/off
s1 = new Sensor(0);//no sensor
s2 = new Sensor(0);
s3= new Sensor(0);
}
//if somewhere in the middle
else {
s0 = new Sensor(p);s0.set_state();//find state on/off
s1 = new Sensor(p);s1.set_state();
s2 = new Sensor(p);s2.set_state();
s3 = new Sensor(p);s3.set_state();
}
//if any one of s1,s2 or s3 is off and s0 is move forward
if(s0.get_state()+s1.get_state() == 0) {
return true;
}
if(s0.get_state()+s2.get_state() == 0) {
return true;
}
if(s0.get_state()+s3.get_state() == 0) {
return true;
}
//else stand still
return false;
}
public static void main(String[] args) {
// current is present position initially outside
int current = 0;
int time =0;
double prob = Double.parseDouble(args[0]);//probability of head
int width = Integer.parseInt(args[1]);//width of border
Border b = new Border(width);//create border of given width
//while not suceed to cross figure out next step
while(!b.check_success(current)) {
//if favourable to move
if(next_move(current,prob,width)) {
current++;//increse current position
}
time+=10;//increment time
}
System.out.println("time taken= "+ time);
}
}