Files
python/TangDou/XiTi/1125.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

125 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
//用于模拟的矩阵
int lin[1000][1000] = {0}; //小林走过的路
int hua[1000][1000] = {0}; //小华走过的路
int main() {
int t, n, x, y, d; // t:测试数据组数,n:是n*n的矩阵
// 上北,下南,左西,右东
// d=0 表示右; d=1 表示下;d=2 表示左;d=3 表示上
// x++ y++ x-- y--
// 小林会向右转,小华则会向左转
cin >> t;
// t次录取
for (int i = 0; i < t; ++i) {
cin >> n; // n*n矩阵
//(x,y)读入初始位置,d:一开始跑的方向
//小林
cin >> x >> y >> d;
//标识走过这个点
lin[x][y] = 1;
//第几步
int step = 1;
//模拟小林走路,小林会向右转
while (true) {
// (1)向右走,到头了,准备向下
if (d == 0 && y < n - 1 && lin[x][y + 1] == 0) {
lin[x][++y] = ++step;
} else if (d == 0 && y == n - 1) {
d = 1;
} else
// (2)向下走,到头了,准备向左
if (d == 1 && x < n - 1 && lin[x + 1][y] == 0) {
lin[++x][y] = ++step;
} else if (d == 1 && x == n - 1) {
d = 2;
} else
// (3)向左走,到头了,准备向上
if (d == 2 && y > 0 && lin[x][y - 1] == 0) {
lin[x][--y] = ++step;
} else if (d == 2 && y == 0) {
d = 3;
} else
// (4)向上走,到头了,准备向右
if (d == 3 && x > 0 && lin[x - 1][y] == 0) {
lin[--x][y] = ++step;
} else if (d == 3 && x == 0) {
d = 4;
} else {
break;
}
}
//输出小林的路线
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
cout << lin[j][k] << " ";
}
cout << endl;
}
//**小华开始***************************************************************************/
//小华
step = 1;
cin >> x >> y >> d;
//标识走过这个点
hua[x][y] = 1;
//模拟小华走路,小华则会向左转
while (true) {
// (1)向右走,到头了,准备向上
if (d == 0 && y < n - 1 && hua[x][y + 1] == 0) {
hua[x][++y] = ++step;
} else if (d == 0 && y == n - 1) {
d = 3;
} else
// (2)向下走,到头了,准备向右
if (d == 1 && x < n - 1 && hua[x + 1][y] == 0) {
hua[++x][y] = ++step;
} else if (d == 1 && x == n - 1) {
d = 0;
} else
// (3)向左走,到头了,准备向下
if (d == 2 && y > 0 && hua[x][y - 1] == 0) {
hua[x][--y] = ++step;
} else if (d == 2 && y == 0) {
d = 1;
} else
// (4)向上走,到头了,准备向左
if (d == 3 && x > 0 && hua[x - 1][y] == 0) {
hua[--x][y] = ++step;
} else if (d == 3 && x == 0) {
d = 2;
} else
break;
}
//输出小华的路线
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
cout << hua[j][k] << " ";
}
cout << endl;
}
}
//输出结果
int min = INT32_MAX;
int min_x = -1;
int min_y = -1;
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
if (lin[j][k] == hua[j][k] && hua[j][k]>0) {
if (lin[j][k] < min) {
min = lin[j][k];
min_x = j;
min_y = k;
}
}
}
}
if (min == INT32_MAX) cout << -1 << endl;
else cout << min_x << " " << min_y << endl;
return 0;
}