3

EDIT: I think I may have to implement a BFS in the run() function?

EDIT2: I have updated the code to what I have now. I'm not comfortable with it, but it seems to be adding asterisks to the power conductors successfully. I have to output the total houses and how many are without power, but I don't know how to keep track of that amount properly.

EDIT3: Ok, I am completely convinced that I have the filling of the grid correct. However, I cannot manage to keep track of the amount of houses that aren't powered. Any advice on that would be great.

Here's the idea:

I'm given an h x w matrix full of symbols. One of them will be the letter "P" which will represent a power plant. From that power plant, any capital letter is conductive and can carry the electricity across the map. On top of that, there are 3 symbols that represent wires to also carry electricity. They are: +, -, and |

Finally, there are homes represented by the letter "H". These are conductive as well if next to any of the other conductive symbols. The idea is to fill the grid with an asterisk "*" if power can reach it. The catch is that each power plant P can only power 30 homes H. The code I'm posting is obviously unfinished, but the breadth-first-fill algorithm (bff) is finished. I can successfully fill any immediate symbol next to the P, but I am having trouble figuring out how to continue to trace away from the P with different symbols. Any ideas are welcome.

Example input:

15 22
, , , , , , , , , , . . . , , , , , , , , ,
, , . , , , , , , H H H - - + , , , , , , ,
, , , , , , , , , H H H . . | . , , , . , ,
, , , , , , , , , H H H = , | . , , , . . .
, , , , , , , , , H H H = , | . , , , . . .
, , , . . . . . , H H H = , | , , , , . . .
. . . . . . . . , H H H = , | , , , , . . .
, , . , , = = = = = = = = , | , , , . . . .
, , X X X X , . . . . C C C C C C . . . . .
. . X P X X , . . . . C C C C C C . . . . .
. . X X X X - - - - - C C C C C C . . . . .
. ~ X X X X . . . . . . . . . . . . . . . .
~ ~ ~ ~ . . . . . . . . . . . . . . . . . .
~ ~ ~ ~ ~ . . . . . . . . . . . . . . . . ~
~ ~ ~ ~ ~ ~ . . . . . . . . . . . ~ ~ . ~ ~

Example output:

0 of 18 homes are without power .
, , , , , , , , , , . . . , , , , , , , , ,
, , . , , , , , , * * * * * * , , , , , , ,
, , , , , , , , , * * * . . * . , , , . , ,
, , , , , , , , , * * * = , * . , , , . . .
, , , , , , , , , * * * = , * . , , , . . .
, , , . . . . . , * * * = , * , , , , . . .
. . . . . . . . , * * * = , * , , , , . . .
, , . , , = = = = = = = = , * , , , . . . .
, , * * * * , . . . . * * * * * * . . . . .
. . * * * * , . . . . * * * * * * . . . . .
. . * * * * * * * * * * * * * * * . . . . .
. ~ * * * * . . . . . . . . . . . . . . . .
~ ~ ~ ~ . . . . . . . . . . . . . . . . . .
~ ~ ~ ~ ~ . . . . . . . . . . . . . . . . ~
~ ~ ~ ~ ~ ~ . . . . . . . . . . . ~ ~ . ~ ~

Code so far:

public class PowerGrid {
    String[][] grid = new String[120][120];
    LinkedList<Pair> q = new LinkedList<Pair>();
    LinkedList<Pair> visited = new LinkedList<Pair>();
    Map<Pair, Integer> dist = new HashMap<Pair, Integer>();
    ArrayList<Pair> pwrPlants = new ArrayList<Pair>();
    String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};  
    int row = -1;
    int col = -1;

    int homesToPwr = 0;
    int homesNotPwrd = 0;
    int totalHomes = 0;

    public static void main (String[] args) {
        PowerGrid pg = new PowerGrid();
        pg. run();

    }

    public void run() {
        Scanner sc = new Scanner(System.in);

        row = sc.nextInt();
        col = sc.nextInt();

        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++) {
                grid[i][j] = sc.next();
        }

        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++) {
                if (grid[i][j].equals("P")) {
                    Pair tmpPair = new Pair(i, j);
                    pwrPlants.add(tmpPair);
                }
            }

        for (Pair p : pwrPlants) {
            homesToPwr += 30;
            for (String s : alphabet) {
                if (s == "H") 
                    if (homesToPwr != 0) 
                        homesToPwr--;                       

                bff(grid, p.x, p.y, s, "*");
            }

            bff(grid, p.x, p.y, "-", "*");
            bff(grid, p.x, p.y, "+", "*");
            bff(grid, p.x, p.y, "|", "*");
        }

        System.out.println(homesToPwr + " of " + totalHomes + " are without power.");

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }
    }

    public void bff (String[][] grid, int x, int y, String oldSymbol, String newSymbol) {
        if (oldSymbol == newSymbol) return;


        Pair pair = new Pair(x,y);
        q.addLast(pair);
        dist.put(pair, 0);
        grid[x][y] = newSymbol;

        while (!q.isEmpty()) {
            Pair v = q.getFirst();
            q.pop();
            int d = dist.get(v) + 1;
            String[] symbols = {"+", "-", "|"};

            visit(grid, v.x + 1, v.y, new Pair(v.x+1, v.y), oldSymbol, newSymbol, d);
            visit(grid, v.x - 1, v.y, new Pair(v.x-1, v.y), oldSymbol, newSymbol, d);
            visit(grid, v.x, v.y + 1, new Pair(v.x, v.y+1), oldSymbol, newSymbol, d);
            visit(grid, v.x, v.y - 1, new Pair(v.x, v.y-1), oldSymbol, newSymbol, d);

            for (String t : alphabet){
                if (t == "H") {
                    if (homesToPwr != 0) {
                        homesToPwr--;
                    }
                }

                    visit(grid, v.x + 1, v.y, new Pair(v.x+1, v.y), t, newSymbol, d);
                    visit(grid, v.x - 1, v.y, new Pair(v.x-1, v.y), t, newSymbol, d);
                    visit(grid, v.x, v.y + 1, new Pair(v.x, v.y+1), t, newSymbol, d);
                    visit(grid, v.x, v.y - 1, new Pair(v.x, v.y-1), t, newSymbol, d);

            }
            for (String s : symbols) {
                visit(grid, v.x + 1, v.y, new Pair(v.x+1, v.y), s, newSymbol, d);
                visit(grid, v.x - 1, v.y, new Pair(v.x-1, v.y), s, newSymbol, d);
                visit(grid, v.x, v.y + 1, new Pair(v.x, v.y+1), s, newSymbol, d);
                visit(grid, v.x, v.y - 1, new Pair(v.x, v.y-1), s, newSymbol, d);
            }
        }
    }

    public void visit (String[][] A, int x, int y, Pair pair, String oldSymbol, String newSymbol, int d) {
        if ((x >= 0 && y >= 0) && (x < row && y < col) && A[x][y].equals(oldSymbol)) {
            if (oldSymbol == "H")
                totalHomes++;
            A[x][y] = newSymbol;
            dist.put(pair, d);
            q.addLast(pair);

        }
    }

    public class Pair {
        int x, y;
        public Pair (int x, int y) {
            this.x = x;
            this.y = y;
        }

        public String toString() {
            String out = "X: " + this.x + "  Y: " + this.y;
            return out;
        }
    }
}
4

1 回答 1

0

您的程序没有正确计算房屋总数。

例如这里有 19 间房子,但按访问方法计算的只有 18 间。这是因为您只访问“连接”到动力装置的案例。

15 22
, , , , , , , , , , . . . , , , , , , , , ,
, , . , , , , H , H H H - - + , , , , , , ,
, , , , , , , , , H H H . . | . , , , . , ,
, , , , , , , , , H H H = , | . , , , . . .
, , , , , , , , , H H H = , | . , , , . . .
, , , . . . . . , H H H = , | , , , , . . .
. . . . . . . . , H H H = , | , , , , . . .
, , . , , = = = = = = = = , | , , , . . . .
, , X X X X , . . . . C C C C C C . . . . .
. . X P X X , . . . . C C C C C C . . . . .
. . X X X X - - - - - C C C C C C . . . . .
. ~ X X X X . . . . . . . . . . . . . . . .
~ ~ ~ ~ . . . . . . . . . . . . . . . . . .
~ ~ ~ ~ ~ . . . . . . . . . . . . . . . . ~
~ ~ ~ ~ ~ ~ . . . . . . . . . . . ~ ~ . ~ ~

您可以这样计算房屋总数:

    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++) {
            grid[i][j] = sc.next();
            if(grid[i][j].equals("H")){
                totalHomes++;
            }
        }

然后以这种方式计算实际的动力房屋数量(我使用了一个新属性homesPwrd(= 0),仅通过这种方法修改,不确定你所有其他整数有什么好处......):

public void visit (String[][] A, int x, int y, Pair pair, String oldSymbol, String newSymbol, int d) {
    if ((x >= 0 && y >= 0) && (x < row && y < col) && A[x][y].equals(oldSymbol)) {
        if (oldSymbol == "H")
            homesPwrd++;
        A[x][y] = newSymbol;
        dist.put(pair, d);
        q.addLast(pair);

    }
}

最后

    System.out.println(totalHomes-homesPwrd + " of " + totalHomes + " are without power.");

输出

19 人中有 1 人没有电。

    1 of 19 are without power.
    ,,,,,,,,,,...,,,,,,,,,
    ,,.,,,,H,******,,,,,,,
    ,,,,,,,,,***..*.,,,.,,
    ,,,,,,,,,***=,*.,,,...
    ,,,,,,,,,***=,*.,,,...
    ,,,.....,***=,*,,,,...
    ........,***=,*,,,,...
    ,,.,,========,*,,,....
    ,,****,....******.....
    ..****,....******.....
    ..***************.....
    .~****................
    ~~~~..................
    ~~~~~................~
    ~~~~~~...........~~.~~
于 2013-05-31T09:49:59.097 回答