Submission #00014


ソースコード

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
#include<bits/stdc++.h>
using namespace std;
#define int long long
main(){
int h,w;cin>>h>>w;
vector<vector<char>>s(h+2,vector<char>(w+2,'#'));
vector<vector<pair<int,int>>>f(26,vector<pair<int,int>>(0));
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>s[i][j];
if('a'<=s[i][j] && s[i][j] <='z'){
f[s[i][j]-'a'].push_back({i,j});
}
}
}
queue<pair<int,int>>q;
vector<vector<int>>t(h+2,vector<int>(w+2,LONG_LONG_MAX));
t[1][1]=0;
q.push({1,1});
while(!q.empty()){
int x=q.front().first;
int y=q.front().second;
q.pop();
if('a'<=s[x][y] && s[x][y]<='z'){
for(int i=0;i<f[s[x][y]-'a'].size();i++){
t[f[s[x][y]-'a'][i].first][f[s[x][y]-'a'][i].second]=t[x][y];
q.push({f[s[x][y]-'a'][i].first,f[s[x][y]-'a'][i].second});
}
f[s[x][y]-'a'].clear();
}
if(s[x-1][y]!='#' && t[x][y]+1<t[x-1][y]){
t[x-1][y] = t[x][y]+1;
q.push({x-1,y});
}
if(s[x+1][y]!='#' && t[x][y]+1<t[x+1][y]){
t[x+1][y] = t[x][y]+1;
q.push({x+1,y});
}
if(s[x][y-1]!='#' && t[x][y]+1<t[x][y-1]){
t[x][y-1] = t[x][y]+1;
q.push({x,y-1});
}
if(s[x][y+1]!='#' && t[x][y]+1<t[x][y+1]){
t[x][y+1] = t[x][y]+1;
q.push({x,y+1});
}
}
if(t[h][w]==LONG_LONG_MAX){
cout<<"-1\n";
}else{
cout<<t[h][w]<<"\n";
}
}

ステータス

項目 データ
問題 0002 - Move and Teleport
ユーザー名 ei2326
投稿日時 2024-08-09 10:55:52
言語 C++17
状態 Accepted
得点 20
ソースコード長 1628 Byte
最大実行時間 93 ms
最大メモリ使用量 9496 KB

セット

セット 得点 Cases
1 ALL 20 / 20 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1.txt AC 19 ms 604 KB
1
in2.txt AC 19 ms 568 KB
1
in3.txt AC 19 ms 400 KB
1
in4.txt AC 18 ms 628 KB
1
in5.txt AC 92 ms 9428 KB
1
in6.txt AC 93 ms 9396 KB
1
in7.txt AC 87 ms 9496 KB
1
in8.txt AC 62 ms 9324 KB
1
in9.txt AC 72 ms 9460 KB
1