Kangaroo Mother Finds Children
Time limit per test: 1.5 seconds
Time limit all tests: 10.0 seconds
Memory limit: 256 megabytes
Mother Kangaroo can't find her baby. Her child was captured by the monster.
The mother kangaroo is now in the upper left corner of the map, and her child is in line x and y of the map. The monster wants to play a game with the mother kangaroo: he doesn't want the mother kangaroo to find her baby too quickly. The mother kangaroo can jump up and down in four directions every second (if there is no wall obstruction), and the monster will build walls in some lattices to complete a maze, so that the mother kangaroo can find her children, but not less than k seconds at the fastest.
Please design such a labyrinth.
Input
The first row contains two integers, N and m (1 < n,m < 8), representing the total number of rows and columns on the map.
The second row contains three integers x, y and K (1 < x < n,1 < y < m, x + Y > 1).
Output
Output of a map should be exactly n rows m columns.
Use * to denote open space and * to denote walls. Where is the mother of the kangaroo and where is the child?
Data are guaranteed to be knowledgeable.
Examples
2 6 1 3 4
.*.*** ......
Official Interpretation:
C. Kangaroo Mother Looking for Children
Because the data range is very small, direct search is enough. Consider DFS. For every grid to go, there can be at most one (actually the one coming) that is traveled around. All searches are over.
Proposer: There are incredibly few people who have passed.
When the game did not come out, and then read the official solution, have a train of thought.
Detailed train of thought:
Violent solution, search a path, its time from the beginning to the end is greater than or equal to k. Of course, this road is not optional. It needs to be judged by conditions. In order for mother kangaroo to follow the qualified path I searched deeply,
I need to take some measures, as follows: suppose the current position of mother kangaroo is (x,y), suppose she goes right next second, then I will set up a wall on the top, bottom and left of (x,y). In this case,
She had no choice but to go to the right, while retaining the original state of the other three directions, because the state of the three points had to be restored in retrospect. That is to say, there are four search directions, if they go in one direction,
In addition to this direction, the other directions will be blocked directly and will continue in this way until a suitable path is found. Define flag variables in deep search, mark whether a set of solutions has been found, when the first one is found to match
If flag is true, it can be returned directly. As long as a group of solutions are found, there is nothing to continue searching after finding the solution.
Meaning is also a waste of time.
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int maxn = 10; int Map[maxn][maxn]; int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; /// Search direction, right, bottom, left. int n,m; int x,y,k; bool flag; /// Used to mark whether a feasible solution has been found. /**Search with sx,sy as a new starting point, and from the starting point to (sx,sy) Step step has been taken.**/ void dfs(int sx,int sy,int step) { int a[5]; if(flag) /// There is a set of feasible solutions. return; Map[sx][sy] = 1; /// Mark 1 to indicate that the position has passed. if(sx == x && sy == y) /// Achieve the goal { if(step >= k) /// If the number of steps is greater than or equal to k, it is the solution. { /// Direct output map, flag to true for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(Map[i][j]==1 || Map[i][j]==0) printf("."); else printf("*"); } printf("\n"); } flag = true; } return; /// If you can't find a solution, you have to go back. } for(int i = 0; i < 4; i++) /// Search in four directions { int tx = sx + dir[i][0]; int ty = sy + dir[i][1]; if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&Map[tx][ty]==0) { /**In order to let mother kangaroo go tx,ty, I have to force her to go nowhere. Let her only go from (sx,sy) to (tx,ty), so exclude around (sx,sy) (tx,ty)Other points**/ for(int j = 0; j < 4; j++) { if(i == j) continue; int tempx = sx + dir[j][0]; int tempy = sy + dir[j][1]; a[j] = Map[tempx][tempy]; /// Array a is used to preserve the original state of the points around, (sx,sy), and backtracking is used. if(tempx<1||tempx>n||tempy<1||tempy>m) continue; if(Map[tempx][tempy]==0) /// Only open space can be blocked Map[tempx][tempy] = 2; } dfs(tx,ty,step+1); /// Take (tx,ty) as the starting point for deep search Map[tx][ty] = 0; /// Backtracking, to 0, means that the point has not been passed for(int j = 0; j < 4; j++) { if(i == j) continue; int tempx = sx + dir[j][0]; int tempy = sy + dir[j][1]; if(tempx<1||tempx>n||tempy<1||tempy>m) continue; Map[tempx][tempy] = a[j]; /// Restore the original state of the points around (sx,sy). } } } } int main() { while(~scanf("%d%d",&n,&m)) { scanf("%d%d%d",&x,&y,&k); memset(Map,0,sizeof(Map)); /// Representing this position is'. ' flag = false; dfs(1,1,0); } }