BFS都能写错!!!

//error
import java.util.*;

public class Solution {


    public static void solve(char[][] board) {

        if (board.length == 0) {
            return;
        }




        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (i == 0 || i == board.length - 1 || j == 0 || j == board[0].length - 1) {

                    if (board[i][j] == 'O') {


                        List<Integer> list = new ArrayList<>(2);
                        list.add(i);
                        list.add(j);


                        BFS(list, board);


                    }


                }
            }
        }


        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {


                if (board[i][j] == '*') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }


            }
        }


    }

    public static int aX[] = {0, 0, -1, 1};
    public static int aY[] = {1, -1, 0, 0};



    public static void BFS(List<Integer> list, char[][] board) {
        Queue<List<Integer>> queue = new LinkedList();
        queue.add(list);
        while (!queue.isEmpty()) {

            List<Integer> listTemp = queue.poll();

            int i = listTemp.get(0);
            int j = listTemp.get(1);


            board[i][j] = '*';


            for (int q = 0; q < 4; q++) {

                int tempX = i + aX[q];
                int tempY = j + aY[q];

                //System.out.println(tempX+"&"+tempY);

                if (tempX < 0 || tempX > board.length - 1 || tempY < 0 || tempY > board[0].length - 1) {

                    continue;
                }


                if (board[tempX][tempY] == 'O') {
                    List<Integer> tempList = new ArrayList(2);
                    tempList.add(tempX);
                    tempList.add(tempY);
                    queue.offer(tempList);

                }


            }
        }
    }


}

//correct
//在加入队列之前visited=true
import java.util.*;

public class Solution {


    public static void solve(char[][] board) {

        if (board.length == 0) {
            return;
        }








        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (i == 0 || i == board.length - 1 || j == 0 || j == board[0].length - 1) {

                    if(board[i][j]=='O')
                    {
                        List<Integer> list = new ArrayList<>(2);
                        list.add(i);
                        list.add(j);
                        board[i][j] = '*';

                        bfs(list,board);

                    }


                }
            }
        }




        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {


                if (board[i][j] == '*') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }


            }
        }





    }

    public static int aX[] = {0, 0, -1, 1};
    public static int aY[] = {1, -1, 0, 0};







    public  static void bfs(List<Integer> list,char [][]board)
    {
        Queue<List<Integer>> queue = new LinkedList();
        queue.add(list);
        while (!queue.isEmpty()) {

            List<Integer> listTemp = queue.poll();

            int tempI = listTemp.get(0);
            int tempJ = listTemp.get(1);


            for (int q = 0; q < 4; q++) {

                int tempX = tempI + aX[q];
                int tempY = tempJ + aY[q];

                //System.out.println(tempX+"&"+tempY);

                if (tempX < 0 || tempX > board.length - 1 || tempY < 0 || tempY > board[0].length - 1) {

                    continue;
                }


                if (board[tempX][tempY] == 'O') {
                    board[tempX][tempY] = '*';

                    List<Integer> tempList = new ArrayList(2);
                    tempList.add(tempX);
                    tempList.add(tempY);
                    queue.offer(tempList);

                }


            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值