UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#202572#3541. 探险one_zero_three_zero10011514ms9292kbC++112.7kb2024-02-16 10:37:242024-02-16 12:37:41

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

struct snake{
    int xi,yi;
    int curx,cury;
};

struct node{
    int idx,di;
    bool operator < (const node &a) const{
        return di > a.di;
    }
};


int n,q,t;
string a[505];
int cnt[505][505];
int ans[505];
vector<node> e[100005];
int dis[100005];
bool vis[100005];
queue<snake> qa,qb;

void bfs(int st){
    vis[st] = 1,dis[st] = 0;
    deque<int> q;
    q.push_back(st);
    while(q.size()){
        int t = q.front();
        q.pop_front();
        if(t == n * n + n) return;
        for(auto && i : e[t]){
            if(vis[i.idx]) continue;
            vis[i.idx] = 1,dis[i.idx] = dis[t] + i.di;
            if(i.di == 0) q.push_front(i.idx);
            else q.push_back(i.idx);
        }
    }
}

void operate(int t){
    memset(vis,0,sizeof(vis));
    bfs(0);
    ans[t] = dis[n * n + n];
}

void update(){
    while(qa.size()){
        snake t = qa.front();
        qa.pop();
        cnt[t.xi][t.yi] = 1;
        if(t.xi + t.curx <= 0 || t.yi + t.cury <= 0) continue;
        if(t.xi + t.curx > n || t.yi + t.cury > n) continue;
        qb.push({t.xi + t.curx,t.yi + t.cury,t.curx,t.cury});
    }
    swap(qa,qb);
}

void make(){
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= n;j ++){
            e[i * n + j].clear();
        }
    }
    e[0].clear();
    e[0].push_back({1 * n + 1,cnt[1][1]});
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= n;j ++){
            if(i < n){
                e[i * n + j].push_back({(i + 1) * n + j,cnt[i + 1][j]});
                e[(i + 1) * n + j].push_back({i * n + j,cnt[i][j]});
            }
            if(j < n){
                e[i * n + j].push_back({i * n + j + 1,cnt[i][j + 1]});
                e[i * n + j + 1].push_back({i * n + j,cnt[i][j]});
            }
        }
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("../data.in","r",stdin);
    freopen("../data.out","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> q;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
        a[i] = " " + a[i];
        for(int j = 1;j <= n;j ++){
            if(a[i][j] == '.') continue;
            if(a[i][j] == 'L') qa.push({i,j,0,-1});
            if(a[i][j] == 'R') qa.push({i,j,0,1});
            if(a[i][j] == 'U') qa.push({i,j,-1,0});
            if(a[i][j] == 'D') qa.push({i,j,1,0});
        }
    }
    for(int i = 0;i <= n + 2;i ++){
        make();
        operate(i);
        update();
    }
    while(q --){
        cin >> t;
        cout << ans[min(t,n + 1)] << "\n";
    }

    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 3756kb

input:

5 5
U.D.R
UDL..
.....
...LR
.DU.D
1
2
3
4
5

output:

4
4
4
4
4

result:

ok 5 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 3756kb

input:

5 5
.D..L
..L..
R.R..
U..D.
..R.L
1
2
3
4
5

output:

2
3
5
6
6

result:

ok 5 lines

Test #3:

score: 10
Accepted
time: 85ms
memory: 4560kb

input:

100 200000
RU.RRUL...D...DLD.......L.......D.D.L.RL..L.LU....R......U..RL...ULDD.L.UDR.UR.R.U..R..R....

output:

4
27
54
81
102
116
129
140
150
161
167
173
178
180
183
186
188
189
189
191
192
192
192
193
194
195
1...

result:

ok 200000 lines

Test #4:

score: 10
Accepted
time: 89ms
memory: 4556kb

input:

100 200000
..........L.U..R.....R.....U...U..L..U..ULLUR..L.LD.LDU..L.U...D.LDRR...RL..RLLR.ULDR..LD...

output:

3
28
62
89
108
127
139
153
160
166
170
176
179
181
187
189
192
193
194
194
195
195
196
196
196
196
1...

result:

ok 200000 lines

Test #5:

score: 10
Accepted
time: 1835ms
memory: 9180kb

input:

300 1
...L...R..LU.....R....D.DLD...R....................D....L.......R........L......L.........D......

output:

599

result:

ok single line: '599'

Test #6:

score: 10
Accepted
time: 1941ms
memory: 9184kb

input:

300 1
..R..............U.R......L.R........D.................UDUU........RL.......D....................

output:

599

result:

ok single line: '599'

Test #7:

score: 10
Accepted
time: 2016ms
memory: 9212kb

input:

300 200000
R...R............................RR..R.R....R......................R....R..........R........

output:

1
2
3
6
7
9
16
32
48
72
88
109
128
140
164
183
200
218
230
238
250
260
269
278
289
296
304
318
328
3...

result:

ok 200000 lines

Test #8:

score: 10
Accepted
time: 1548ms
memory: 9292kb

input:

300 200000
R..................R.R...........R.R...R.......R.......R.....................R.......RR.....

output:

1
2
3
5
6
11
17
36
53
78
92
118
139
161
179
198
209
223
235
245
253
267
279
286
294
302
312
319
328
...

result:

ok 200000 lines

Test #9:

score: 10
Accepted
time: 1996ms
memory: 9176kb

input:

300 200000
R.......................................D.....................U..........R...L.....U........

output:

1
1
1
1
1
1
2
4
5
10
20
24
32
41
46
54
65
76
80
90
98
106
112
121
127
132
138
147
156
160
164
168
17...

result:

ok 200000 lines

Test #10:

score: 10
Accepted
time: 2004ms
memory: 9188kb

input:

300 200000
..L..R..........................D..............D.............U..............R...............

output:

0
0
1
1
1
6
18
37
50
58
81
97
119
131
140
156
174
191
202
217
226
238
245
257
265
270
277
291
301
31...

result:

ok 200000 lines

Extra Test:

score: 0
Extra Test Passed