Submission #61392722
Source Code Expand
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include<bits/stdc++.h>
#define int long long
#define f(i,j,n) for(int i=j;i<=n;i++)
#define F(i,n,j) for(int i=n;i>=j;i--)
#define updmax(a,b) a=max(a,b)
#define updmin(a,b) a=min(a,b)
#define pb push_back
#define XQZ
using namespace std;
namespace fsd{
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2)?EOF:*p1++)
const int MAXSIZE=1<<20;
char buf[MAXSIZE],*p1,*p2;
inline int read(){
int ak=0,ioi=1;char c=gc();
while(!isdigit(c)){if(c=='-')ioi=-1;c=gc();}
while(isdigit(c))ak=ak*10+(c^48),c=gc();
return ak*ioi;
}
inline string reads(){
string o="";
char p=gc();
while(p>'z'||p<'a'){p=gc();}
while(p<='z'&&p>='a'){o+=p;p=gc();}
return o;
}
inline char readc(){
char p=gc();
while(!((p<='z'&&p>='a')||(p<='Z'&&p>='A'))){p=gc();}
return p;
}
inline long double readd(){
long double ak=0;int ioi=1;char c=gc();
while(!isdigit(c)){if(c=='-')ioi=-1;c=gc();}
while(isdigit(c))ak*=10,ak+=c-'0',c=gc();
c=gc();
long double q=0.1;
while(isdigit(c))ak+=(c-'0')*q,q*=0.1,c=gc();
return ak*ioi;
}
}
using namespace fsd;
int n,m;
bool vis[2][1010][1010];
int d[2][1010][1010];
char a[1010][1010];
int h[3][3]={{1,0},{-1,0}};
int s[3][3]={{0,1},{0,-1}};
queue<pair<int,pair<int,int> > >q;
void gs(){
cin>>n>>m;
f(i,1,n)f(j,1,m)cin>>a[i][j],d[0][i][j]=d[1][i][j]=LLONG_MAX/2;
int x,y;
f(i,1,n)f(j,1,m)if(a[i][j]=='S')x=i,y=j;
int sx,sy;
f(i,1,n)f(j,1,m)if(a[i][j]=='G')sx=i,sy=j;
q.push({0,{x,y}});
q.push({1,{x,y}});
vis[0][x][y]=vis[1][x][y]=1;
d[0][x][y]=d[1][x][y]=0;
while(!q.empty()){
int t=q.front().first,x=q.front().second.first,y=q.front().second.second;
q.pop();
if(t==0){
f(i,0,1){
int nx=x+h[i][0],ny=y+h[i][1];
if(nx<=n&&nx>=1&&ny<=m&&ny>=1&&a[nx][ny]!='#'&&!vis[1][nx][ny]){
vis[1][nx][ny]=1;
d[1][nx][ny]=d[0][x][y]+1;
q.push({1,{nx,ny}});
}
}
}else{
f(i,0,1){
int nx=x+s[i][0],ny=y+s[i][1];
if(nx<=n&&nx>=1&&ny<=m&&ny>=1&&a[nx][ny]!='#'&&!vis[0][nx][ny]){
vis[0][nx][ny]=1;
d[0][nx][ny]=d[1][x][y]+1;
q.push({0,{nx,ny}});
}
}
}
}
int qr=min(d[0][sx][sy],d[1][sx][sy]);
cout<<(qr==LLONG_MAX/2?-1:qr)<<endl;
}
signed main(){
#ifndef XQZ
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
#ifdef NXD
int t=0;cin>>t;while(t--)
#endif
gs();
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Snaky Walk |
User |
xiangqizhen |
Language |
C++ 17 (Clang 16.0.6) |
Score |
400 |
Code Size |
2423 Byte |
Status |
AC |
Exec Time |
81 ms |
Memory |
22520 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
All |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_00.txt |
AC |
1 ms |
3632 KB |
00_sample_01.txt |
AC |
1 ms |
3612 KB |
00_sample_02.txt |
AC |
1 ms |
3572 KB |
01_random_00.txt |
AC |
22 ms |
13336 KB |
01_random_01.txt |
AC |
3 ms |
7012 KB |
01_random_02.txt |
AC |
33 ms |
20268 KB |
01_random_03.txt |
AC |
10 ms |
10744 KB |
01_random_04.txt |
AC |
24 ms |
15608 KB |
01_random_05.txt |
AC |
3 ms |
8764 KB |
01_random_06.txt |
AC |
1 ms |
3864 KB |
01_random_07.txt |
AC |
2 ms |
5004 KB |
01_random_08.txt |
AC |
40 ms |
20308 KB |
01_random_09.txt |
AC |
76 ms |
22312 KB |
01_random_10.txt |
AC |
73 ms |
22388 KB |
01_random_11.txt |
AC |
69 ms |
22328 KB |
01_random_12.txt |
AC |
69 ms |
22348 KB |
01_random_13.txt |
AC |
69 ms |
22348 KB |
01_random_14.txt |
AC |
71 ms |
22520 KB |
01_random_15.txt |
AC |
40 ms |
20324 KB |
01_random_16.txt |
AC |
40 ms |
20576 KB |
01_random_17.txt |
AC |
39 ms |
20432 KB |
01_random_18.txt |
AC |
75 ms |
22312 KB |
01_random_19.txt |
AC |
77 ms |
22320 KB |
02_random2_00.txt |
AC |
40 ms |
20516 KB |
02_random2_01.txt |
AC |
40 ms |
20516 KB |
02_random2_02.txt |
AC |
39 ms |
20272 KB |
02_random2_03.txt |
AC |
41 ms |
20444 KB |
02_random2_04.txt |
AC |
42 ms |
20448 KB |
02_random2_05.txt |
AC |
40 ms |
20348 KB |
02_random2_06.txt |
AC |
40 ms |
20320 KB |
02_random2_07.txt |
AC |
40 ms |
20444 KB |
02_random2_08.txt |
AC |
40 ms |
20520 KB |
02_random2_09.txt |
AC |
40 ms |
20340 KB |
02_random2_10.txt |
AC |
40 ms |
20396 KB |
02_random2_11.txt |
AC |
40 ms |
20372 KB |
02_random2_12.txt |
AC |
40 ms |
20548 KB |
02_random2_13.txt |
AC |
40 ms |
20376 KB |
02_random2_14.txt |
AC |
40 ms |
20260 KB |
02_random2_15.txt |
AC |
40 ms |
20360 KB |
02_random2_16.txt |
AC |
40 ms |
20368 KB |
02_random2_17.txt |
AC |
39 ms |
20376 KB |
02_random2_18.txt |
AC |
41 ms |
20404 KB |
02_random2_19.txt |
AC |
42 ms |
20788 KB |
03_random3_00.txt |
AC |
76 ms |
22300 KB |
03_random3_01.txt |
AC |
55 ms |
22264 KB |
03_random3_02.txt |
AC |
79 ms |
22240 KB |
03_random3_03.txt |
AC |
76 ms |
22344 KB |
04_random4_00.txt |
AC |
69 ms |
22272 KB |
04_random4_01.txt |
AC |
74 ms |
22248 KB |
04_random4_02.txt |
AC |
70 ms |
22408 KB |
04_random4_03.txt |
AC |
74 ms |
22480 KB |
05_handmade_00.txt |
AC |
54 ms |
22364 KB |
05_handmade_01.txt |
AC |
68 ms |
22352 KB |
05_handmade_02.txt |
AC |
1 ms |
3540 KB |
05_handmade_03.txt |
AC |
1 ms |
3596 KB |
05_handmade_04.txt |
AC |
1 ms |
3512 KB |
05_handmade_05.txt |
AC |
81 ms |
22268 KB |