QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#21347#1273. It's All SquaresFudanU1#TL 3ms5884kbC++173.1kb2022-03-04 16:30:392022-05-08 02:54:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-08 02:54:59]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:5884kb
  • [2022-03-04 16:30:39]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
#define srep(i, l, r) for (int i = l; i < r; i++)
#define sper(i, r, l) for (int i = r; i > l; i--)
#define maxn 422
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;

int n, m, q;
int a[maxn][maxn];

int ct[maxn * maxn];
int dir[maxn][maxn];
bool vis[maxn][maxn];

bool judge(int x, int y) {
	return !vis[x][y] && 1 <= x && x <= n && 1 <= y && y <= m;
}

int di[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

char s[maxn * maxn << 2]; int l;
int lis[maxn * maxn], cs = 0;
int rlis[maxn * maxn][2], cr = 0;
bool havv[maxn * maxn];

bool get_numx(int x, int y) {
	bool num = 0;
	rep(i, x, n) if (!((dir[i][y] >> 1) & 1)) num ^= 1; 
	return num;
}
bool get_numy(int x, int y) {
	bool num = 0;
	rep(i, y, m) if (!((dir[x][i] >> 3) & 1)) num ^= 1; 
	return num;
}
pii get_st(int x, int y) {
	int i = 1;
	if (s[i] == 'L') {
		if (get_numy(x, y)) return pii(x, y);
		if (get_numy(x, y + 1)) return pii(x, y + 1);
	}
	else if (s[i] == 'R') {
		if (get_numy(x + 1, y)) return pii(x + 1, y);
		if (get_numy(x + 1, y + 1)) return pii(x + 1, y + 1);
	}
	else if (s[i] == 'D') {
		if (get_numx(x + 1, y)) return pii(x + 1, y);
		if (get_numx(x, y)) return pii(x, y);
	}
	else {
		if (get_numx(x + 1, y + 1)) return pii(x + 1, y + 1);
		if (get_numx(x, y + 1)) return pii(x, y + 1);
	}
}

queue<pii> que;

void update(pii _op) {
	vis[_op.fi][_op.se] = 1;
	++cr; rlis[cr][0] = _op.fi, rlis[cr][1] = _op.se;
	++cs; lis[cs] = a[_op.fi][_op.se];
	ct[lis[cs]]++;
	havv[lis[cs]] = 1;
}

void work(int x, int y, int d) {
	if (d == 1) {
		rep(i, 1, l) {
			if (s[i] == 'L') {
				dir[x][y + 1] &= (15 - 4);
				dir[x][y] &= (15 - 8);
				x -= 1;
			}
			else if (s[i] == 'R') {
				dir[x + 1][y + 1] &= (15 - 4);
				dir[x + 1][y] &= (15 - 8);
				x += 1;
			}
			else if (s[i] == 'D') {
				dir[x + 1][y] &= (15 - 1);
				dir[x][y] &= (15 - 2);
				y -= 1;
			}
			else {
				dir[x + 1][y + 1] &= (15 - 1);
				dir[x][y + 1] &= (15 - 2);
				y += 1;
			}
		}
		
		pii st = get_st(x, y);
		
		//cerr << x << '@' << y << endl;
		
		update(st);
		que.push(st);
		while (!que.empty()) {
			pii op = que.front(); que.pop();
			rep(i, 0, 3) {
				if (!((dir[op.fi][op.se] >> i) & 1)) continue;
				if (!judge(op.fi + di[i][0], op.se + di[i][1])) continue;
				pii _op = pii(op.fi + di[i][0], op.se + di[i][1]);
				update(_op);
				que.push(_op);
			}
		}
	}
	else {
		rep(i, 1, cs) ct[lis[i]] = 0;
		rep(i, 1, cr) dir[rlis[i][0]][rlis[i][1]] = 15, vis[rlis[i][0]][rlis[i][1]] = 0;
		cs = cr = 0;
	}
}

int main(){
	int T; scanf("%d", &T);
	while (T--) {
		scanf("%d%d%d", &n, &m, &q);
		rep(i, 1, n) rep(j, 1, m) dir[i][j] = 15;
		rep(i, 1, n) rep(j, 1, m) scanf("%d", &a[i][j]); 
		rep(i, 1, q) {
			int x, y;
			scanf("%d%d%s", &x, &y, s + 1);
			l = strlen(s + 1);
			work(x, y, 1);
			
			int ans = 0;
			rep(i, 1, cs) if (havv[lis[i]] && ct[lis[i]] > 0) havv[lis[i]] = 0, ans++;
			printf("%d\n", ans);
			
			work(x, y, -1);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 5884kb

input:

1
3 3 2
1 2 3
1 1 2
7 8 9
0 0 RRRUUULLLDDD
0 0 RRUULLDD

output:

6
2

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
353 304 4000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98...

output:

59
64
78
56
106982
55
62
68
121
106905
65
71
72
55
78
79
58
61
57
59
52
66
79
68
68
96
106172
79
60
58
58
64
62
106873
54
80
52
67
67
50
106880
62
70
77
93
69
106882
50
53
86
65
74
57
77
51
82
56
81
106518
74
63
61
58
55
106915
86
53
65
82
63
61
99
77
84
74
83
59
67
73
72
75
51
87
61
51
105834
73
75...

result: