Submission #00212


ソースコード

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<bits/stdc++.h>
using namespace std;
int main(){
int h,w;cin>>h>>w;
string s[h];
for(int i=0;i<h;i++){
cin>>s[i];
}
vector<vector<int>>t(h,vector<int>(w,999));
vector<vector<int>>t2(h,vector<int>(w,999));
pair<int,int>st,go;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(s[i][j]=='S'){
st=make_pair(i,j);
t[i][j]=0;
s[i][j]='.';
}else if(s[i][j]=='G'){
go=make_pair(i,j);
s[i][j]='.';
}
}
}
queue<pair<pair<int,int>,int>>q;
q.push(make_pair(st,0));
pair<pair<int,int>,int>now;
while(!q.empty()){
now=q.front();
q.pop();
int x=now.first.first,y=now.first.second,f=now.second;
if(f==0){
if(s[x][y]=='.'){
if(x-1>=0&&s[x-1][y]!='#'&&t[x-1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x-1,y),0));
t[x-1][y]=t[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t[x+1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x+1,y),0));
t[x+1][y]=t[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t[x][y-1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y-1),0));
t[x][y-1]=t[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t[x][y+1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y+1),0));
t[x][y+1]=t[x][y]+1;
}
}else if(s[x][y]=='U'){
if(x-1>=0&&s[x-1][y]!='#'&&t[x-1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x-1,y),0));
t[x-1][y]=t[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t2[x+1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x+1,y),1));
t2[x+1][y]=t[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t2[x][y-1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y-1),1));
t2[x][y-1]=t[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t2[x][y+1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y+1),1));
t2[x][y+1]=t[x][y]+1;
}
}else if(s[x][y]=='D'){
if(x-1>=0&&s[x-1][y]!='#'&&t2[x-1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x-1,y),1));
t2[x-1][y]=t[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t[x+1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x+1,y),0));
t[x+1][y]=t[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t2[x][y-1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y-1),1));
t2[x][y-1]=t[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t2[x][y+1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y+1),1));
t2[x][y+1]=t[x][y]+1;
}
}else if(s[x][y]=='L'){
if(x-1>=0&&s[x-1][y]!='#'&&t2[x-1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x-1,y),1));
t2[x-1][y]=t[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t2[x+1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x+1,y),1));
t2[x+1][y]=t[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t[x][y-1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y-1),0));
t[x][y-1]=t[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t2[x][y+1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y+1),1));
t2[x][y+1]=t[x][y]+1;
}
}else if(s[x][y]=='R'){
if(x-1>=0&&s[x-1][y]!='#'&&t2[x-1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x-1,y),1));
t2[x-1][y]=t[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t2[x+1][y]>t[x][y]+1){
q.push(make_pair(make_pair(x+1,y),1));
t2[x+1][y]=t[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t2[x][y-1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y-1),1));
t2[x][y-1]=t[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t[x][y+1]>t[x][y]+1){
q.push(make_pair(make_pair(x,y+1),0));
t[x][y+1]=t[x][y]+1;
}
}
}else{
if(s[x][y]=='.'){
if(x-1>=0&&s[x-1][y]!='#'&&t2[x-1][y]>t2[x][y]+1){
q.push(make_pair(make_pair(x-1,y),1));
t2[x-1][y]=t2[x][y]+1;
}
if(x+1<h&&s[x+1][y]!='#'&&t2[x+1][y]>t2[x][y]+1){
q.push(make_pair(make_pair(x+1,y),1));
t2[x+1][y]=t2[x][y]+1;
}
if(y-1>=0&&s[x][y-1]!='#'&&t2[x][y-1]>t2[x][y]+1){
q.push(make_pair(make_pair(x,y-1),1));
t2[x][y-1]=t2[x][y]+1;
}
if(y+1<w&&s[x][y+1]!='#'&&t2[x][y+1]>t2[x][y]+1){
q.push(make_pair(make_pair(x,y+1),1));
t2[x][y+1]=t2[x][y]+1;
}
}else if(s[x][y]=='U'){
if(x-1>=0&&s[x-1][y]!='#'&&t2[x-1][y]>t2[x][y]+1){
q.push(make_pair(make_pair(x-1,y),1));
t2[x-1][y]=t2[x][y]+1;
}
}else if(s[x][y]=='D'){
if(x+1<h&&s[x+1][y]!='#'&&t2[x+1][y]>t2[x][y]+1){
q.push(make_pair(make_pair(x+1,y),1));
t2[x+1][y]=t2[x][y]+1;
}
}else if(s[x][y]=='L'){
if(y-1>=0&&s[x][y-1]!='#'&&t2[x][y-1]>t2[x][y]+1){
q.push(make_pair(make_pair(x,y-1),1));
t2[x][y-1]=t2[x][y]+1;
}
}else if(s[x][y]=='R'){
if(y+1<w&&s[x][y+1]!='#'&&t2[x][y+1]>t2[x][y]+1){
q.push(make_pair(make_pair(x,y+1),1));
t2[x][y+1]=t2[x][y]+1;
}
}
}
}
if(t[go.first][go.second]==999&&t2[go.first][go.second]==999){
cout<<-1<<"\n";
return(0);
}
cout<<min(t[go.first][go.second],t2[go.first][go.second])<<"\n";
}

ステータス

項目 データ
問題 0008 - 一方通行迷路ゲーム
ユーザー名 ei2326
投稿日時 2023-08-28 11:51:40
言語 C++17
状態 Wrong Answer
得点 0
ソースコード長 6740 Byte
最大実行時間 32 ms
最大メモリ使用量 1608 KB

セット

セット 得点 Cases
1 ALL 0 / 30 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1 AC 20 ms 476 KB
1
in2 AC 15 ms 572 KB
1
in3 AC 18 ms 692 KB
1
in4 AC 19 ms 664 KB
1
in5 AC 23 ms 760 KB
1
in6 AC 30 ms 600 KB
1
in7 AC 19 ms 700 KB
1
in8 AC 20 ms 800 KB
1
in9 AC 25 ms 944 KB
1
in10 AC 21 ms 568 KB
1
in11 AC 20 ms 344 KB
1
in12 AC 26 ms 520 KB
1
in13 AC 23 ms 600 KB
1
in14 AC 27 ms 1300 KB
1
in15 AC 32 ms 1288 KB
1
in16 AC 21 ms 1272 KB
1
in17 AC 26 ms 1256 KB
1
in18 AC 23 ms 1496 KB
1
in19 AC 24 ms 1480 KB
1
in20 AC 26 ms 1440 KB
1
in21 AC 25 ms 1420 KB
1
in22 WA 25 ms 1404 KB
1
in23 WA 27 ms 1396 KB
1
in24 WA 18 ms 1384 KB
1
in25 AC 24 ms 1500 KB
1
in26 WA 24 ms 1492 KB
1
in27 WA 26 ms 1608 KB
1
in28 AC 26 ms 1596 KB
1