Orange Boy Can You Solve It Out? Ep. 55

思考题 with hard-to-write checker and interactor

Peripheral Lightup!

Note: this is not a traditional problem. Follow the instruction in the statement to help construct your solution

Lead-in: The Palace is celebrating a festival with LED lights! Instead of connecting all LED lights with data wires and using a master computer to control them, Ninetail decided to use short-sighted micro computers!

You are given a N*M grid layout of _the LED-wall_ consisting of chars. Each char can be '.','#' or '$'.

  • '.' is called a blank-space. They cannot be lighted and are always black. They don't process codes either.
  • '#' is called a sub-computer. They can be in two states: not lighted or lighted. They have the ability to process codes and send messages to adjacent computers.
  • '$' is called a master-computer. They can be in two states as well: not lighted or lighted. They have the ability to comprehend the expected outcome of the program, process codes and send messages to adjacent(adjacent-4) computers.

Ninetail has given you the expected outcome of the grids, requiring some parts of the grid to be lighted and some others aren't. The expected outcome is also a N*M grid of chars. Each char can be '.' or '#', representing not lighted and lighted respectively.

The following conditions hold:

  • All computers are connected(adjacent-4).
  • All '.' in the layout are also '.' in expected outcome. (aka blank space won't be lit up)
  • Only 1 master computer exists in the layout.
  • All computers are not lighted by default

You are given the following APIs(Let's assume you use C++):

  • bool send(string direction, int message): send a message to an adjacent computer. Direction can be 'left' 'up' 'right' 'down'. If there is no computer at that direction, this method return false, otherwise it returns true. Calling this method and returning true once costs 1 gold (in other words failed sending wont cost)

  • bool lightUp(): toggle the lighting state of the current computer. Returns the current lighting state after toggling.

Please implement the following methods:
- void onReceive(int message): This method is called when send is called in an adjacent computer.
- void startup(vector<string> layout, vector<string> expectedOutcome): This method is called for the master computer once the task begins. The format of the arguments are same as above.

Please note that:
- Do not write a main function in your solution.
- Your onReceive method will be applied to all computers. That means:
- It's intended that you do not implement different onReceive for different computers. You implement one uniform method for all computers.
- It's intended that the method will NOT know the location of the computer that is running it nor the sender of the message.
- Your method will NOT know the expected outcome, the layout,
- This method will be called on master computer as well.
- Your method cannot access variables defined by other computer instances. Though you can still use global variables to store information used only by this computer.
- The onReceive method is called in the order of the invocations of send (aka like in BFS)
- startUp will only be called once. Its host is the master computer.
- Your solution will be judged on the total gold cost according to the scoringFormula. However, if your finally light output is different from the expected outcome, you will be given 0 points.


Of course, this solution is not optimal.

Example of the example

If layout=$##
and expectedOutcome=#.#

Step 1. In master.startup, lightUp() is called for master. Now=#..
Step 2. In master.startup, master(computer1) sends computer2(right) 0.
Step 3. In master.startup, master(computer1) sends computer2(right) another 1.
Step 4. In computer2.onReceive(arg='0'), because firstMessage=true, computer2 will not light up nor forward the message. Now=#..
Step 5. In computer2.onReceive(arg='1'), because firstMessage=false, computer2 sends computer3(right) 1
Step 6. In computer3.onReceive(arg='1'), because firstMessage=true, computer3 lights up. Now=#.#
Step 7. Finish


1\leq N,M\leq 30

来源:Hell Hole Studios Blog

< <上一篇
目 录