On a grid of at most 5 by 5, simulate daily simultaneous attacks where each AI nation hits its strongest surviving neighbor, and choose one attack per day to survive as long as possible.
Medium7SimulationGreedyBrute forceImplementationNo attempts yetTime limit5sMemory limit512 MBThe political balance of the world has collapsed, and every nation has declared war on every other nation. The only thing left to hope for is to survive as long as possible.
Every industrial center has already been destroyed, so the only way to attack is to hurl conscripted soldiers at a neighbor. That limits each nation to attacking the nations immediately next to it. The world is an R×C grid. Rows are numbered from 1 in the far north to R in the far south, and columns from 1 in the far west to C in the far east. Each nation occupies one square of the grid, so it has at most 4 neighbors.
Every nation starts with a strength that everyone knows. The other nations have no concept of strategy, so each morning a nation picks the surviving neighbor with the highest strength. A tie goes to the northernmost of those nations, and then to the westernmost. It sends an army whose power equals its own current strength S at that neighbor, and by the end of the day the target's strength drops by S. A nation with no surviving neighbor attacks nobody. A nation whose total damage for the day is at least its own strength ends the day at strength 0 and is destroyed, and from the next day on it neither attacks nor can be attacked.
All attacks happen at the same time, and an army's power comes from the strength its nation had that morning. A nation destroyed on a given day still carries out its own attack on that day.
Your nation is in column c, row r, and it listens to your advice, so it does not have to follow that strategy. Each day you may pick one surviving neighbor and attack it, or do nothing at all. You cannot attack two neighbors at once, and you cannot send an army weaker than your current strength.
You survive a day if your nation's strength is above 0 when the day ends. Find the largest number of days you can survive.
The first line contains the number of test cases, T. T test cases follow.
The first line of each test case contains four integers C, R, c, and r. The next R lines contain C integers each. The ci-th number on the ri-th of those lines is the starting strength Sci,ri of the nation in column ci and row ri. That value may be 0, which means the nation has already been destroyed. Your nation's starting strength is not 0.
Limits
For each test case, print one line containing Case #A: followed by:
B day(s), where B is the largest number of days you can survive.forever, if you can outlast all your neighbors.A is the test case number, starting from 1. Write day(s) exactly that way, including when B is 0 or 1.