AC(主要是要注意走重复的点不算步数)

我是小学生  •  1天前


include <bits/stdc++.h>

using namespace std; typedef long long ll;

ll t, n, m, k; int a[1010][1010] = {0};

bool vis[1010][1010] = {0}; char c;

bool pd(ll x, ll y) {

return (x >= 1 && x <= n && y >= 1 && y <= m);

}

int main() {

cin >> t;
while (t--) {
	memset(a, 0, sizeof(a));
	memset(vis, 0, sizeof(vis));
	ll tot = 1;
	ll d, x, y;
	cin >> n >> m >> k;
	cin >> x >> y >> d;
	for (ll i = 1; i <= n; i++) {
		for (ll j = 1; j <= m; j++) {
			cin >> c;
			if (c == 'x')
				a[i][j] = 1;
		}
	}

	vis[x][y] = 1;

	while (k--) {
		if (d == 0) {
			int x1 = x, y1 = y + 1;
			if (pd(x1, y1) && a[x1][y1] == 0) {
				x = x1;
				y = y1;
				if (!vis[x][y]) {
					tot++;
					vis[x][y] = 1;
				}
			} else {
				d = (d + 1) % 4;
			}
		} else if (d == 1) {
			int x1 = x + 1, y1 = y;
			if (pd(x1, y1) && a[x1][y1] == 0) {
				x = x1;
				y = y1;
				if (!vis[x][y]) {
					tot++;
					vis[x][y] = 1;
				}
			} else {
				d = (d + 1) % 4;
			}
		} else if (d == 2) {
			int x1 = x, y1 = y - 1;
			if (pd(x1, y1) && a[x1][y1] == 0) {
				x = x1;
				y = y1;
				if (!vis[x][y]) {
					tot++;
					vis[x][y] = 1;
				}
			} else {
				d = (d + 1) % 4;
			}
		} else if (d == 3) {
			int x1 = x - 1, y1 = y;
			if (pd(x1, y1) && a[x1][y1] == 0) {
				x = x1;
				y = y1;
				if (!vis[x][y]) {
					tot++;
					vis[x][y] = 1;
				}
			} else {
				d = (d + 1) % 4;
			}
		}
	}
	cout << tot << endl;
}
return 0;

}


评论:

请先登录,才能进行评论