Submission #129768

#TimeUsernameProblemLanguageResultExecution timeMemory
129768davitmargPortals (BOI14_portals)C++17
20 / 100
38 ms30044 KiB
/*DavitMarg*/ #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <map> #include <set> #include <queue> #include <iomanip> #include <stack> #include <cassert> #include <iterator> #include <bitset> #include <fstream> #define mod 1000000007ll #define LL long long #define LD long double #define MP make_pair #define PB push_back #define all(v) v.begin(),v.end() using namespace std; int n,m,k,st,en,used[1000006],dist[1000006]; pair<int,int> u[1003][1003],d[1003][1003],l[1003][1003],r[1003][1003]; vector<int> g[1000006]; char a[1003][1003]; int id(int y,int x) { return y*m+x; } void print(int ind) { int y,x; x=ind%m; if(!x) x=m; y=(ind-x)/m; cout<<"! "<<y<<" "<<x<<" d="<<dist[ind]<<endl; } void addNode(int a,int b) { if(a==b) return; g[a].PB(b); } int main() { cin >> n >> m; k=n*m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c",a[i]+j); if(a[i][j]=='S') { st=id(i,j); a[i][j]='.'; } else if(a[i][j]=='C') { en=id(i,j); a[i][j]='.'; } } for(int i=1;i<=n;i++) a[i][0]=a[i][m+1]='#'; for(int i=1;i<=m;i++) a[0][i]=a[n+1][i]='#'; for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) { if(a[i][j]=='#') u[i][j]=l[i][j]=MP(i,j); else { u[i][j]=u[i-1][j]; l[i][j]=l[i][j-1]; } } for(int i=n+1;i>=1;i--) for(int j=m+1;j>=1;j--) { if(a[i][j]=='#') d[i][j]=r[i][j]=MP(i,j); else { d[i][j]=d[i+1][j]; r[i][j]=r[i][j+1]; } } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(a[i][j]=='#') continue; if(a[i-1][j]!='#') addNode(id(i,j),id(i-1,j)); if(a[i+1][j]!='#') addNode(id(i,j),id(i+1,j)); if(a[i][j-1]!='#') addNode(id(i,j),id(i,j-1)); if(a[i][j+1]!='#') addNode(id(i,j),id(i,j+1)); if(a[i-1][j]=='#' || a[i+1][j]=='#' || a[i][j-1]=='#'|| a[i][j+1]=='#') { int y,x; y=u[i][j].first; x=u[i][j].second; addNode(id(i,j),id(y+1,x)); y=d[i][j].first; x=d[i][j].second; addNode(id(i,j),id(y-1,x)); y=l[i][j].first; x=l[i][j].second; addNode(id(i,j),id(y,x+1)); y=r[i][j].first; x=r[i][j].second; addNode(id(i,j),id(y,x-1)); } } queue<int> q; q.push(st); used[st]=1; while(!q.empty()) { int v=q.front(); q.pop(); //print(v); for(int i=0;i<g[v].size();i++) { int to=g[v][i]; if(used[to]) continue; used[to]=1; dist[to]=dist[v]+1; q.push(to); } } cout<<dist[en]<<endl; return 0; } /* 4 4 S..# .... .... ..C. */

Compilation message (stderr)

portals.cpp: In function 'int main()':
portals.cpp:144:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<g[v].size();i++)
                     ~^~~~~~~~~~~~
portals.cpp:59:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf(" %c",a[i]+j);
             ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...