YCOJ Guohe Cu C ++

Guohe Cu is a relatively simple ~ ~ The problem can be done with recursion or dynamic programming, but today is not primarily recursion or dynamic programming, but with the depth-first search to do.Although there will be two sets of TLE ~~

Deep down search is a search algorithm (as shown) Here Insert Picture Description
all paths it can effectively statistics midpoint to the starting point, the difference is that BFS, BFS (broad search) is one level search, and DFS (deep search) is down search, and to the border and then back, and then search the other side. So, BFS is used to find the shortest path, while the total number of statistics for DFS path.

Now, let's look deep search of ideas Guohe Cu.

Description
on the board there is a Guohe Cu point A, point B needs to go to the goal. Death walking rules: can be down, or right. At the same time there is a certain point of the other horses (e.g. point C) on the board, the point where the horse jumps one step up and all the other point is called a control point of the horse, as shown in point C in P1 and 3-1 , ......, P8, and death can not control the other points of the horse. Chessboard coordinate representation, A point (0,0), B point (n, m) (n, m is an integer of not more than 20), the position coordinates of the same horse is given needs, C ≠ A and C ≠ B . Now you calculate the required number of paths can reach the stroke from point A to point B.

Input

Coordinates are given n, m and the point C.
Output

From point A to reach the number of path B points.
Here Insert Picture Description

Sample Input 1
8 6 0 4
Sample Output 1
1617
——摘自YCOJ

First, we can think of using two-dimensional array to represent the control point in the running direction of soldiers and horses.

Traveling direction and control points as shown in FIG horse soldiers:
()
(Death and blue represents the traveling direction, the control point on behalf of soldiers who can not leave the red horse, green for midpoint)

Thus, because the soldiers can only take two squares, and the horse has eight control points, so the code is as follows:

int dir[2][2] = {{1,0},{0,1}};
int die[8][2]={{-1,2},{-1,-2},{1,2},{1,-2},{2,-1},{-2,-1},{2,1},{-2,1}};

Then, finally have a boundary value, then define a boundary value:

bool in(int x,int y){
    return 0<=x && x<=n && 0<=y && y<=m;
}

Now, to the body of code, DFS. When Death met Ma, you should go back one step back, and then determine the next step (Figure)

Here Insert Picture Description

void  dfs(int x, int y){
    if(x == Tx&&y == Ty){
        sum++;//统计
        return ;
    }   
        
    for(int i=0;i<2;i++){ 
        int tx = x+dir[i][0];
        int ty = y+dir[i][1];
        if(in(tx, ty) && !mp[tx][ty] && !vis[tx][ty]){//判断马的控制点
            if(tx == Tx&& y == Ty){
                sum++;
            }else{
                vis[tx][ty]=1;//标记      
                dfs(tx,ty);
                vis[tx][ty]=0;//解除标记
            }
            
        }
    }
}

Search on these deep, but still a little horse and boundary values:

for (int i=0;i<8;i++){
        int tx=x1+die[i][0];
        int ty=y1+die[i][1];
        if(in(tx,ty)){
            mp[tx][ty]=1;
        }
    }

最后上代码整体:
#include<bits/stdc++.h>
using namespace std;
bool mp[100][100];
bool vis[110][110];
int n,m;
int Sx,Sy ,Tx,Ty,sum = 0;
int dir[2][2] = {{1,0},{0,1}};
int die[8][2]={{-1,2},{-1,-2},{1,2},{1,-2},{2,-1},{-2,-1},{2,1},{-2,1}};
bool in(int x,int y){
    return 0<=x && x<=n && 0<=y && y<=m;
}

void  dfs(int x, int y){
    if(x == Tx&&y == Ty){
        sum++;
        return ;
    }   
        
    for(int i=0;i<2;i++){ 
        int tx = x+dir[i][0];
        int ty = y+dir[i][1];
        if(in(tx, ty) && !mp[tx][ty] && !vis[tx][ty]){
            if(tx == Tx&& y == Ty){
                sum++;
            }else{
                vis[tx][ty]=1;      
                dfs(tx,ty);
                vis[tx][ty]=0;
            }
            
        }
    }
}


    
int main(){
    int x1,y1;
    cin >> n >> m>>x1>>y1;
    mp[x1][y1]=1;
    for (int i=0;i<8;i++){
        int tx=x1+die[i][0];
        int ty=y1+die[i][1];
        if(in(tx,ty)){
            mp[tx][ty]=1;
        }
    }
    Tx=n;
    Ty=m;
    vis[Sx][Sy] = 1;
    dfs(Sx,Sy);
    cout <<sum;
    return 0;
}

However, although the sample over, but there will be two sets of TLE, so you have to use pruning, but pruning is not playing. DFS Guohe Cu whole idea is like this.

Guess you like

Origin www.cnblogs.com/A-Konnyaku/p/10993806.html