SalesRouting

package salesRouting;

/*Problem Statement

    You want to send a group of salespeople from location 0 to location 1, but no two of them can travel through the same location (other than 0 and 1).
    
    This removes the possibility of trying to sell a customer the same product twice.
    
    Character j of element i (both 0-based) of adj denotes whether locations i and j are connected by a symmetric link ('1' for connected, '0' otherwise).
    
    Return the greatest number of salespeople that can be sent. The constraints will guarantee that locations 0 and 1 do not share a link.

Definition

Class:     SalesRouting
Method:     howMany
Parameters:     String[]
Returns:     int
Method signature:     int howMany(String[] adj)
(be sure your method is public)

Constraints
-     adj will contain between 3 and 12 elements, inclusive.
-     Each element of adj will contain exactly N characters, where N is the number of elements in adj.
-     Each character in adj will be '0' (zero) or '1' (one).
-     Character i of element j of adj will be the same as character j of element i.
-     Character i of element i of adj will be '0'.
-     Character 1 of element 0 of adj will be '0'.

Examples
0)     {"001","001","110"}    Returns: 1
We can send a single salesperson from location 0 to location 2, and finally to location 1.

1)     {"0010","0010","1100","0000"}    Returns: 1
Same as before, but now there is an isolated location 3.

2)     {"001100","000001","100010","100010","001101","010010"}        Returns: 1
The only location that is directly connected to location 1 is 5, so only 1 salesperson can be sent.

3)     {"001111","001111","110000","110000","110000","110000"}        Returns: 4

4)     {"00000","00000","00000","00000","00000"}        Returns: 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc.
All rights reserved.*/

public class SalesRouting {
    
    public static int howMany(String[] adj) {
        matrix = new int[adj.length][adj[0].length()];
        flags = new int[adj.length];
        
        for(int i = 0; i < adj.length; i++) {
            flags[i] = 0;
            for(int j = 0; j < adj[0].length(); j++) {
                matrix[i][j] = adj[i].charAt(j) - 48;
                
            }
        }
        
        return iterate(0);    
    }

    private static int[][] matrix;
    private static int[] flags;
    private static int iterate(int index) {
        int result = 0;
        if(index == 1) {
            System.out.print(index+ " ");
            return 1;
        }
        
        int next = 1;
        while(next < matrix.length) {
            if(matrix[index][next] == 1 && flags[next] == 0 && flags[index] == 0) {
                if(index != 0 && index != 1) flags[index] = 1;
                int success = iterate(next);
                if (success == 1) {
                    if(index == 0) {
                        System.out.println(0 + " ");
                    } else System.out.print(index+ " ");
                    result++;
                }else flags[index] = 0;
            }
            next++;
        }
        return result;
    }
}

    public static void main(String[] args) {
        //String[] str={"0011","0011","1101","1110"};
        //String[] str={"0011111","0011111","1100000","1100000","1100000","1100000","1100000"};
        String[] str={"001100","000011","100110","101001","011000","010100"};
        System.out.println("How many: " + SalesRouting.howMany(str));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值