0
点赞
收藏
分享

微信扫一扫

track the movement path

J简文 2022-03-12 阅读 49
c++

#include <stdio.h>

#define MAX(a,b) a>b? a:b;
#define MIN(a,b) a<b? a:b;
int K;
int dirs[] = { 0,1,-1,1,-1 };

typedef struct {
    int sx, sy;
    int ex, ey;
    int dir;
}line_;
line_  lines[100];
//1 - 北 2 - 南 3 - 东 4 - 西
void inputdata(int i, int dir, int dist, int prex, int prey)
{
    //start point
    lines[i].sx = prex;
    lines[i].sy = prey;
    //end point
    if (dir <= 2) {   // 北或者南 
        lines[i].ey = lines[i].sy + dirs[dir] * dist;  // 起点的y 加上 距离 向北就是加  向南就是减  1*dist  -1*dist
        lines[i].ex = lines[i].sx;   // 横坐标不变
    }
    else {            //西或者东
        lines[i].ex = lines[i].sx + dirs[dir] * dist;   // 
        lines[i].ey = lines[i].sy;  // 纵坐标不变
    }
    lines[i].dir = dir;  // 记第几个点的方向
}

int checkintersect(int h, int v) { // 0 1
    int tmpy = lines[h].sy;  // 第一个点的起始点y坐标
    int tmpx = lines[v].sx;  // 第二个点的起始点x坐标
    if ((tmpx > lines[h].sx&&tmpx < lines[h].ex) || (tmpx<lines[h].sx&&tmpx>lines[h].ex)) {  //如果第二个点的起始点x坐标 > 第一个点的起始点x坐标 并且 < 第一个点的终点x坐标
        if ((tmpy > lines[v].sy&&tmpy < lines[v].ey) || (tmpy<lines[v].sy&&tmpy>lines[v].ey)) {
            return 1;
        }
    }
    return 0;
}

int checkoverlap(int v1s, int v1e, int v2s, int v2e)
{
    int v1min, v1max, v2min, v2max;

    v1max = MAX(v1s, v1e);
    v1min = MIN(v1s, v1e);
    v2max = MAX(v2s, v2e);
    v2min = MIN(v2s, v2e);
    if (v1max <= v2min || v1min >= v2max)
        return 0;
    else
        return 1;
}
int checkcrossed(int v1, int v2) // 1 0
{
    int tmpx, tmpy;
    if ((lines[v1].dir <= 2 && lines[v2].dir >= 3)) {   // 如果第一个点是向北或者南  第二个点是向西或东
        //intersect v2:horizontal v1:vertical
        if (checkintersect(v2, v1)) { // 0 1; 
            return v1 + 1;
        }
    }
    else if (lines[v1].dir >= 3 && lines[v2].dir <= 2) {  // 如果第一个点是向西或者东  第二个点是向北或南
        //intersect  v1:horizontal v2:vertical
        if (checkintersect(v1, v2)) {
            return v1 + 1;
        }
    }
    else {
        //overlap
        if ((lines[v1].dir <= 2) && (lines[v1].sx == lines[v2].sx)) {
            //v1 v2 vertical collineation    
            if (checkoverlap(lines[v1].sy, lines[v1].ey, lines[v2].sy, lines[v2].ey))
                return v1 + 1;
        }
        else if ((lines[v1].dir >= 3) && (lines[v1].sy == lines[v2].sy)) {
            //v1 v2 horizontal collineation
            if (checkoverlap(lines[v1].sx, lines[v1].ex, lines[v2].sx, lines[v2].ex))
                return v1 + 1;
        }
    }
    return -1;
}

int main(void)
{
    int t, tc;
    int i, j;
    int dir, dist;
    int answer;

//    freopen("input.txt", "r", stdin);
    scanf_s("%d", &t);
    for (tc = 0; tc < t; tc++) {
        scanf_s("%d", &K);

        //input data
        scanf_s("%d %d", &dir, &dist);
        inputdata(0, dir, dist, 0, 0);  // 把数据输入
        for (i = 1; i < K; i++) {   // 点的序号是 0 1 2 3 4  从第二个点开始输入方向和距离
            scanf_s("%d %d", &dir, &dist); 
            inputdata(i, dir, dist, lines[i - 1].ex, lines[i - 1].ey); //输入数据  前一个点的终点 就是后一个点的起点
        }
        //这里已经初始化了图

        //code 
        answer = -1;    // 初始化结果

        // 检查有没有发生碰撞
        for (i = 1; i < K; i++) {   // 分析从第一步开始
            for (j = 0; j < i; j++) {
                answer = checkcrossed(i, j); // 1 0
                if (answer != -1)
                    break;
            }
            if (answer != -1)
                break;
        }

        //print
        printf("#%d %d\n", tc + 1, answer);
    }
    return 0;
}

举报

相关推荐

0 条评论