首页 > 阿里春招笔试3/10
头像
Kopoo
编辑于 2021-03-10 13:29
+ 关注

阿里春招笔试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));
    }


}


全部评论

(4) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐