Submission #1368789
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__",", __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#define INF 11200000
int main(){
int n,m;
cin>>n>>m;
vector<string> vec(n);
rep(i,n) cin>>vec[i];
int sx,sy,gx,gy,cx,cy;
rep(i,n)rep(j,m){
if(vec[i][j]=='S') sx=i,sy=j;
if(vec[i][j]=='G') gx=i,gy=j;
if(vec[i][j]=='C') cx=i,cy=j;
}
//dbg(sx,sy,gx,gy,cx,cy);
auto dfs = [&](int p,int q,int r,int s){
vector<vector<int>> d(n,vector<int>(m,INF));
d[p][q] = 0;
queue<int> xx,yy;
xx.push(p);yy.push(q);
while(!xx.empty()){
int x = xx.front(); xx.pop();
int y = yy.front(); yy.pop();
if(x==r && y==s) break;
const int dx[] ={0,0,1,-1}, dy[]={1,-1,0,0};//dbg(x,y);
rep(i,4){
int nx =x+dx[i], ny=y+dy[i];
if(nx<0 || ny<0 || nx>=n || ny>=m || vec[nx][ny]=='#') continue;
if(d[nx][ny] <= d[x][y]) continue;
d[nx][ny] = d[x][y]+1;
xx.push(nx);yy.push(ny);
}
}
return d[r][s];
};
int ans = dfs(sx,sy,cx,cy) + dfs(cx,cy,gx,gy);
if(ans>=INF) cout << -1 << endl;
else cout << ans << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
C - 自宅からの脱出 |
User |
tossy |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
1879 Byte |
Status |
TLE |
Exec Time |
2135 ms |
Memory |
504660 KB |
Judge Result
Set Name |
All |
Score / Max Score |
0 / 100 |
Status |
|
Set Name |
Test Cases |
All |
input_0.txt, input_1.txt, input_10.txt, input_11.txt, input_12.txt, input_13.txt, input_14.txt, input_15.txt, input_16.txt, input_17.txt, input_18.txt, input_19.txt, input_2.txt, input_20.txt, input_21.txt, input_22.txt, input_23.txt, input_3.txt, input_4.txt, input_5.txt, input_6.txt, input_7.txt, input_8.txt, input_9.txt |
Case Name |
Status |
Exec Time |
Memory |
input_0.txt |
AC |
1 ms |
256 KB |
input_1.txt |
AC |
1 ms |
256 KB |
input_10.txt |
AC |
1 ms |
256 KB |
input_11.txt |
AC |
1 ms |
256 KB |
input_12.txt |
AC |
1 ms |
256 KB |
input_13.txt |
AC |
2 ms |
256 KB |
input_14.txt |
AC |
17 ms |
1564 KB |
input_15.txt |
TLE |
2114 ms |
170248 KB |
input_16.txt |
TLE |
2115 ms |
190328 KB |
input_17.txt |
TLE |
2124 ms |
330184 KB |
input_18.txt |
TLE |
2131 ms |
447384 KB |
input_19.txt |
TLE |
2132 ms |
497784 KB |
input_2.txt |
AC |
1 ms |
256 KB |
input_20.txt |
TLE |
2114 ms |
170352 KB |
input_21.txt |
TLE |
2135 ms |
504660 KB |
input_22.txt |
AC |
11 ms |
1564 KB |
input_23.txt |
AC |
11 ms |
1564 KB |
input_3.txt |
AC |
1 ms |
256 KB |
input_4.txt |
AC |
1 ms |
256 KB |
input_5.txt |
AC |
1 ms |
256 KB |
input_6.txt |
AC |
1 ms |
256 KB |
input_7.txt |
AC |
1 ms |
256 KB |
input_8.txt |
AC |
1 ms |
256 KB |
input_9.txt |
AC |
1 ms |
256 KB |