QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#350656#6723. Grid with ArrowswudibaolongAC ✓145ms9916kbC++142.2kb2024-03-10 22:40:562024-03-10 22:40:57

Judging History

This is the latest submission verdict.

  • [2024-03-10 22:40:57]
  • Judged
  • Verdict: AC
  • Time: 145ms
  • Memory: 9916kb
  • [2024-03-10 22:40:56]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int NM = 1e6 + 5;

char mp[NM] = {0};
int fa[NM] = {0};
int in[NM] = {0}, out[NM] = {0};
int n, m;

bool check(pair<int, int> p) {
    int x = p.first, y = p.second;
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

int point(int i, int j) {
    return (i - 1) * m + j;
}

pair<int, int> move(int i, int j, char ch, int k) {
    switch (ch) {
        case 'u': return {i - k, j};
        case 'd': return {i + k, j};
        case 'l': return {i, j - k};
        case 'r': return {i, j + k};
    }
}

void init() {
    for (int i = 1; i <= n * m; ++i) {
        fa[i] = i;
        in[i] = out[i] = 0;
    }
}

int find(int x) {
    return fa[x] == x ? x:(fa[x] = find(fa[x]));
}

void merge(int a, int b) {
    int aa = find(a), bb = find(b);
    fa[bb] = aa;
}

bool check1() {
    int cnt = 0;
    for (int i = 1; i <= n * m; ++i) {
        //cout << fa[i] << " \n"[i == n * m];
        if (fa[i] == i) ++cnt;
    }
    //cout << cnt << '\n';
    return cnt == 1;
}

bool check2() {
    int in_0 = 0;
    for (int i = 1; i <= n * m; ++i) if (!in[i]) ++in_0;
    if (in_0 > 1) return 0;
    int cnt0 = 0, cnt1 = 0, cnt_1 = 0;
    for (int i = 1; i <= n * m; ++i) {
        int t = out[i] - in[i];
        if (!t) ++cnt0;
        else if (t == 1) ++cnt1;
        else if (t == -1) ++cnt_1;
    }
    if (cnt1 <= 1 && cnt_1 <= 1 && cnt1 + cnt_1 + cnt0 == n * m) return 1;
    return 0;
}

void solve() {
    scanf("%d %d", &n, &m);
    init();
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            scanf(" %c", mp + point(i, j));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            int k;
            cin >> k;
            auto t = move(i, j, mp[point(i, j)], k);
            if (check(t)) {
                int u = point(i, j), v = point(t.first, t.second);
                out[u]++;
                in[v]++;
                merge(v, u);
            }
        }
    if (check1() && check2()) cout << "Yes\n";
    else cout << "No\n";
}

signed main() {
    //ios::sync_with_stdio(false), cin.tie(nullptr);
    int T = 1;
    scanf("%d", &T);
    while (T--) solve();
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9916kb

input:

2
2 3
rdd
url
2 1 1
1 1 2
2 2
rr
rr
1 1
1 1

output:

Yes
No

result:

ok 2 token(s): yes count is 1, no count is 1

Test #2:

score: 0
Accepted
time: 145ms
memory: 8356kb

input:

1109
5 8
rddddldl
drruludl
rrldrurd
urrrlluu
uurrulrl
4 4 1 2 4 3 1 6
1 3 5 1 1 1 3 6
2 4 1 1 2 1 1 1
2 3 4 2 4 3 3 3
4 1 1 2 2 5 1 5
7 9
rdrddrdll
urrdruldl
ruullrulu
drrlrlddl
rrrdddlll
ruulururl
ruurrlluu
7 1 1 1 2 1 2 3 6
1 7 3 1 3 1 2 1 8
2 2 1 2 4 3 1 2 2
2 2 4 1 1 5 3 3 1
3 4 6 1 2 1 2 7 7
6 ...

output:

Yes
No
No
No
Yes
No
Yes
Yes
Yes
Yes
No
No
No
Yes
Yes
No
Yes
No
Yes
No
No
No
Yes
Yes
No
Yes
Yes
No
Yes
No
No
Yes
No
No
Yes
Yes
Yes
No
No
Yes
Yes
Yes
No
Yes
No
Yes
No
Yes
Yes
No
Yes
No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
Yes
No
No
Yes
Yes
No
No
Yes
Yes
No
No
Yes
Yes
Yes
Yes
Y...

result:

ok 1109 token(s): yes count is 602, no count is 507