Blue Bridge Cup - Path Mystery

topic:

The Mystery of the Path
Xiao Ming pretends to be a knight from Planet X and enters a strange castle.
There is nothing in the castle, only the ground paved with square stones.


Suppose the castle floor is nxn squares. [As shown in Figure 1.png].


According to custom, the knight should walk from the northwest corner to the southeast corner.
Can move horizontally or vertically, but cannot walk diagonally or jump.
Every time you reach a new square, you have to shoot one arrow to the north and one to the west.
(There are n targets each in the west and north walls of the castle) The

same square is only allowed to pass once. But you don't have to go through all the squares.


Can you infer the knight's path given only the number of arrows on the target?


Sometimes it's OK, like the example in Figure 1.png.


The requirement of this question is to know the number of the arrow target, and find the walking path of the knight (the test data ensures that the path is unique)


Input:
an integer N (0<N<20) in the first line, indicating that there are N x N squares on the ground.
The second line N integers, separated by spaces, representing the numbers on the target in the north (from west to east)
The third row of N integers, separated by spaces, representing the numbers on the target in the west (from north to south)


Output:
several integers in one row, Represents a knight's path.


For convenience of representation, we agree that each small grid is represented by a number, numbered from the northwest corner: 0, 1, 2, 3....
For example, the number of squares in Figure 1.png is:


0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

Example:
User input:
4
2 4 3 4
4 3 3 3 The

program should output:
0 4 5 1 2 3 7 11 10 9 13 14 15

Resource convention:
Peak memory consumption < 256M
CPU consumption < 1000ms



Analysis: For this topic, my first feeling is to search deeply, that is, each time you judge the four directions that meet the conditions and then enter, and reduce the total value corresponding to the corresponding x and y by one. At the same time, the path of the point is stored in the array, and the end condition is when the point at the bottom corner is reached, and the values ​​corresponding to all x and y are 0, and the true value is returned at this time. If the current deep search path does not meet the conditions, it returns false, and at the same time restores the previously reduced values ​​of x and y to the original values. If the four directions of the current point cannot go, then delete the last value of the current array. In general, if the deep search fails, the previous layer that has been revoked will continue the deep search. until the right path is found.

Code:

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int Lx[20],Ly[20];//Count all the values ​​corresponding to x, y, that is, the number of arrows on the target
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};//Four directions
struct Node
{
	bool flag;//marks whether to walk through
	int x,y;//The coordinates of the current node
};
Node arr[20][20];
vector<int> v;
int n;
bool dfs(int x, int y)
{	
	//Determine whether the last point is reached and whether it is a valid path
	if (x==n-1 && y==n-1)
	{
		for(int i =0;i<n;i++)
		{
			if (Lx[i] || Ly[i])
				return false;
			v.push_back(x*n+y);
			return true;
		}
	}
	
	// record the current point at the end of the vector
	v.push_back(x*n+y);
	arr[x][y].flag=1;
	int tx, ty;
	// Traverse four directions
	for (int i =0;i<4;i++)
	{
		tx =x+dir[i][0];
		ty = y + dir [i] [1];
		//whether the next point is a valid position
		if (tx<0 || tx>=n || ty<0 || ty >=n)continue;
		//Whether the next point has entered, and whether the arrow on the target is satisfied
		if (!arr[tx][ty].flag && (Lx[tx]>=1 &&Ly[ty]>=1))
		{
			//Deep search, stop if a matching path is found, otherwise
			//Restore the number of arrows on the target
			Lx [tx] -; Ly [ty] -;
			if (dfs(tx,ty))
				return true;
			else
			{
				Lx [tx] ++; Ly [ty] ++;
			}
		}	
	}
	//When the four directions are not satisfied
	//Set the point as unvisited and erase the recorded path
	arr[x][y].flag=0;
	v.erase(v.begin()+v.size()-1);
	return false;
}
intmain()
{
	//ifstream cin("a.txt");

	cin>>n;
	for (int i=0;i<n;i++)
		cin>>Ly[i];
	 for (int i=0;i<n;i++)
		cin>>Lx[i];
	//initialization
	for (int i=0;i<n;i++)
	{
		for (int j =0; j<n;j++)
		{
			arr[i][j].flag=0;
			arr[i][j].x=i;
			arr[i][j].y=j;
		}
	}
	Lx[0]--;Ly[0]--;
	dfs(0,0);
	for (int i=0 ; i< v.size();i++)
		cout<< v[i]<<endl;
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324640293&siteId=291194637