#include #include using namespace std;

struct Person { int x, y, d; };

bool canMove(int n, int x, int y, int d, vector<vector>& visited) { if (x < 0 || x >= n || y < 0 || y >= n || visited[x][y]) { return false; } return true; }

void move(int& x, int& y, int d) { if (d == 0) { y++; } else if (d == 1) { x++; } else if (d == 2) { y--; } else if (d == 3) { x--; } }

int main() { int t; cin >> t;

for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    
    vector<vector<bool>> visited(n, vector<bool>(n, false));
    
    Person lin, hua;
    cin >> lin.x >> lin.y >> lin.d;
    cin >> hua.x >> hua.y >> hua.d;
    
    visited[lin.x][lin.y] = true;
    visited[hua.x][hua.y] = true;
    
    while (true) {
        move(lin.x, lin.y, lin.d);
        move(hua.x, hua.y, hua.d);
        
        if (!canMove(n, lin.x, lin.y, lin.d, visited)) {
            lin.d = (lin.d + 1) % 4;
        }
        
        if (!canMove(n, hua.x, hua.y, hua.d, visited)) {
            hua.d = (hua.d + 3) % 4;
        }
        
        if (visited[lin.x][lin.y] && visited[hua.x][hua.y]) {
            cout << lin.x << ' ' << lin.y << endl;
            break;
        }
        
        visited[lin.x][lin.y] = true;
        visited[hua.x][hua.y] = true;
    }
}

return 0;

}

C++实现两人在矩形方格中相遇位置的算法

原文地址: https://www.cveoy.top/t/topic/qvnQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录