首页 > 专题系列 > Java解POJ > POJ 1979 Red and Black [解题报告] Java
2013
11-10

POJ 1979 Red and Black [解题报告] Java

Red and Black

问题描述 :

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

输入:

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ – a black tile

‘#’ – a red tile

‘@’ – a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

输出:

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

样例输入:

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

样例输出:

45
59
6
13

解题代码:

import java.io.BufferedInputStream;   
import java.util.Scanner;   
  
/**  
 *  
 *poj1979  
 * 这道题是数据结构的第一道,刚开始是用c语言做的,但后来其他题目我都用了java去写,  
 * 所以现在只是简单把c语法转化为java法,统一一下编码而已  
 * @author NC  
 */  
public class Main {   
  
    private final static int MAXCOL = 22;   
    private final static int MAXROW = 22;   
    private static int[][] floor = new int[MAXROW][MAXCOL]; //最多是20行,20列,再加两行两列作为边界   
    private static int[][] visited = new int[MAXROW][MAXCOL]; //标记是否访问过,默认没有访问过   
  
    public static void main(String[] args) {   
  
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        while (scan.hasNext()) {   
            int col = scan.nextInt();   
            int row = scan.nextInt();   
            int i = 0, j = 0, k = 0;   
            int count = 0;   
            int flag = 0;   
            char c;   
            //读取数据   
            if (col == 0 && row == 0) {   
                break;   
            }   
            //因为是循环输入,所以每一次都得初始化   
            for (i = 0; i < MAXROW; i++) {   
                for (j = 0; j < MAXCOL; j++) {   
                    floor[i][j] = 0;   
                    visited[i][j] = 0;   
                }   
            }   
            scan.nextLine();   
            //一个一个读取字符,将符号转换为数字   
            for (i = 1; i <= row; i++) {   
                char[] ss = scan.nextLine().trim().toCharArray();   
                for (j = 1; j <= col; j++) {   
                    while (true) {   
                        c = ss[j-1];   
                        if (c == '.' || c == '#' || c == '@') {   
                            break;   
                        }   
                    }   
                    if (c == '.') {   
                        floor[i][j] = 1;   
                    } else if (c == '#') {   
                        floor[i][j] = 2;   
                    } else if (c == '@') {   
                        floor[i][j] = 3;   
                    }   
                }   
            }   
            //找出人所站的位置   
            flag = 0;   
            for (i = 1; i <= row; i++) {   
                for (j = 1; j <= col; j++) {   
                    if (floor[i][j] == 3) {   
                        flag = 1;   
                        break; //要跳出两层   
                    }   
                }   
                if (flag == 1) {   
                    break;   
                }   
            }   
            //深度遍历   
            if (floor[i][j] == 3) {   
                DFS(i, j);   
            }   
            //count the number of tiles he can reach from the initial tile   
            count = 0;   
            for (i = 1; i <= row; i++) {   
                for (j = 1; j <= col; j++) {   
                    if (visited[i][j] == 1) {   
                        count++;   
                    }   
                }   
            }   
            //print the number of tiles he can reach from the initial tile   
            System.out.println(count);   
        }   
    }   
  
    static void DFS(int i, int j) {   
        //标记访问过   
        visited[i][j] = 1;   
        //四个方向都要遍历,不能用else if   
        if (floor[i][j + 1] == 1 && visited[i][j + 1] == 0) {   
            DFS(i, j + 1);   
        }   
        if (floor[i][j - 1] == 1 && visited[i][j - 1] == 0) {   
            DFS(i, j - 1);   
        }   
        if (floor[i + 1][j] == 1 && visited[i + 1][j] == 0) {   
            DFS(i + 1, j);   
        }   
        if (floor[i - 1][j] == 1 && visited[i - 1][j] == 0) {   
            DFS(i - 1, j);   
        }   
    }   
}

  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢