字節跳動2019/04/14第一題

1.題目描述:記不清了,大致是產品經理用1表示,程序員用2,0表示空。對於一個二維數組,每分鐘程序員可以將上下左右的產品經理變爲程序員,就是將1變爲2;對於0不做變化。輸出爲將所有產品經理變爲程序員需要幾分鐘;如果最後結果還有產品經理無法改變狀態,輸出-1

2.輸入:

1 2 1

1 1 0

0 1 1

輸出:

3 

note:

1 2 1          2 2 2          2 2 2       2 2 2

1 1 0          1 2 0           2 2 0      2 2 0

0 1 1          0 1 1           0 2 1       0 2 2

3.算法:簡單的模擬過程,說是可以用BFS,還沒嘗試。發現大家很容易卡在輸入輸出,我本來輸入處理:


While(true){
if(tmp.equals(""))
    break;
}

一直顯示數組越界,後來改成sc.hasNext()才通過。本機運行,使用ctrl+d中止輸入。 

package example.Tiktok;

import java.util.Scanner;

public class A1 {
    //程序員改變產品經理的過程
    public static boolean change(int[][] data,int i,int j){
        int cnt = 0;//記錄change過程的操作數
        if(i>0){
            if(data[i-1][j] == 1){
                data[i-1][j] = 3;//變爲3的原因是:如果變爲2,在本輪循環就會處理這些2,這肯定是不對的
                cnt++;
            }
        }
        if(j>0){
            if(data[i][j-1] == 1){
                data[i][j-1] =3;
                cnt++;
            }
        }
        if(j>=0 && j<data[0].length-1) {
            if (data[i][j + 1] == 1){
                data[i][j+1] = 3;
                cnt++;
            }
        }
        if(i>=0 && i<data.length-1 ){
            if(data[i+1][j] == 1){
                data[i+1][j] =3;
                cnt++;
            }
        }
        if(cnt > 0)
            return true;
        else
            return false;

    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = 0;
        String s = "";
        //輸入
        while (sc.hasNext()){
            String tmp = sc.nextLine();
            rows++;
            s  = s + ","+tmp;
        }
        int time = 0;//最後結果:第幾分鐘完成了所有的可變產品經理變成程序員
        String[] str = s.substring(1,s.length()).split(",");
        for(int i=0;i<str.length;i++){
            str[i] = str[i].replace(" ","");
        }
        int cols = str[0].length();
        int[][] data = new int[rows][cols];
        for(int i=0;i<str.length;i++){
            for(int j=0;j<str[i].length();j++){
                data[i][j] = str[i].charAt(j)-'0';
            }
        }

        while (true){
            boolean flag = false;//每一輪變化change過程是否有具體改變狀態
            int cnt = 0;//記錄每輪變化後1的個數,要是爲0,沒必要進行下一輪
            for(int i=0;i<rows;i++){
                for(int j=0;j<cols;j++){
                    if(data[i][j] == 2){
                        if(change(data,i,j)){
                            flag = true;//有一個變動 就可以繼續走下去
                        }
                    }

                }
            }
            for(int i=0;i<rows;i++) {
                for (int j = 0; j < cols; j++) {
                    if(data[i][j] == 3){
                        data[i][j] =2;//把原來的3再改回2
                    }
                    if(data[i][j] == 1)
                        cnt++;
                }
            }
            time++;
            //一輪結束
            if(flag == false){
                time = -1;
                break;
            }
            if(cnt == 0){
                //一輪結束沒有1
                break;
            }



        }
        System.out.println(time);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章