Files
python/TangDou/CSP-J/J2-2024/explore.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
1.8 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;
const int N = 1010;
int a[N][N];//装地图,1:代表有障碍物,0:代表是空地,2:代表是空地但我走过
int main() {
//配置从文本文件读入数据
freopen("explore.in","r", stdin);
freopen("explore.out","w", stdout);
int T;
cin >> T;
while(T--){
//多组测试数据清空数据是最重要的
memset(a, 0, sizeof(a));//0:代表是空的,没有输入过
//n, m 表示地图的行数和列数,k 表示机器人执行操作的次数
int n, m, k;
cin >> n >> m >> k;
//(x,y):机器人的初始坐标,d是初始的朝向
int x, y, d;
cin >> x >> y >> d;
a[x][y] = 2;
for (int i = 1; i <= n;i++)
for (int j = 1; j <= m;j++){
char q;
cin >> q;
if (q == 'x') a[i][j] = 1;//障碍物是1,否则是0,0就不填写了
}
//至此,输入结束
while(k--){
int nx, ny;
if (d == 0) nx = x, ny = y + 1;
if (d == 1) nx = x + 1, ny = y;
if (d == 2) nx = x, ny = y - 1;
if (d == 3) nx = x - 1, ny = y;
if(a[nx][ny]==1 || nx>n || ny>m|| nx<1 || ny<1) {
d = (d + 1) % 4;
//cout << "Turn d=" << d << endl;
}
else {
x = nx, y = ny, a[x][y] = 2;
//cout << "x=" << x << ",y=" << y << " d=" << d << endl;
}
}
//输出结果
int cnt = 0;
for (int i = 1;i<=n;i++)
for (int j = 1;j<=m;j++)
if (a[i][j] == 2) cnt++;
cout << cnt << endl;
}
return 0;
}