-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
116 lines (100 loc) · 3.03 KB
/
code.java
File metadata and controls
116 lines (100 loc) · 3.03 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static void next_move(int posr, int posc, String[] board){
//add logic here
int mindirtdist=4;
int mindirtR=-1;
int mindirtC=-1;
int n=board.length;
String nextMove="";
if(board[posr].charAt(posc)=='d')
{
nextMove="CLEAN";
}
else
{
for(int i=-1;i<=1;i++) // loop to find the dirt in the visible region
{
if((posr+i)<0 || (posr+i)>=n)
continue;
for(int j=-1;j<=1;j++)
{
if((posc+j)<0 || (posc+j)>=n)
continue;
if(board[posr+i].charAt(posc+j)=='d')
{
int dirtR=posr+i;
int dirtC=posc+j;
int dirtdistR=posr-dirtR;
int dirtdistC=posc-dirtC;
if(dirtdistR<0)
dirtdistR*=-1;
if(dirtdistC<0)
dirtdistC*=-1;
int dirtdist=dirtdistR+dirtdistC;
if(dirtdist<mindirtdist)
{
mindirtdist=dirtdist;
mindirtR=posr+i;
mindirtC=posc+j;
}
}
}
} // loop ends
if(mindirtR!=-1 && mindirtC!=-1) // logic if the dirt is found in the visible region
{
int dirtdistR=posr-mindirtR;
int dirtdistC=posc-mindirtC;
if(dirtdistC!=0)
{
if(dirtdistC>0)
nextMove="LEFT";
else
nextMove="RIGHT";
}
else
{
if(dirtdistR!=0)
{
if(dirtdistR>0)
nextMove="UP";
else
nextMove="DOWN";
}
}
}
else // this will move the bot in a definite manner if the dirt is not found within the visible region
{
if(posc!=n-1 || posr!=n-1)
{
if(posr==n-1 && posc%2==0)
{
nextMove="RIGHT";
}
else if(posr==0 && posc%2==1)
nextMove="RIGHT";
else
{
if(posc%2==0)
nextMove="DOWN";
else
nextMove="UP";
}
}
}
}
System.out.println(nextMove);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [] pos = new int[2];
String board[] = new String[5];
for(int i=0;i<2;i++) pos[i] = in.nextInt();
for(int i=0;i<5;i++) board[i] = in.next();
next_move(pos[0], pos[1], board);
}
}