topcoder SRM495 div1 level2

Problem Statement

 There are N boxes numbered 0 through N-1. Every box except for one contains carrots, but Rabbit Hanako does not know which box is the empty one. Each box has the same probability of being empty.



Hanako wants to find the empty box without opening it. Fortunately, she has some clues. Some of the boxes contain information about the content of other boxes, and she knows which boxes contain such information. You are given a String[] information, where the j-th character of the i-th element is 'Y' if opening the i-th box will reveal whether or not the j-th box contains carrots, or 'N' if the i-th box contains no such information.



Return the probability that she can find the empty box without opening it, assuming she behaves optimally.
 

Definition

 
Class:CarrotBoxes
Method:theProbability
Parameters:String[]
Returns:double
Method signature:double theProbability(String[] information)
(be sure your method is public)
 
 
 

Constraints

-information will contain between 1 and 50 elements, inclusive.
-Each element of information will contain exactly N characters, where N is the number of elements of information.
-The i-th character of the i-th element of information will be 'Y'.
-Each character in information will be 'Y' or 'N'.
 

Examples

0) 
 
{"YYYYY",
 "NYNNN",
 "NNYNN",
 "NNNYN",
 "NNNNY"}
Returns: 0.8
The optimal strategy is opening box 0 first. If box 0 contains carrots, she can find the empty box without opening it because box 0 contains information about all boxes. It happens with probability 0.8.
1) 
 
{"YNNNN",
 "NYNNN",
 "NNYNN",
 "NNNYN",
 "NNNNY"}
Returns: 0.2
No box contains information about other boxes, so she can find the empty box without opening it only when she opens all other boxes. It happens with probability 0.2.
2) 
 
{"Y"}
Returns: 1.0
Since there is only one box, she knows that the only box is empty.
3) 
 
{"YNNNN",
 "YYNNN",
 "YNYNN",
 "NNNYY",
 "NNNYY"}
Returns: 0.6
4) 
 
{"YYYNNNYN",
 "NYNNNNYN",
 "NNYNNNNN",
 "NYNYNNNN",
 "YNNNYNNY",
 "NNYNNYNN",
 "NNNNYNYN",
 "NNYNNNNY"}
Returns: 0.875
5) 
 
{"YNNNNNNNNYNNNNNNNNNN",
 "NYNNNNNNNNNNNNNNNNNN",
 "NNYNNNNNNNYNNNNNYNNN",
 "NNNYNYNNNNNNNNYNNNNN",
 "NNNNYNNNNNNNNNYNNNNY",
 "NNNNNYNNNNNNNNNNNNNY",
 "NNNNYNYNYNNNNNNNNNNN",
 "NNNNNNNYNNNYYNNNNNNN",
 "NNNNNNNNYNNNNNNNNNNN",
 "YNNNNNNNNYNNNNNYNNNN",
 "NNNNNNNNNNYNNNNNNNNN",
 "NYNNNNNNNNNYNNNNNNNN",
 "NNNNNNNYNNNNYNNNNNNN",
 "NNNNNNNNNNNNNYNNNYNN",
 "NNNNNNNNNNNYNNYNNNYN",
 "NYNNNNNNNNNNNNNYNNNN",
 "NNYNNNNNNNNNNNNNYNNN",
 "NNNNNNNNNNNNNYNYNYNN",
 "NNNNNNNNYNYNNNNNNNYY",
 "NNNYNNNNNNNNNNNNNNNY"}
Returns: 0.75


【题解】

显然,我们可以用一个图来描述问题。G(V,E) V是N个点的集合,而E则是关系。

其实这是一个类似覆盖的问题。由于是有向图,所以要缩点

必然是要选入度为0的点,所以算法由此而生。

但要注意一点:存在一种情况,可以不打开最后一个箱子,因为那时已经可以得出那个箱子是空的。

【代码】

public class CarrotBoxes
{
	public double theProbability(String[] information)
	{
	    int n = information.length;
	    // Floyd-Warshall to get an array connected[i][j]
	    // connected[i][j] is true if there is a directed path
	    // between i and j.
		boolean[][] connected = new boolean[n][n];
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
			    connected[i][j] = (information[i].charAt(j)=='Y');
			}
		}
		for (int k=0; k<n; k++) {
			for (int i=0; i<n; i++) {
				for (int j=0; j<n; j++) {
				    connected[i][j] = connected[i][j]
				                     || (connected[i][k] && connected[k][j] );
				}
			}

		}
		
		// Find the top-most components
		int[] top = new int[n];
		int   tn  = 0;
		boolean[] tried = new boolean[n];
		
		for (int i=0; i<n; i++) {
		    if(! tried[i]) {
		        //mark all the boxes in the same component as tried
		        for (int j=0; j<n; j++) {
		            if ( connected[i][j] && connected[j][i] ) {
		                tried[j] = true;
		            }
		        }
		        // count the number of in-going connections to the
		        // box.
		        int c = 0;
		        for (int j=0; j<n; j++) {
		            // interested only in connections from other components
		            if ( connected[j][i]  && ! connected[i][j] ) {
		                c ++;
		            }
		        }
		        if ( c== 0) {
		            // It is a "top-most" component, add to our array
		            top[tn++] = i;
		        }
		    }
		}
		
		// Try to pick which box to open last.
		for (int lastid = 0; lastid < tn; lastid++) {
		    int last = top[lastid];
		    boolean[] opened = new boolean[n];
		    // Assume we open a box from each of the
		    // other top components, then mark all the
		    // boxes reachable from those components 
		    // as "opened"
		    for (int j=0; j<tn; j++) {
		        if ( j != lastid ) {
		            for (int k=0; k<n; k++) {
		                if (connected[ top[j] ][k]) {
		                    opened[k] = true;
		                }
		            }
		        }
		    }
		    
		    // If only one box is left open, then we can use
		    // this improvement.
		    boolean ok = true;
		    for (int j=0; j<n; j++) {
		        if ( (j!=last) && ! opened[j] ) {
		            ok = false;
		        }
		    }
		    if ( ok ) {
        		return ( (n-(tn-1) ) / (double)(n));
		    }
		}
		
		// Since there is no improvement with last method, 
		// we will have to open one box per each of the tn top components:
		return ( (n-tn) / (double)(n));
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值