You are on page 1of 6

Problem Statement for Marketing (Tugas 1 No.

1 DFS )
Problem Statement
You work for a very large company that markets many different products. In some cases, one product you market competes with another. To help deal with this situation you have split the intended consumers into two groups, namely Adults and Teenagers. If your company markets 2 products that compete with each other, selling one to Adults and the other to Teenagers will help maximize profits. Given a list of the products that compete with each other, you are going to determine whether all can be marketed such that no pair of competing products are both sold to Teenagers or both sold to Adults. If such an arrangement is not feasible your method will return -1. Otherwise, it should return the number of possible ways of marketing all of the products.

The products will be given in a String[] compete whose kth element describes product k. The kth element will be a single-space delimited list of integers. These integers will refer to the products that the kth product competes with. For example:
compete = {"1 4", "2", "3", "0", ""}

The example above shows product 0 competes with 1 and 4, product 1 competes with 2, product 2 competes with 3, and product 3 competes with 0. Note, competition is symmetric so product 1 competing with product 2 means product 2 competes with product 1 as well. Ways to market: 1) 0 to Teenagers, 1 to Adults, 2 to Teenagers, 3 to Adults, and 4 to Adults 2) 0 to Adults, 1 to Teenagers, 2 to Adults, 3 to Teenagers, and 4 to Teenagers Your method would return 2.

Definition
Class: Marketing Method: howMany Parameters: String[] Returns: long Method signature: long howMany(String[] compete) (be sure your method is public)

Constraints
- compete will contain between 1 and 30 elements, inclusive. - Each element of compete will have between 0 and 50 characters, inclusive. - Each element of compete will be a single space delimited sequence of integers such that:

All of the integers are unique. Each integer contains no extra leading zeros. Each integer is between 0 and k-1 inclusive where k is the number of elements in compete.

- No element of compete contains leading or trailing whitespace. - Element i of compete will not contain the value i. - If i occurs in the jth element of compete, j will not occur in the ith element of compete.

Examples
0)
{"1 4","2","3","0",""} Returns: 2

The example from above. 1)


{"1","2","0"} Returns: -1

Product 0 cannot be marketed with product 1 or 2. Product 1 cannot be marketed with product 2. There is no way to achieve a viable marketing scheme. 2)
{"1","2","3","0","0 5","1"} Returns: 2

3)
{"","","","","","","","","","", "","","","","","","","","","", "","","","","","","","","",""} Returns: 1073741824

4)
{"1","2","3","0","5","6","4"} Returns: -1

Problem Statement for Escape (Tugas 1 No. 2 BFS)


Problem Statement
You are playing a video game that involves escaping from a dangerous area. Within the area there are DEADLY regions you can't enter, HARMFUL regions that take 1 life for every step you make in them, and NORMAL regions that don't affect you in any way. You will start from (0,0) and have to make it to (500,500) using only Up, Left, Right, and Down steps. The map will be given as a String[] deadly listing the DEADLY regions and a String[] harmful listing the HARMFUL regions. The elements in each of these parameters will be formatted as follows: Input format(quotes for clarity): "X1 Y1 X2 Y2" where (X1,Y1) is one corner of the region and (X2,Y2) is the other corner of the region The corners of the region are inclusive bounds (i.e. (4,1) and (2,2) include x-values between 4 and 2 inclusive and y-values between 1 and 2 inclusive). All unspecified regions are considered NORMAL. If regions overlap for a particular square, then whichever region is worst takes effect (e.g. DEADLY+HARMFUL = DEADLY, HARMFUL+NORMAL = HARMFUL, HARMFUL+HARMFUL = HARMFUL, DEADLY+NORMAL=DEADLY). Damage taken at each step occurs based on the destination square and not on the starting square (e.g. if the square (500,500) is HARMFUL you WILL take a point of damage stepping onto it; if the square (0,0) is HARMFUL you WON'T take a point of damage stepping off of it; this works analogously for DEADLY squares). Return the least amount of life you will have to lose in order to reach the destination. Return -1 if there is no path to the destination. Your character is not allowed to leave the map (i.e. have X or Y less than 0 or greater than 500).

Definition
Class: Escape Method: lowest Parameters: String[], String[] Returns: int Method signature: int lowest(String[] harmful, String[] deadly) (be sure your method is public)

Notes
- If two harmful regions overlap, the area where they overlap is exactly the same as nonoverlapping harmful regions (i.e. the effect is NOT cumulative, and the overlapping region still takes exactly 1 life)

Constraints
- deadly will contain between 0 and 50 elements inclusive - harmful will contain between 0 and 50 elements inclusive - Each element of deadly and harmful will be of the form (quotes for clarity): "X1 Y1 X2 Y2" where X1,Y1,X2, and Y2 are integers between 0 and 500 inclusive and contain no leading zeros - Each element of deadly and harfmul will contain no leading, trailing or extra whitespace

Examples
0)
{} {} Returns: 0

There are no DEADLY or HARMFUL regions. 1)


{"500 0 0 500"} {"0 0 0 0"} Returns: 1000

(0,0) is DEADLY but that doesn't affect our path since we never step onto it (only from it). The rest of the map is NORMAL. 2)
{"0 0 250 250","250 250 500 500"} {"0 251 249 500","251 0 500 249"} Returns: 1000

Just enough space to get around the DEADLY regions. 3)


{"0 0 250 250","250 250 500 500"} {"0 250 250 500","250 0 500 250"} Returns: -1

No way around the DEADLY regions. 4)

{"468 209 456 32", "71 260 306 427", "420 90 424 492", "374 253 54 253", "319 334 152 431", "38 93 204 84", "246 0 434 263", "12 18 118 461", "215 462 44 317", "447 214 28 475", "3 89 38 125", "157 108 138 264", "363 17 333 387", "457 362 396 324", "95 27 374 175", "381 196 265 302", "105 255 253 134", "0 308 453 55", "169 28 313 498", "103 247 165 376", "264 287 363 407", "185 255 110 415", "475 126 293 112", "285 200 66 484", "60 178 461 301", "347 352 470 479", "433 130 383 370", "405 378 117 377", "403 324 369 133", "12 63 174 309", "181 0 356 56", "473 380 315 378"} {"250 384 355 234", "28 155 470 4", "333 405 12 456", "329 221 239 215", "334 20 429 338", "85 42 188 388", "219 187 12 111", "467 453 358 133", "472 172 257 288", "412 246 431 86", "335 22 448 47", "150 14 149 11", "224 136 466 328", "369 209 184 262", "274 488 425 195", "55 82 279 253", "153 201 65 228", "208 230 132 223", "369 305 397 267", "200 145 98 198", "422 67 252 479", "231 252 401 190", "312 20 0 350",

"406 72 207 294", "488 329 338 326", "117 264 497 447", "491 341 139 438", "40 413 329 290", "148 245 53 386", "147 70 186 131", "300 407 71 183", "300 186 251 198", "178 67 487 77", "98 158 55 433", "167 231 253 90", "268 406 81 271", "312 161 387 153", "33 442 25 412", "56 69 177 428", "5 92 61 247"} Returns: 254

You might also like