IOI 2009:Mecho
IOI2009 Mecho
64-bit integer IO format: %lld Java class name: Main
Mecho the bear has found a little treasure - the bees' secret honeypot, which is full of honey! He was happily eating his newfound treasure until suddenly one bee saw him and sounded the bee alarm. He knows that at this very moment hordes of bees will emerge from their hives and start spreading around trying to catch him. He knows he has to leave the honeypot and go home quickly, but the honey is so sweet that Mecho doesn't want to leave too soon. Help Mecho determine the latest possible moment when he can leave.
Mecho's forest is represented by a square grid of by unit cells, whose sides are parallel to the north-south and east-west directions. Each cell is occupied by a tree, by a patch of grass, by a hive or by Mecho's home. Two cells are considered adjacent if one of them is immediately to the north, south, east or west of the other (but not on a diagonal). Mecho is a clumsy bear, so every time he makes a step, it has to be to an adjacent cell. Mecho can only walk on grass and cannot go through trees or hives, and he can make at most steps per minute. At the moment when the bee alarm is sounded, Mecho is in the grassy cell containing the honeypot, and the bees are in every cell containing a hive (there may be more than one hive in the forest). During each minute from this time onwards, the following events happen in the following order:
- If Mecho is still eating honey, he decides whether to keep eating or to leave. If he continues eating, he does not move for the whole minute. Otherwise, he leaves immediately and takes up to steps through the forest as described above. Mecho cannot take any of the honey with him, so once he has moved he cannot eat honey again.
- After Mecho is done eating or moving for the whole minute, the bees spread one unit further across the grid, moving only into the grassy cells. Specifically, the swarm of bees spreads into every grassy cell that is adjacent to any cell already containing bees. Furthermore, once a cell contains bees it will always contain bees (that is, the swarm does not move, but it grows).
In other words, the bees spread as follows: When the bee alarm is sounded, the bees only occupy the cells where the hives are located. At the end of the first minute, they occupy all grassy cells adjacent to hives (and still the hives themselves). At the end of the second minute, they additionally occupy all grassy cells adjacent to grassy cells adjacent to hives, and so on. Given enough time, the bees will end up simultaneously occupying all grassy cells in the forest that are within their reach. Neither Mecho nor the bees can go outside the forest. Also, note that according to the rules above, Mecho will always eat honey for an integer number of minutes. The bees catch Mecho if at any point in time Mecho finds himself in a cell occupied by bees.
Task
Write a program that, given a map of the forest, determines the largest number of minutes that Mecho can continue eating honey at his initial location, while still being able to get to his home before any of the bees catch him.
Constraints
- the size (side length) of the map
- the maximum number of steps Mecho can take in each minute
Input
The input contains several testcases.
The fist line contains the number of testcase T.
Each testcase has the form as following:
- The first line contains the integers and , separated by a space.
- The next lines represent the map of the forest. Each of these lines contains characters with each character representing one unit cell of the grid. The possible characters and their associated meanings are as follows:
T denotes a tree G denotes a grassy cell M denotes the initial location of Mecho and the honeypot, which is also a grassy cell D denotes the location of Mecho's home, which Mecho can enter, but the bees cannot. H denotes the location of a hive
Note: It is guaranteed that the map will contain exactly one letter M, exactly one letter D and at least one letter H. It is also guaranteed that there is a sequence of adjacent letters Gthat connects Mecho to his home, as well as a sequence of adjacent letters G that connects at least one hive to the honeypot (i.e., to Mecho's initial location). These sequences might be as short as length zero, in case Mecho's home or a hive is adjacent to Mecho's initial location. Also, note that the bees cannot pass through or fly over Mecho's home. To them, it is just like a tree.
Output
For each test , your program must write to standard output a single line containing a single integer: the maximum possible number of minutes that Mecho can continue eating honey at his initial location, while still being able to get home safely. If Mecho cannot possibly reach his home before the bees catch him, the number your program writes to standard output must be instead.
Grading
For a number of tests, worth a total of 40 points, will not exceed 60.
Example
For the input data:
1
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
the correct result is:
1
Explanation of the example: After eating honey for one minute, Mecho can take the shortest path directly to the right and he will be home in another two minutes, safe from the bees.
For the input data:
1
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
the correct result is:
2
Explanation of the example: After eating honey for two minutes, Mecho can take steps during the third minute, then steps during the fourth minute and steps during the fifth minute.
Source
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 const int INF=233333333; 7 char map[810][810]; 8 int dis[810][810]; 9 int Time[810][810]; 10 int n,k; 11 struct Data{ 12 int x,y; 13 }S; 14 void P() 15 { 16 queue<Data>q; 17 for(int i=1;i<=n;i++) 18 for(int j=1;j<=n;j++) 19 if(map[i][j]=='H') 20 q.push((Data){i,j}); 21 else if(map[i][j]!='T') 22 dis[i][j]=INF; 23 24 while(!q.empty()) 25 { 26 Data node=q.front();q.pop(); 27 if((map[node.x+1][node.y]=='G'||map[node.x+1][node.y]=='M') 28 &&dis[node.x+1][node.y]>dis[node.x][node.y]+1) 29 dis[node.x+1][node.y]=dis[node.x][node.y]+1,q.push((Data){node.x+1,node.y}); 30 if((map[node.x-1][node.y]=='G'||map[node.x-1][node.y]=='M') 31 &&dis[node.x-1][node.y]>dis[node.x][node.y]+1) 32 dis[node.x-1][node.y]=dis[node.x][node.y]+1,q.push((Data){node.x-1,node.y}); 33 if((map[node.x][node.y+1]=='G'||map[node.x][node.y+1]=='M') 34 &&dis[node.x][node.y+1]>dis[node.x][node.y]+1) 35 dis[node.x][node.y+1]=dis[node.x][node.y]+1,q.push((Data){node.x,node.y+1}); 36 if((map[node.x][node.y-1]=='G'||map[node.x][node.y-1]=='M') 37 &&dis[node.x][node.y-1]>dis[node.x][node.y]+1) 38 dis[node.x][node.y-1]=dis[node.x][node.y]+1,q.push((Data){node.x,node.y-1}); 39 } 40 return; 41 } 42 43 bool DFS(int t) 44 { 45 if(t>=dis[S.x][S.y])return false; 46 queue<Data>q; 47 q.push((Data){S.x,S.y}); 48 for(int i=1;i<=n;i++) 49 for(int j=1;j<=n;j++) 50 Time[i][j]=INF; 51 Time[S.x][S.y]=t*k+1; 52 53 while(!q.empty()) 54 { 55 Data node=q.front();q.pop(); 56 int nt=Time[node.x][node.y]; 57 if(dis[node.x+1][node.y]>nt/k&&Time[node.x+1][node.y]>nt+1){ 58 Time[node.x+1][node.y]=nt+1;q.push((Data){node.x+1,node.y}); 59 if(map[node.x+1][node.y]=='D') 60 return true; 61 } 62 63 64 if(dis[node.x-1][node.y]>(nt)/k&&Time[node.x-1][node.y]>nt+1){ 65 Time[node.x-1][node.y]=nt+1,q.push((Data){node.x-1,node.y}); 66 if(map[node.x-1][node.y]=='D') 67 return true; 68 } 69 70 71 if(dis[node.x][node.y+1]>(nt)/k&&Time[node.x][node.y+1]>nt+1){ 72 Time[node.x][node.y+1]=nt+1,q.push((Data){node.x,node.y+1}); 73 if(map[node.x][node.y+1]=='D') 74 return true; 75 } 76 77 78 if(dis[node.x][node.y-1]>(nt)/k&&Time[node.x][node.y-1]>nt+1){ 79 Time[node.x][node.y-1]=nt+1,q.push((Data){node.x,node.y-1}); 80 if(map[node.x][node.y-1]=='D') 81 return true; 82 } 83 } 84 return false; 85 } 86 int main() 87 { 88 scanf("%d%d",&n,&k); 89 for(int i=1;i<=n;i++) 90 scanf("%s",map[i]+1); 91 92 for(int i=1;i<=n;i++) 93 for(int j=1;j<=n;j++) 94 if(map[i][j]=='M'){ 95 S.x=i;S.y=j; 96 } 97 P(); 98 int lo=0,hi=10000; 99 while(lo<=hi) 100 { 101 int mid=(lo+hi)>>1; 102 if(DFS(mid))lo=mid+1; 103 else hi=mid-1; 104 } 105 printf("%d\n",hi); 106 return 0; 107 }