Day2-J- escape the maze -HDU-1728

Given an m × n (m row, n-column) labyrinth, maze two positions, Gloria would go from one position to another maze, of course, places some space maze, Gloria can pass through in some places obstacles, she must go around, from a position of the maze, and it can only go four adjacent positions, of course, during walking, gloria can not go outside to go to the maze. Headache is, gloria is no sense of direction, and therefore, she was walking, you can not turn a bend too much, otherwise she would faint. We assume two positions are given space, initially, gloria direction faced yet, she can choose any one of four directions of departure, and not turn into one. gloria from one location to another location you went to?

Input conduct a first integer t (1 ≤ t ≤ 100) , indicates the number of test data, the next set of test data is t, each test, the 
  first two acts integers m, n (1 ≤ m, n ≤ 100), respectively, number of rows and columns of the maze, the next m rows, each row comprising n characters, where the characters '' represents the position of a space, the character '*' indicates that the obstacle position, the input data only two characters, each set of test data of the last row of five integers K, X  . 1 , Y  . 1 , X  2 , Y  2  (. 1 ≤ K ≤ 10, X ≤. 1 . 1 , X  2  ≤ n-,. 1 ≤ Y  . 1 , y  2  ≤ m), where k represents a number of revolutions up to bend gloria, (X  . 1 , y  . 1 ), (X  2 , y 2 ) represent two positions, wherein X  . 1 , X  2 corresponding column, y  . 1 , Y  2 corresponding row. 
Output test data corresponding to each row, if gloria come from one location to another location, outputs "yes", and otherwise outputs "no". Sample Input

2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

Sample Output

NO 
yes 

Analysis: Simple search BFS the shortest path, plus a turn restrictions, you can add one more condition when a heavy sentence, but whether accessed directly determine cause leakage solution, pruning conditions should be: at this point the remaining number of times toward the steering
is greater than or equal to the access point has (not referenced) of (0 tantamount to prevent the steering frequency is cut), the number of turns corresponding to the priority, the more the greater the contribution to the solution.
code show as below:
const int maxm = 150;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

struct Node {
    int x, y, turn, dire;
    Node(int _x = 0, int _y = 0, int _turn = -1,int _dire = -1):x(_x), y(_y),turn(_turn),dire(_dire) {}
};

int T, m, n, sx, sy, ex, ey, k, d[maxm][maxm][5];
string buf[maxm];

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

bool bfs() {
    memset(d, -1, sizeof(d));
    queue<Node> q;
    for (int i = 0; i < 4; ++i) {
        q.push(Node(sx, sy, k, i));
        d[sx][sy][i] = k;
    }
    while (!q.empty()){
        Node p = q.front();
        q.pop();
        if(p.x == ex && p.y == ey)
            return true;
        for (int i = 0; i < 4; ++i) {
            Node tmp = p;
            tmp.x += dx[i], tmp.y += dy[i];
            if(i == p.dire) {
                if(inside(tmp.x, tmp.y) && buf[tmp.x][tmp.y] == '.' && d[tmp.x][tmp.y][i] <= p.turn) {
                    d[tmp.x][tmp.y][i] = p.turn;
                    q.push(tmp);
                }
            } else if (tmp.turn > 0) {
                tmp.turn--, tmp.dire = i;
                if(inside(tmp.x, tmp.y) && buf[tmp.x][tmp.y] == '.' && d[tmp.x][tmp.y][i] <= p.turn) {
                    d[tmp.x][tmp.y][i] = p.turn;
                    q.push(tmp);
                }
            }
        }
    }
    return false;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &m, &n);
        for (int i = 0; i < m; ++i)
            cin >> buf[i];
        scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
        sy--, sx--, ey--, ex--;
        if(bfs())
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
View Code
 

 Although we use a steering judgment condition number is greater than equal to 0 so that the steering frequency is not cut, but added a lot of repeated points, we can continue to optimize the code, since the number of times to reduce the steering, then the contribution to the solution is also reduced, reach the same point in the same direction must be smaller than the priority of not turning, then we can make the following strategy: If you do not turn directly after advancing into the team, if the turn, just before entering the team does not turn, so turn the situation is slower than a straight step, naturally there will be differences in priorities, directly on the code experience:

const int maxm = 150;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

struct Node {
    int x, y, turn, dire;
    Node(int _x = 0, int _y = 0, int _turn = -1,int _dire = -1):x(_x), y(_y),turn(_turn),dire(_dire) {}
};

int T, m, n, sx, sy, ex, ey, k, d[maxm][maxm][5];
string buf[maxm];

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

bool bfs() {
    memset(d, 0, sizeof(d));
    queue<Node> q;
    for (int i = 0; i < 4; ++i) {
        q.push(Node(sx, sy, k, i));
        d[sx][sy][i] = k;
    }
    while (!q.empty()){
        Node p = q.front();
        q.pop();
        if(p.x == ex && p.y == ey)
            return true;
        for (int i = 0; i < 4; ++i) {
            Node tmp = p;
            if(i == p.dire) {
                tmp.x += dx[i], tmp.y += dy[i];
                if(inside(tmp.x, tmp.y) && buf[tmp.x][tmp.y] == '.' && !d[tmp.x][tmp.y][i]) {
                    d[tmp.x][tmp.y][i] = 1;
                    q.push(tmp);
                }
            } else if (tmp.turn > 0) {
                tmp.turn--, tmp.dire = i;
                if(!d[tmp.x][tmp.y][i]) {
                    d[tmp.x][tmp.y][i] = 1;
                    q.push(tmp);
                }
            }
        }
    }
    return false;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &m, &n);
        for (int i = 0; i < m; ++i)
            cin >> buf[i];
        scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
        sy--, sx--, ey--, ex--;
        if(bfs())
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
View Code

From 110ms down to 60ms, the code only on the basis of fine-tuning.

 

Guess you like

Origin www.cnblogs.com/GRedComeT/p/11229286.html