import java util public class lesson1 public static void main String c

 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.* ;
public class lesson1
{
public static void main(String []cmd)
{
lesson18 l18 = new lesson18();
l18.Run();
}
}
class lesson18
{
public class Position
{
public int x;
public int y;
public Position() { x=0; y=0; }
public Position(Position cp) { x=cp.x; y=cp.y; }
public boolean equals(Object o)
{
Position pos = (Position)o;
return pos.x == x && pos.y == y;
}
};
enum MoveCode
{
Up,
Down,
Left,
Right
};
List<MoveCode> lsMoves = new ArrayList<MoveCode>();
List<Position> visited = new ArrayList<Position>();
Position cur = new Position();
public Boolean LonelyKingMoveLoops(List<MoveCode> moves)
{
visited.add(new Position(cur));
for(int i=0; i < 3; ++i)
{
switch(moves.get(i))
{
case Up:
++cur.y;
break;
case Down:
--cur.y;
break;
case Left:
--cur.x;
break;
case Right:
++cur.x;
break;
}
if(visited.contains(cur)) return true;
visited.add(new Position(cur));
}
return false;
}
public void BuildMoves()
{
lsMoves.add(MoveCode.Up);
lsMoves.add(MoveCode.Left);
lsMoves.add(MoveCode.Down);
lsMoves.add(MoveCode.Right);
}
public void Run()
{
BuildMoves();
if(LonelyKingMoveLoops(lsMoves))
{
System.out.println("побывал");
}
else
{
System.out.println("не побывал");
}
}
}