QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243123#6723. Grid with ArrowsCipherxzc#AC ✓79ms16348kbC++202.9kb2023-11-07 21:15:212023-11-07 21:15:21

Judging History

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

  • [2023-11-07 21:15:21]
  • 评测
  • 测评结果:AC
  • 用时:79ms
  • 内存:16348kb
  • [2023-11-07 21:15:21]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 1e5 + 5, dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int T, n, m, head[N], nxt[N * 2], to[N * 2], tot;
bool vis[N];
vector<vector<int>> a, dir;
vector<vector<bool>> in;

inline void add(int u, int v){
    //cout << u << ' ' << v << endl;
    to[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
}

void dfs(int p){
    //cout << p << ' ';
    vis[p] = true;
    tot++;
    for (int i = head[p]; i; i = nxt[i]){
        int q = to[i];
        if (!vis[q]){
            dfs(q);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> T;
    while (T--){
        cin >> n >> m;
        a.resize(n + 1);
        dir.resize(n + 1);
        in.resize(n + 1);
        for (int i = 1; i <= n; i++){
            a[i].resize(m + 1);
            dir[i].resize(m + 1);
            in[i].resize(m + 1);
        }
        for (int i = 1; i <= n * m; i++){
            vis[i] = false;
            head[i] = 0;
        }
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                in[i][j] = 0;
            }
        }

        char ch;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                cin >> ch;
                if (ch == 'u'){
                    dir[i][j] = 0;
                }else if (ch == 'd'){
                    dir[i][j] = 1;
                }else if (ch == 'l'){
                    dir[i][j] = 2;
                }else{
                    dir[i][j] = 3;
                }
            }
        }
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                cin >> a[i][j];
            }
        }

        tot = 0;
        int cnt = 0;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                int gg = dir[i][j], x = i + dx[gg] * a[i][j], y = j + dy[gg] * a[i][j];
                //cout << x << ' ' << y << endl;
                if (1 <= x && x <= n && 1 <= y && y <= m){
                    if (!in[x][y]){
                        cnt++;
                        in[x][y] = true;
                    }
                    add((i - 1) * m + j, (x - 1) * m + y);
                }
            }
        }

        tot = 0;
        if (cnt < n * m - 1){
            cout << "No" << endl;
            continue;
        }else if (cnt < n * m){
            for (int i = 1; i <= n; i++){
                for (int j = 1; j <= m; j++){
                    if (!in[i][j]){
                        dfs((i - 1) * m + j);
                        break;
                    }
                }
            }
        }else{
            dfs(1);
        }

        if (tot == n * m){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }

    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 79ms
memory: 16348kb

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