首页 > 专题系列 > Java解POJ > POJ 1164 The Castle [解题报告] Java
2013
11-09

POJ 1164 The Castle [解题报告] Java

The Castle

问题描述 :

     1   2   3   4   5   6   7  

#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(Figure 1)

# = Wall
| = No wall
- = No wall



Figure 1 shows the map of a castle.Write a program that calculates

1. how many rooms the castle has

2. how big the largest room is

The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.

输入:

Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.

输出:

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

样例输入:

4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

样例输出:

5
9

解题代码:

//* @author 
import java.io.*; 
import java.util.*; 

public class Main 
{ 
    static Room[][] matrix; 
    static int row; 
    static int col; 
    public static void main(String[] args) throws Exception 
    { 
        readFile(); 
        for(int i=0; i< row; i++) 
        { 
            for(int j=0; j< col; j++) 
            { 
                if(!matrix[i][j].bFlag) 
                { 
                    process(i,j); 
                    sumCount++; 
                    if(maxCount>INF) 
                    { 
                        INF = maxCount; 
                    } 
                    maxCount=0; 
                } 
            } 
        } 
        System.out.print(sumCount); 
        System.out.print("\n"+INF); 
    } 

    public static void readFile() throws Exception 
    { 
        BufferedReader br = new BufferedReader( 
            new InputStreamReader(System.in)); 
        StringTokenizer st = null; 
        row = Integer.valueOf(br.readLine()); 
        col = Integer.valueOf(br.readLine()); 
        matrix = new Room[row][col]; 
        int count = 0; 
        while(count< row) 
        { 
            st = new StringTokenizer(br.readLine()," "); 
            int j = 0; 
            Room room = null; 
            while(st.hasMoreTokens()) 
            { 
                room = new Room(Integer.valueOf(st.nextToken())); 
                matrix[count][j] = room; 
                j++; 
            } 
            count++; 
        } 
    } 

    static void displayRoom() 
    { 
        for(int i=0; i< row; i++) 
        { 
            for(int j=0; j< col; j++) 
                System.out.println(matrix[i][j]); 
            System.out.println(); 
        } 
    } 


    static int sumCount = 0; 
    static int maxCount = 0; 
    static int INF = 0; 
    static void process(int i,int j) 
    { 
        if(i< 0 || j< 0 || i>=row || j>=col) 
            return; 
        if(matrix[i][j].bFlag) 
            return; 
        maxCount++; 
        matrix[i][j].bFlag = true; 
        if(matrix[i][j].states[0][1]==2) 
        { 
            process(i-1,j); 
        } 
        if(matrix[i][j].states[1][2]==2) 
        { 
            process(i,j+1); 
        } 
        if(matrix[i][j].states[2][1]==2) 
        { 
            process(i+1,j); 
        } 
        if(matrix[i][j].states[1][0]==2) 
        { 
            process(i,j-1); 
        } 
         
         
    } 

     
    static class Room 
    { 
        int[][] states = new int[3][3]; 
        boolean bFlag = false; 
        Room(int flag) 
        { 
            states[1][1] = 1; 
            switch(flag) 
            { 
            case 0: 
                states[0][1] = 2; 
                states[1][2] = 2; 
                states[1][0] = 2; 
                states[2][1] = 2; 
                break; 
            case 1: 
                states[0][1] = 2; 
                states[1][2] = 2; 
                states[2][1] = 2; 
                break; 
            case 2: 
                states[1][0] = 2; 
                states[1][2] = 2; 
                states[2][1] = 2; 
                break; 
            case 3: 
                states[1][2] = 2; 
                states[2][1] = 2; 
                break; 
            case 4: 
                states[0][1] = 2; 
                states[1][0] = 2; 
                states[2][1] = 2; 
                break; 
            case 5: 
                states[0][1] = 2; 
                states[2][1] = 2; 
                break; 
            case 6: 
                states[1][0] = 2; 
                states[2][1] = 2; 
                break; 
            case 7: 
                states[2][1] = 2; 
                break; 
            case 8: 
                states[0][1] = 2; 
                states[1][0] = 2; 
                states[1][2] = 2; 
                break; 
            case 9: 
                states[0][1] = 2; 
                states[1][2] = 2; 
                break; 
            case 10: 
                states[1][0] = 2; 
                states[1][2] = 2; 
                break; 
            case 11: 
                states[1][2] = 2; 
                break; 
            case 12: 
                states[0][1] = 2; 
                states[1][0] = 2; 
                break; 
            case 13: 
                states[0][1] = 2; 
                break; 
            case 14: 
                states[1][0] = 2; 
                break; 
            case 15: 
                break; 
            } 
        } 

        public String toString() 
        { 
            for(int i=0; i< 3; i++) 
            { 
                for(int j=0; j< 3; j++) 
                    System.out.print(states[i][j] + " "); 
                System.out.println(); 
            } 
            return ""; 
        } 
    } 
}

  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.