Topcoder: Problem Statement SRM 39 DIV1 500 Point

In some cities, when you ask for the location of a place, people give you a specific address. LinearCity, on the other hand, is one-dimensional, so people just tell you to walk either left or right from your current location until you reach your desired location.

 

Over time, you have collected information about the relative positions of pairs of places in LinearCity. This information is given in the String refDirection and the int[]s refSource and refDestination. The i-th element of refDirection is the direction you need to walk (either 'L' or 'R', quotes for clarity) to get from refSource[i] to refDestination[i] (each place in the city is represented by a distinct integer between 0 and N-1, inclusive, where N is the total number of places in the city). You know that all the information is consistent, meaning that there is no place that is both to the left and to the right of another place.

 

You are arranging a tour for newcomers to the city, and you need to know the directions between various places. Your are given int[]s source and destination, each containing the same number of elements. Return a String[] where the i-th element is the direction you must walk to get from source[i] to destination[i]. If the direction cannot be deduced, the corresponding element should be "UNKNOWN" (quotes for clarity). Otherwise, it should be either "LEFT" of "RIGHT" (quotes for clarity).

 

Definition

    

Class:

LinearCity

Method:

getReference

Parameters:

int[], int[], String, int, int[], int[]

Returns:

String[]

Method signature:

String[] getReference(int[] refSource, int[] refDestination, String refDirection, int N, int[] source, int[] destination)

(be sure your method is public)

    

 

Constraints

-

refSource will contain between 1 and 50 elements, inclusive.

-

Each element of refSource will be between 0 and N-1, inclusive.

-

refDestination will contain the same number of elements as refSource.

-

Each element of refDestination will be between 0 and N-1, inclusive.

-

refSource[i] will be different than refDestination[i], for all i between 0 and M-1, where M is the size of refSource.

-

refDirection will contain the same number of characters as the number of elements in refSource.

-

Each character in refDirection will be 'L' or 'R'.

-

N will be between 2 and 50, inclusive.

-

source will contain between 1 and 50 elements, inclusive.

-

Each element of source will be between 0 and N-1, inclusive.

-

destination will contain the same number of elements as source.

-

Each element of destination will be between 0 and N-1, inclusive.

-

source[i] will be different than destination[i], for all i between 0 and K-1, where K is the size of source.

-

All references will be consistent, as explained in the problem statement.

Examples

0)

 

    

{1, 2}

{2, 0}

"RR"

3

{1, 0}

{0, 1}

Returns: {"RIGHT", "LEFT" }

If you can go from 1 to 2 walking to the right and you can go from 2 to 0 walking to the right, then you can go from 1 to 0 walking to the right passing by 2. Finally, if you can go from 1 to 0 walking to the right, then you can go from 0 to 1 walking to the left.

1)

 

    

{1, 0}

{2, 2}

"RL"

3

{1, 0}

{0, 1}

Returns: {"RIGHT", "LEFT" }

This is the same case as Example 0, but with the direction between 0 and 2 reversed.

2)

 

    

{2, 3, 1, 0, 2, 0, 5, 5}

{1, 4, 4, 4, 4, 3, 2, 3}

"RLRLRLLL"

6

{0, 1, 0}

{2, 3, 5}

Returns: {"LEFT", "RIGHT", "UNKNOWN" }

You can go from 0 to 2 walking to left passing through places 3, 4 and 1, in order. Similarly, you can go from 1 to 3 walking to the right passing through place 4. However, it is impossible to find a unidirectional path from 0 to 5 using the given references.

3)

 

    

{1, 0, 2, 3}

{0, 2, 3, 2}

"RRRL"

5

{0, 2, 3, 0, 4}

{2, 4, 1, 1, 0}

Returns: {"RIGHT", "UNKNOWN", "LEFT", "LEFT", "UNKNOWN" }

It is possible to have repeated references, and to have a query about a place not mentioned in the given references.

4)

 

    

{6, 0, 0, 5, 2, 4, 1, 1, 1, 6, 2, 0, 2, 2, 3, 1, 5, 1, 5, 6, 0}

{4, 6, 4, 2, 6, 3, 2, 4, 5, 5, 3, 1, 0, 4, 0, 6, 4, 3, 3, 3, 5}

"RLRLRRRRRRRLRRLRRRRRL"

7

{5, 6, 2, 4, 6, 2, 4}

{0, 0, 0, 5, 2, 5, 6}

Returns: {"RIGHT", "RIGHT", "RIGHT", "LEFT", "LEFT", "RIGHT", "LEFT" }


============================================================================================

import java.util.*;

public class LinearCity {

    public String[] getReference
    (int[] refSource, int[] refDestination, String refDirection, int N, int[] source, int[] destination){
       
        Vector<String> result = new Vector<String>();
       
        int [][]right=new int[N][N], left=new int[N][N];
       
        int i, j;
       
        for(i=0; i<N; i++)
            for(j=0; j<N; j++)
                right[i][j] = left[i][j] = -1;
       
        for(i=0; i<refSource.length; i++){
            int a = refSource[i], b= refDestination[i];
            char direction = refDirection.charAt(i);
            if(direction=='L'){
                b = refSource[i];
                a = refDestination[i];
            }
           
            for(j=0; j<N; j++){
                if(left[a][j]!=-1)
                    left[b][j] = 1;
                if(right[b][j]!=-1)
                    right[a][j] = 1;
            }
           
            left[b][a] = 1;
            right[a][b] = 1;   
        }
        for(i=0; i<source.length; i++)
        {
            int s = source[i], d= destination[i];
            if(right[s][d]==1 || left[d][s]==1)
                result.add("RIGHT");
            else if(left[s][d]==1 || right[d][s]==1)
                result.add("LEFT");
            else
                result.add("UNKNOWN");
               
        }
        return result.toArray(new String[0]);
    }
        public static void main(String[] args) {
   
        // TODO Auto-generated method stub
            LinearCity c = new LinearCity();
            int[] refSource={1,2},
                refDestination={2,0};
            String refDirection= "RR";
            int N=3;
            int []source={1,0}, destination={0,1};
            String[] result =c.getReference(refSource,refDestination, refDirection, N, source, destination);
            for(int i=0; i< result.length; i++)
                System.out.println("result " + i + ":" + result[i]);
        }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值