首页 > 专题系列 > Java解POJ > POJ 1179 Polygon [解题报告] Java
2013
11-09

POJ 1179 Polygon [解题报告] Java

Polygon

问题描述 :

Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N.



On the first move, one of the edges is removed. Subsequent moves involve the following steps:

�pick an edge E and the two vertices V1 and V2 that are linked by E; and

�replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2.

The game ends when there are no more edges, and its score is the label of the single vertex remaining.

Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.



Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.

输入:

Your program is to read from standard input. The input describes a polygon with N vertices. It contains two lines. On the first line is the number N. The second line contains the labels of edges 1, …, N, interleaved with the vertices’ labels (first that of the vertex between edges 1 and 2, then that of the vertex between edges 2 and 3, and so on, until that of the vertex between edges N and 1), all separated by one space. An edge label is either the letter t (representing +) or the letter x (representing *).

3 <= N <= 50

For any sequence of moves, vertex labels are in the range [-32768,32767].

输出:

Your program is to write to standard output. On the first line your program must write the highest score one can get for the input polygon. On the second line it must write the list of all edges that, if removed on the first move, can lead to a game with that score. Edges must be written in increasing order, separated by one space.

样例输入:

4
t -7 t 4 x 2 x 5

样例输出:

33
1 2

解题代码:

import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
    public static BigInteger[][] dp=new BigInteger[110][110];
    public static BigInteger[][] dp1=new BigInteger[110][110];
    public static BigInteger INF;
    public static BigInteger NEG;
    public static BigInteger[] array=new BigInteger[200];
    public static char[] ops=new char[100];
    public static BigInteger[] warray=new BigInteger[200];
    public static char[] wops=new char[100];
    public static int N; 

    static void DP(int s,int t){
        if(s==t){  
          dp1[s][t]=dp[s][t]=warray[s]; 
           return; 
        } 
       if(!dp[s][t].max(NEG).equals(NEG))        {
            return;
        } 

        BigInteger a,b,c,d;
        BigInteger tmp; 
        for(int i=s;i< t;i++){
            DP(s,i);
            DP(i+1,t);
            a=dp[s][i]; 
            b=dp1[s][i];
            c=dp[i+1][t];
            d=dp1[i+1][t]; 
            switch(wops[i]){
              case 't': 
              {                
               dp[s][t]=dp[s][t].max(a.add(c)); 
               dp1[s][t]=dp1[s][t].min(b.add(d)); 
               break; 
              }  

              case 'x': 
              { 
               tmp=a.multiply(c); 
               tmp=tmp.max(b.multiply(d));
               dp[s][t]=dp[s][t].max(tmp); 
               tmp=a.multiply(d);
               tmp=tmp.min(c.multiply(b));
               tmp=tmp.min(a.multiply(c)); 
               tmp=tmp.min(b.multiply(d)); 
               dp1[s][t]=dp1[s][t].min(tmp);
               break; 
              }  
            }
          }  
      } 

      static void work(){
        BigInteger res=NEG;
        BigInteger tmp;
        int cnt=0;
        int[] record=new int[100];
        for(int i=0;i< N;i++){
            for(int j=0;j< N;j++){
               wops[j]=ops[(i+j+1)%N]; 
               warray[j]=array[(i+j)%N]; 
            }
            for(int j=0;j< 100;j++) {
                for(int k=0;k< 100;k++){ 
                   dp[j][k]=NEG;
                    dp1[j][k]=INF; 
                }
            } 
           DP(0,N-1);
          //System.out.println(dp[0][N-1]);
            if(!res.max(dp[0][N-1]).equals(res)) { 
               res=dp[0][N-1]; 
               cnt=0;
               record[cnt++]=i+1; 
             }else if(res.max(dp[0][N-1]).equals(dp[0][N-1])){  
              record[cnt++]=i+1; 
             }
        }   
             System.out.println(res); 
            for(int i=0;i< cnt;i++){
               if(i>0)
                System.out.print(" ");
               System.out.print(record[i]); 
            } 
           System.out.println(""); 
    } 

       public static void main(String[] args) throws FileNotFoundException    {
        Scanner cin=new Scanner(System.in);//Scanner cin=new Scanner(new File("input.txt")); 
       INF=BigInteger.valueOf(10).pow(301); 
       NEG=BigInteger.valueOf(-10).pow(301); 
       String[] buffer=new String[200]; 
       String str;
        str=cin.nextLine(); 
       N=Integer.parseInt(str); 
       str=cin.nextLine(); 
       buffer=str.split(" "); 
       for(int i=0;i< N;i++){
            ops[i]=buffer[i*2].charAt(0);  
          array[i]=new BigInteger(buffer[i*2+1]);  
      } 
       work(); 
   }
}

  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  2. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”