Statistics

Problem Statement for "PoisonedSwamp"

Problem Statement

You are about to cross a poisoned swampy area. Luckily, you have its map: the String[] swamp containing a rectangle of characters. Each character represents one unit square of the swamp as follows:

  • '.' is a safe square, you can step on it.
  • '#' is a blocked square, you cannot step on it.
  • '1'-'9' are poisoned squares. Stepping on a square with digit X increases your poison count by X.
  • 'S' is a spring with cleansing water. Stepping on it resets your poison count back to zero.

You start in the top left corner of the map (row 0, column 0) and your poison count is 0. You want to reach the bottom right corner of the map. You move in steps. In each step you can move from your current square to another square that shares a side with your current one. You may not leave the mapped area. If your poison count reaches 10 or more, you die.

You ARE allowed to enter the same square multiple times. If you step on the same poisoned square multiple times, you get the corresponding amount of poison each time you do so. If you step on the same spring multiple times, you get cleansed each time you do so.


Return "possible" if you can cross the swamp alive and "impossible" if you cannot do so.

Definition

Class:
PoisonedSwamp
Method:
cross
Parameters:
String[]
Returns:
String
Method signature:
String cross(String[] swamp)
(be sure your method is public)

Constraints

  • swamp will contain between 2 and 20 elements, inclusive.
  • Each element of swamp will contain the same number of characters, and that number will be between 2 and 20, inclusive.
  • Each character in swamp will be one of ".#123456789S" (quotes for clarity).
  • The characters in the top left and bottom right corner of swamp will be '.'.

Examples

  1. {".....", "####.", ".....", ".####", "....."}

    Returns: "possible"

    There's no poison, just find your way across.

  2. {".....", "####.", "97...", "9#2##", "97..."}

    Returns: "possible"

    There is a strongly poisoned area in this swamp but luckily it can be avoided completely by taking a shortcut and only receiving 2 poison.

  3. {".111111111.", ".#########.", "..22222...."}

    Returns: "possible"

    If you go down and right, you'll eventually collect 10 poison and die. Luckily, if you go right and then down, you'll only collect 9 poison and you'll reach the end of the swamp alive.

  4. {".#222#111", "4#S#2#1#S", "4#3#S#1#9", "S33#111#."}

    Returns: "possible"

    A lot of poison in this swamp, but luckily for us the cleansing springs are frequent enough to get us through.

  5. {"..111111111111..", "###############."}

    Returns: "impossible"

    Too much poison along this way.

  6. {"..111111111111..", "#######S#######."}

    Returns: "possible"

    This example shows that sometimes we need to revisit the same cell. In this case, the only way to cross the swamp is to make a detour: after you have 6 poison, step down to reach the cleansing spring and then return back and continue. You will reach the end of the swamp alive (with 7 poison) if you do so.

  7. {".1199999999", "99199999999", "1119911S199", "19999199199", "11S11199111", "9999999999."}

    Returns: "possible"

  8. {"..#.#.", "..#.#.", "#5#.5.", "....#."}

    Returns: "impossible"

  9. {".1.#.", ".9.#.", ".###.", "2#.1.", ".#.7."}

    Returns: "impossible"

  10. {".1..................", "###################.", "....................", ".###################", "....................", "###################.", "....................", ".###################", "....................", "###################.", "....................", ".###################", "....................", "###################.", "....................", ".###################", "....................", "#################.##", "..................#.", "..................#."}

    Returns: "impossible"

  11. {".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1..................", ".1.................#", ".1................#."}

    Returns: "impossible"

  12. {"..11111111111111111.", "#######S###S#######."}

    Returns: "possible"

  13. {"..11111111111111111.", "#######.###S#######."}

    Returns: "impossible"

  14. {"..11111111111111111.", "#######S###.#######."}

    Returns: "impossible"

  15. {"..111111111111..", "#######.#######.", "..............#.", ".############.#.", ".7S#..4.....4.#.", ".####.#.#5#.#.#.", "....6.....6...#.", "#4#.#.#7#.#4#9#.", "5...7.6.......#.", "#.#.#4#7#4###.#.", "S3....4S2.....#."}

    Returns: "possible"

  16. {"..111111111111..", "#######.#######.", "..............#.", ".############.#.", ".7S#..4.....4.#.", ".####.#.#5#.#.#.", "....6.....6...#.", "#4#.#.#7#.#4#9#.", "5...7.6.......#.", "#.#.#4#7#4###1#.", "S3....4S2.....#."}

    Returns: "impossible"

  17. {".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".1111111111111111111", ".111111111111111111."}

    Returns: "impossible"

  18. {".1111111111111111111", "..111111111111111111", ".1.11111111111111111", ".11.1111111111111111", ".111.111111111111111", ".1111.11111111111111", ".11111.1111111111111", ".111111.111111111111", ".1111111.11111111111", ".11111111.1111111111", ".111111111S111111111", ".1111111111.11111111", ".11111111111.1111111", ".111111111111.111111", ".1111111111111.11111", ".11111111111111.1111", ".111111111111111.111", ".1111111111111111.11", ".11111111111111111.1", ".111111111111111111."}

    Returns: "possible"

  19. {".1111111111111111111", "..111111111111111111", ".1.11111111111111111", ".11.1111111111111111", ".111.111111111111111", ".1111.11111111111111", ".11111.1111111111111", ".111111.111111111111", ".1111111.11111111111", ".11111111.1111111111", ".111111111.111111111", ".1111111111S11111111", ".11111111111.1111111", ".111111111111.111111", ".1111111111111.11111", ".11111111111111.1111", ".111111111111111.111", ".1111111111111111.11", ".11111111111111111.1", ".111111111111111111."}

    Returns: "impossible"

  20. {".1111111111111111111", "..111111111111111111", ".1.11111111111111111", ".11.1111111111111111", ".111.111111111111111", ".1111.11111111111111", ".11111.1111111111111", ".111111.111111111111", ".1111111.11111111111", ".11111111S1111111111", ".111111111.111111111", ".1111111111.11111111", ".11111111111.1111111", ".111111111111.111111", ".1111111111111.11111", ".11111111111111.1111", ".111111111111111.111", ".1111111111111111.11", ".11111111111111111.1", ".111111111111111111."}

    Returns: "impossible"

  21. {".111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "1111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111S1111111111."}

    Returns: "impossible"

  22. {".111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "1111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "111111111S111111111."}

    Returns: "possible"

  23. {".111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111119211111111", "11111111111111111111", "11111111111111111111", "1111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "111111111S111111111."}

    Returns: "possible"

  24. {".111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111129211111111", "11111111111111111111", "11111111111111111111", "1111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "111111111S111111111."}

    Returns: "impossible"

  25. {".111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111129111111111", "11111111111111111111", "11111111111111111111", "1111111111S111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "11111111111111111111", "111111111S111111111."}

    Returns: "possible"

  26. {"..111111111111......", "#######.#######..2..", "#######....####.....", "#######2##.####.....", "...............####.", "111111111111111###..", ".................#.#", ".555555555555...#..#", "................#..#", "................#..#", "...............#...#", "...............#...#", "...............#...#", "................##..", "................##..", "................##..", "................##..", "..5555..........##..", "....S...........##..", "................##.." }

    Returns: "possible"

  27. {".1.#.", ".9.#.", ".###.", "2#.1.", ".#.7." }

    Returns: "impossible"

  28. {".727.", "##S#." }

    Returns: "possible"

  29. {"..111111111111..", "#######S#######." }

    Returns: "possible"

  30. {".999999999", "999999999." }

    Returns: "impossible"

  31. {".#S#.", ".9.9." }

    Returns: "possible"

  32. {".#...", ".#.#.", "...#." }

    Returns: "possible"

  33. {".99", "999", "99." }

    Returns: "impossible"

  34. {"....", "....", "...." }

    Returns: "possible"

  35. {"..", "9.", "1.", ".#", ".." }

    Returns: "possible"

  36. {".#", "#." }

    Returns: "impossible"

  37. {"..111111111111..", "###############." }

    Returns: "impossible"

  38. {".####", "####.", ".....", ".####", ".....", "....." }

    Returns: "impossible"

  39. {".99S45S54S45S54S45S5", "99S45S54S45S54S45S54", "9S45S54S45S54S45S54S", "S45S54S45S54S45S54S4", "45S54S45S54S45S54S45", "5S54S45S54S45S54S45S", "S54S45S54S45S54S45S5", "54S45S54S45S54S45S54", "4S45S54S45S54S45S54S", "S45S54S45S54S45S54S4", "45S54S45S54S45S54S45", "5S54S45S54S45S54S45S", "S54S45S54S45S54S45S5", "54S45S54S45S54S45S54", "4S45S54S45S54S45S54S", "S45S54S45S54S45S54S4", "45S54S45S54S45S54S45", "5S54S45S54S45S54S45S", "S54S45S54S45S54S45S5", "54S45S54S45S54S45S5." }

    Returns: "impossible"

  40. {"..#.#.", "..#.#.", "#5#.5.", "....#." }

    Returns: "impossible"

  41. {"....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", ".................999", ".................999", ".................99." }

    Returns: "impossible"

  42. {".SSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSSS", "SSSSSSSSSSSSSSSSSS." }

    Returns: "possible"

  43. {".S111111", "11111111", "11111111", "11111111", "11111111", "11111111", "1111111." }

    Returns: "impossible"

  44. {".416.", ".#.#.", "..S#." }

    Returns: "possible"

  45. {".#..", "##..", "S..." }

    Returns: "impossible"

  46. {".##", "#.#", "##." }

    Returns: "impossible"

  47. {".2S", ".#8", "99." }

    Returns: "possible"

  48. {".99S9.", "#####." }

    Returns: "impossible"


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)2024, TopCoder, Inc. All rights reserved.
This problem was used for: