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

POJ 1101 The Game [解题报告] Java

The Game

问题描述 :

One morning, you wake up and think: “I am such a good programmer. Why not make some money?” So you decide to write a computer game.

The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.

One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:

It consists of straight segments, each one being either horizontal or vertical.

It does not cross any other game pieces.

(It is allowed that the path leaves the board temporarily.)

Here is an example:



The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.

The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.

输入:

The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a "X" if there is a game piece at this location, and a space if there is no game piece.

Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0".

The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.

输出:

For each board, output the line “Board #n:”, where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with “Pair m: “, where m is the number of the pair (starting the count with 1 for each board). Follow this by “ksegments.”, where k is the minimum number of segments for a path connecting the two game pieces, or “impossible.”, if it is not possible to connect the two game pieces as described above.

Output a blank line after each board.

样例输入:

5 4
XXXXX
X   X
XXX X
 XXX 
2 3 5 3
1 3 4 4
2 3 3 4
0 0 0 0
0 0

样例输出:

Board #1:
Pair 1: 4 segments.
Pair 2: 3 segments.
Pair 3: impossible.

解题代码:

import java.util.*;
  class Piece{ 
    int x, y;  
    int step;  

    public Piece(){
        this(0,0,0);
    }

    public Piece(int x,int y,int step){
      this.x=x;
      this.y=y;
      this.step=step;
    }
  }

public class Main{
   private char g[][];  
   private int w, h;  
   private int dir[][] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};  
   private boolean visited[][];  

   private Piece start, goal;  

   public Main(int w,int h, Piece start, Piece goal,char[][] g){
         this.w=w;
         this.h=h;
         this.g=g;
         this.start=start;
         this.goal=goal;
         visited=new boolean[h+2][w+2];
   }

   public Piece getGoal(){
     return goal;
   }

   
   public void bfs(){  
    int i;  
   	Queue que = new LinkedList();
	que.add(start);  
        visited[start.x][start.y] = true;  
    while(!que.isEmpty())  
    {  
        Piece temp1 = que.poll();  
        if(temp1.x==goal.x && temp1.y==goal.y && goal.step>temp1.step)  
        {  
                goal.step = temp1.step;  
                break;
         }  
        for(i=0; i<4; ++i)  
        {  
           
            Piece temp2=new Piece();  
            temp2.x = temp1.x + dir[i][0];  
            temp2.y = temp1.y + dir[i][1];  
           while(temp2.x>=0 && temp2.x<=h+1    
                && temp2.y>=0 && temp2.y<=w+1 
				&& !visited[temp2.x][temp2.y] &&g[temp2.x][temp2.y]==' '){//在同一个方向继续向下走   
               
                visited[temp2.x][temp2.y] = true;  
                temp2.step = temp1.step+1;  
                que.add(new Piece(temp2.x,temp2.y,temp2.step));  
                 
                temp2.x += dir[i][0];  
                temp2.y += dir[i][1];  
               
            }     
        }  
    }  
}  

public static void main(String args[]){  
    Scanner in=new Scanner(System.in);
    
    int nPacase=0;
    int nCase=0;
    Piece start=new Piece();
    Piece goal=new Piece();
    while(true)   {  

     int w=in.nextInt();
     int h=in.nextInt();
      in.nextLine();
     if(w==0&&h==0) break;
     char g[][]=new char[h+2][w+2];
     for(int i=1; i<=h; ++i)  
        {  
            String line=in.nextLine();
              for(int j=1; j<=w; j++)  
                g[i][j]=line.charAt(j-1);  
        }  
   
        nPacase = 0;  
              
        //外围加框   
        for(int i=0; i<=w+1; i++)  
        {  
            g[0][i] = ' ';  
            g[h+1][i] = ' ';  
        }  
        for(int i=0; i<=h+1; ++i)  
        {  
            g[i][0] = ' ';  
            g[i][w+1] = ' ';  
        }  

        //测试图的正确性   
       /* for(int i=0; i<=h+1; i++) 
        { 
            for(int j=0; j<=w+1; j++) 
                System.out.print(g[i][j]); 
              System.out.println(); 
        }  */

        
        System.out.println("Board #"+(++nCase)+":");  
        while(1==1)  
        {  
             char[][] c=new char[h+2][w+2];
              
          for(int i=0; i<=h+1; i++) 
           { 
            for(int j=0; j<=w+1; j++) 
                c[i][j]=g[i][j]; 
             
           }  
    
            start.y=in.nextInt();
            start.x=in.nextInt();
            goal.y=in.nextInt();
            goal.x=in.nextInt();
         
            start.step = 0;  
            goal.step = 1000000;  
            if(start.x==0 && start.y==0 && goal.x==0 && goal.y==0)  
                break;  
          
            if(c[goal.x][goal.y] == 'X')  
            {  
                c[goal.x][goal.y] = ' ';  
            
            }  
            Main m=new Main(w,h,start,goal,c);
            m.bfs();  
            
            if(m.getGoal().step == 1000000)  
                System.out.println("Pair "+(++nPacase)+": impossible.");  
            else  
                System.out.println("Pair "+(++nPacase)+": "+m.getGoal().step+" segments.");  


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