阿里春招笔试3/10
用dfs做了一下,感觉很麻烦,尤其是边界的条件也不优雅
给出无情for循环的解法
这就很容易了😂
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] temp = bf.readLine().split(" ");
int m = Integer.parseInt(temp[0]);
int n = Integer.parseInt(temp[1]);
int k = Integer.parseInt(temp[2]);
char[][] arr = new char[m][n];
for (int i = 0; i < m; i++) {
String str = bf.readLine();
for (int j = 0; j < n; j++) {
arr[i][j] = str.charAt(j);
}
}
String[] dir = new String[k];
for (int i = 0; i < k; i++) {
dir[i] = bf.readLine();
}
int start_x=0, start_y=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == '@') {
start_x = i;
start_y = j;
break;
}
}
}
for(int i=0;i<k;i++){
//System.out.println(dir[i]+" "+start_x+start_y);
if(dir[i].equals("EAST")){
while(start_y+1<n&&arr[start_x][start_y+1]!='#')start_y++;
}
else if(dir[i].equals("SOUTH")){
while(start_x+1<m&&arr[start_x+1][start_y]!='#')start_x++;
}
else if(dir[i].equals("WEST")){
while(start_y-1>=0&&arr[start_x][start_y-1]!='#')start_y--;
}
else if(dir[i].equals("NORTH")){
while(start_x-1>=0&&arr[start_x-1][start_y]!='#')start_x--;
}
}
System.out.println((start_x+1)+" "+(start_y+1));
}
} 

海康威视公司福利 1262人发布