QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#350024#6723. Grid with Arrowswudibaolong#RE 1ms3824kbC++142.6kb2024-03-10 12:46:042024-03-10 12:46:04

Judging History

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

  • [2024-03-10 12:46:04]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3824kb
  • [2024-03-10 12:46:04]
  • 提交

answer

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

void solve() {
    int n, m; cin >> n >> m;
    vector<string> ss(n + 5);
    ss[0] = ss[n + 1] = string(m + 5, ' ');
    for (int i = 1; i <= n; ++i) {
        cin >> ss[i];
        ss[i] = " " + ss[i] + " ";
    }
    
    vector<int> cfa(n * m + 5);
    vector<int> in(n * m + 5), out(n * m + 5);
    for (int i = 1; i <= n; ++i) 
        cfa[i] = i;
    auto find = [&](auto self, int u) -> int {
        return cfa[u] == u ? u : cfa[u] = self(self, cfa[u]);
    };

    auto get_v = [&](int i, int j, int len) -> int {
        if (ss[i][j] == 'r') {
            if (j + len > m) {
                return -1;
            } else {
                return (i - 1) * n + j + len;            
            }
        } else if (ss[i][j] == 'l') {
            if (j - len < 1) {
                return -1;
            } else {
                return (i - 1) * n + j - len; 
            }
        } else if (ss[i][j] == 'u') {
            if (i - len < 1) {
                return -1;
            } else {
                return (i - len - 1) * n + j;
            }
        } else if (ss[i][j] == 'd') {
            if (i + len > n) {
                return -1;
            } else {
                return (i + len - 1) * n + j;
            }
        }
    };

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            int len; cin >> len;
            int u = (i - 1) * m + j;
            int v = get_v(i, j, len);
            if (v == -1) {   
                continue;
            } else {
                out[u]++, in[v]++;
                cfa[find(find, u)] = find(find, v);
            }
        }
    }

    int z1_cnt = 0, f1_cnt = 0, zero_cnt = 0;
    set<int> st;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            int u = (i - 1) * n + j;
            int du = out[u] - in[u];
            if (du == 0) {
                zero_cnt++;
            } else if (du == 1) {
                z1_cnt++;
            } else if (du == -1) {
                f1_cnt++;
            } else {
                cout << "No\n";
                return;
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        st.insert(find(find, i));
    }
    if (st.size() != 1) {
        cout << "No\n";
        return;
    }
    
    if (z1_cnt == 1) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T; cin >> T;
    while (T--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3824kb

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: -100
Runtime Error

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:


result: