c++题解,代码没有注释,可从上网查询,输出必须符合样例,本题有8个测试点,请得满分。
T-2 Breaking Through
分数 35
作者 陈越
单位 浙江大学
In a war game, you are given a map consists of n×n square blocks. Starting from your current block, your task is to conquer a destination block as fast as you can. The difficulties are not only that some of the blocks are occupied by enemies, but also that they are shooting in some directions. When one is shooting in some direction, the whole row/column in that direction will be covered by fire, unless there is another enemy blocking the way. You must make sure that you are not shot on the way to your destination.
However, it is very much likely, at the very beginning, that this task is impossible. So you must follow the following instructions:
Step 1: find the shortest path from the starting block to the destination, avoiding all the enemy blocks. The path length is the number of blocks on the way, not including the starting block.
If this path is not unique, select the smallest index sequence – that is, all the blocks are indexed from 1 to n
2
, starting from the upper-left corner, and row by row till the lower-right corner. An index sequence is an ordered sequence of the block indices from the starting block to the destination. One sequence { s,u
1
,⋯,u
k
,d } is said to be smaller than { s,v
1
,⋯,v
k
,d }, if there exists 1≤p≤k such that u
i
=v
i
for i<p and u
p
<v
p
.
Step 2: if some of the blocks along the selected shortest path is covered by fire, conquer and clear the nearest reachable enemy block that are firing at the path (in case that such a block is not unique, take the one with the smallest index). Then take that block as the starting position, goto step 1. Keep in mind that you must always make sure that you are not shot on the way.
If step 2 is impossible, then the game is over. Otherwise, keep going until you finally conquer the destination.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer n (3≤n≤100), which is the size of the map. Then n lines follow, each gives n numbers. Each number describes the status of the corresponding block:
0 means the block is clear;
1, 2, 3, or 4 means the block is occupied by an enemy, who will shoot toward up, down, left, and right, respectively;
5 means the block is your starting position;
6 means the block is your destination.
All the numbers in a line are separated by a space.
Note:
You can never cross any of the boundaries of the map.
When an enemy is shooting from block A in direction B, every block starting from A in direction B will be covered by fire, untill the boundary is reached, or another enemy block is encountered.
The enemies will never kill each other. Only you will get killed if you step into a block that is covered by fire.
It is guaranteed that there is no more than n enemy blocks, and your starting position is not on fire.
Output Specification:
Print in a line the path length for you to reach the last block from your starting position, and the block number. The two numbers must be separated by 1 space.
In the next line, print Win if the destination is reached, or Lose if not.
Sample Input 1:
6
6 2 0 0 0 0
0 0 0 0 0 0
0 4 0 0 0 0
0 0 0 0 1 0
0 0 0 3 5 0
4 0 0 0 0 0
Sample Output 1:
10 1
Win
Hint:
The movements are shown by the following figures 14.
Figure 1 shows how the blocks are numbered.
Figure 2 shows the initial status of the map, where the player’s position is green, enemy blocks are black, and red blocks are covered by fire.
At the very beginning the shortest path was 29->30->24->18->12->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 23, 14 and 2. Since the enemy block 23 is the nearest reachable one, it was cleared with path length 1.
Now starting from block 23, the shortest path was 23->17->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 14 and 2. Since the enemy block 14 is covered by fire from block 2, taking block 2 now is the only option. So next, block 2 is cleared with path length 8 (crossing the destination).
Finally we can get to the destination from block 2 to 1.
Sample Input 2:
4
5 0 2 0
0 0 0 0
0 1 0 0
4 0 6 3
Sample Output 2:
6 3
Lose
Hint:
The movements are shown by the following figures 5~8.
Figure 5 shows how the blocks are numbered.
Figure 6 shows the initial status of the map.
At the very beginning the shortest path was 1->2->6->7->11->15, but the path was covered by fire shooting from blocks 3, 10, 13 and 16. Since the enemy block 10 is the nearest reachable one, it was cleared with path length 3.
Now starting from block 10, the shortest path was 10->11->15, but the path was covered by fire shooting from blocks 3, 13 and 16. Since the enemy blocks 13 and 16 are covered by fire from each other, taking block 3 now is the only option. So next, block 3 is cleared with path length 3.
Now it is clear that there is no way to conquer the destination, since block 15 is fired by 13 and 16, yet we cannot clear any of them without getting fired. Hence the last block we can reach is 3.
steps2.png
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB