Solution to a problem P1556 [path] happy

Portal

The essence of the question is not difficult, probably also the question of the level of yellow, dfs can.(I will not tell you that I submitted three times before AC)

Arriving at a cow (dfs next) conditions:

1, equal to the current x-axis or bovine y-axis coordinates.

2, need to turn to arrive .

3, had not been.

The first focuses on the second condition, how to determine whether there is a turn? As used herein, a function, by determining the start point and the target point on the return line direction:

inline int Dire ( int X, int Y, int X1, int Y1) {
     // because the conditions 1, it is not only the magnitude relationship determined in the same axis. 
    IF (X <X1) return  . 1 ;
     IF (X> X1) return  2 ;
     IF (Y <Y1) return  . 3 ;
     IF (Y> Y1) return  . 4 ; 
}

The second priority is to determine whether the path. All point to define an array of bool, whether the record has passed. At the beginning of dfs function to determine whether all the points have been through, if established, determine whether it can return to the origin after the turn . If it still holds, the counter is incremented.

Last code is as follows:

#include <cstdio>
#include <cstring>
using namespace std;
struct point {int x, y;} p[11];
int n, ans = 0;
bool b[11], c;
inline int dire(int x, int y, int x1, int y1) {
    if (x < x1) return 1;
    if (x > x1) return 2;
    if (y < y1) return 3;
    if (y > y1) return 4;
}
void dfs(int x, int y, int d) {
    c = true;
    for (register int i = 0; i < n; i++)
        if (b[i]) {
            c = false; break;
        } 
    if (c && (x == 0 || y == 0) && dire(x, y, 0, 0) != d)  {
        ans++; return;
    }
    for (register int i = 0; i < n; i++)
        if ((p[i].x == x || p[i].y == y) && b[i] && dire(x, y, p[i].x, p[i].y) != d) {
            b[i] = false;
            dfs(p[i].x, p[i].y, dire(x, y, p[i].x, p[i].y));
            b[i] = true;
        }
}
int main() {
    memset(b, true, sizeof(b));
    scanf("%d", &n);
    for (register int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y);
    dfs(0, 0, 0);
    printf("%d\n", ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/mhhx/p/11634960.html