Dating with Girls (2) Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2821 Accepted Submission (s): 791
Problem Description If You had solved the problem Dating with girls (1). I Think you can solve this problem too. This problem was also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze. If you can find the girl and then you can date with the girl. Else the girl would date with other boys. What a pity!
The Maze is very strange. There is many stones in the maze. The stone'll disappear at time t if it's a multiple of K (2<= K <=), on the other time, stones'll be still th Ere.
There is only '. ' or ' # ', ' Y ', ' G ' on the map of the maze. '. ' Indicates the blank which you can move on, ' # ' indicates stones. ' Y ' indicates the your location. ' G ' indicates the girl ' s location. There is only one ' Y ' and one ' G '. Every seconds you can move left, right, up or down.
Input The first line contain an integer T. Then T cases followed. Each case is begins with three integers r and C (1 <= R, c <=), and K (2 <=k <= 10).
The next R line is the map ' s description.
Output for each cases, if you can find the girl, output the least time in seconds, else output "Please give me another cha Nce! ". Sample Input
1
6 6 2
... Y..
...#..
.#....
...#..
...#..
.. #G #.
Sample Output
7
Full-text translation of the personal version (available to English poor little partners and some non-English candidates 0.0):
If you have solved dating with girls (1). Spicy? I think you can solve this problem too. This question is also about sister paper. Now you are in a maze and have a sister paper you want to offer, if you can find the sister paper, then you can ask her, otherwise she will be about other boys, how the egg hurts ...
This maze is very strange, there are many stones in the maze. These stones will disappear at the T moment, the definition of T is a multiple of k, at other times the stone is still there.
There's only one guarantee. # Y G Several characters in the Maze map,. Only take you can go straight to the place, #表示石头, Y means your initial seat, G-sister paper, guaranteed only a Y and a G, every second you can go to the left, go right, go up, or go down (translation here you should understand, there is no place to move the situation OH ~~~~~~)
Ideas:
Because there are a lot of points can be repeated, so we have to open three-dimensional array vis "" "" "to judge the weight, if one dimension is used to represent X, one dimension is used to represent Y, then the other one to indicate what. Of course, the number of steps to walk, but we do not know the total number of steps we can walk AH ~ so we here to the K to seek redundancy, as long as it is not 0, all indicate that the stone exists. The rest of the part directly BFS not to be a good.
Note the point:
The points that can be walked directly are: G Y and ". "I forgot about y when I first handed in here, and I can repeat this place, so WA got a pitch.
Indirect can go to the point is: # that is the current number of steps%k==0 can go.
Finally, two sets of possible data are available:
2
2 3 9
# #.
G#y
2 4 4
Y. #G
#.##
AC Code:
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struct Zuobiao {int x,y,output;}
Now,nex;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
Char a[105][105];
int vis[105][105][12];
int n,m,k;
void BFs (int x,int y) {memset (vis,0,sizeof (VIS));
Now.x=x;
Now.y=y;
now.output=0;
Vis[x][y][0]=1;
Queue<zuobiao >s;
S.push (now);
while (!s.empty ()) {Now=s.front ();
if (a[now.x][now.y]== ' G ') {printf ("%d\n", now.output);
return;
} s.pop ();
for (int i=0;i<4;i++) {nex.x=now.x+fx[i];
Nex.y=now.y+fy[i];
nex.output=now.output+1;
printf ("%d%d%d%d\n", nex.x,nex.y,nex.output,nex.output%k,vis[nex.x][nex.y][nex.output%k]); if (nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y][ nex.output%k]==0) {if (a[nex.x][nex.y]== '. ' | | a[nex.x][nex.y]== ' G ' | |
a[nex.x][nex.y]== ' y ') {//printf ("yes\n");
Vis[nex.x][nex.y][nex.output%k]=1;
S.push (NEX); } if (a[nex.x][nex.y]== ' # ' &&nex.output%k==0) {//printf ("yes\n"
);
Vis[nex.x][nex.y][nex.output%k]=1;
S.push (NEX);
}}}} printf ("Please give me another chance!\n");
} int main () {int t;
scanf ("%d", &t);
while (t--) {int sx,sy;
scanf ("%d%d%d", &n,&m,&k);
for (int i=0;i<n;i++) {scanf ("%s", A[i]);
for (int j=0;j<m;j++) {if (a[i][j]== ' Y ') {sx=i;
Sy=j;
}}} BFS (Sx,sy); }
}