Risk
InterviewTime limit3sMemory limit128 MB
not used
- Level
Medium7 of 10
- Topics
- Graph
- Solved
- No attempts yet
Problem
Risk is a famous board game played on a map of the world. The world is divided by borders into several territories, and each territory belongs to exactly one player. Every territory you own must always be garrisoned by at least one of your armies.
On your turn, you may leave each army where it is or move it one step into an adjacent territory that you own. A single army may move only once, from the territory it occupies at the start of your turn to an adjacent territory; it cannot travel two or more steps in a row. You may, however, perform such moves for as many armies as you like, and you may split the armies of one territory among several destinations. You may never move every army out of a territory, leaving it with a garrison of 0. If you have no army to move, you may simply pass.
A common strategy is to mass as many armies as possible in the territories that touch enemy territory. This turn you want to make your weakest territory, the enemy-adjacent territory holding the fewest armies, as strong as possible. When your turn ends, what is the largest number of armies that can be garrisoned in your weakest territory?
Input
The first line contains the number of test cases ().
Each test case is given as follows.
- The first line contains the total number of territories ().
- The next line contains integers (), where is the number of your armies garrisoned in territory . If is , that territory is held by the enemy.
- Then lines follow, each consisting of characters 'Y' or 'N'. The -th character of the -th line is 'Y' if territories and are adjacent and 'N' otherwise. The adjacency is always symmetric, and the -th character of the -th line is 'N' for every .
In every test case you own at least one territory, the enemy owns at least one territory, and at least one of your territories is adjacent to an enemy territory.
Output
For each test case, print on its own line the largest number of armies that can be garrisoned in the weakest territory (the enemy-adjacent territory of yours that holds the fewest armies).