跳到正文

题解

题解:P11228 [CSP-J 2024] 地图探险

Zephyn约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年10月29日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
题目
P11228

版本

  1. v1当前

题意

给一个起始点和初始方向,行动 kk 次。如果前方在地图内且为空地,往前走;否则右转(方向++)。问总共走了多少个格子?

PS:右转算一次行动。

思路

kk 最多 10610^6 次,TT 最多 55,时间复杂度 O(5×106)O(5 \times 10^6),不会超,直接模拟就行了。

代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
bool a[N][N];
set<pair<int,int>> f;
int main(int argc, char **argv){
//	freopen("explore.in","r",stdin);
//	freopen("explore.out","w",stdout);
	int t;
	cin >> t;
	while (t--){
		memset(a,0,sizeof a);
		f.clear();
		int n,m,k;
		int x,y,d;
		cin >> n >> m >> k;
		cin >> x >> y >> d;
		f.insert({x,y});
		for (int i = 1;i <= n;i++){
			string s;
			cin >> s;
			for (int j = 1;j <= m;j++){
				a[i][j] = s[j - 1] == '.'?1:0;
			}
		}
		while (k--){
			int xi = x + dx[d],yi = y + dy[d];
			if (xi > 0 && xi <= n && yi > 0 && yi <= m && a[xi][yi])	x = xi,y = yi,f.insert({x,y});
			else	d++,d %= 4;
		}
		printf("%d\n",f.size());
	}
	return 0;
}
原文