Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <queue>
- using namespace std;
- ifstream fin("suprasolicitare.in");
- ofstream fout("suprasolicitare.out");
- const int N=105;
- bool a[N][N],r[N][N];
- int d[N][N],t[N][N],n,x,y;
- const int dx[]={-1,0,1,0},
- dy[]={0,1,0,-1};
- queue <pair <int, int> > Q;
- void get_sol(int x,int y)
- {
- int tx=t[x][y]/n,ty=t[x][y]%n;
- if (t[x][y]!=-1)
- get_sol(tx,ty);
- else
- return;
- if (ty==y)
- {
- if(a[(x + 1)%n][y])
- fout<<"Jos\n";
- if(a[(x+ n-1)%n][y])
- fout<<"Sus\n";
- }
- else
- {
- if(a[x][(y+1)%n])
- fout<<"Dreapta\n";
- else
- if(a[x][(y+n-1)% n])
- fout<<"Stanga\n";
- }
- }
- int main()
- {
- fin>>n;
- for(int i=0;i<n;++i)
- for(int j=0;j<n;++j)
- fin>>a[i][j];
- fin>>x>>y;
- d[--x][--y]=1;
- t[x][y]=-1;
- Q.push(make_pair(x,y));
- for(int i=0;i<n;++i)
- {
- bool ok=1;
- for(int j=0;ok&&j<n;++j)
- ok&=!a[i][j];
- if (ok)
- for(int j=0;j<n;++j)
- r[i][j]=1;
- }
- for (int j=0;j<n;++j)
- {
- bool ok=1;
- for(int i=0;i<n;++i)
- ok&=!a[i][j];
- if (ok)
- for(int i=0;i<n;++i)
- r[i][j]=1;
- }
- while(Q.size()&&!r[Q.front().first][Q.front().second])
- {
- int x =Q.front().first,y=Q.front().second;
- Q.pop();
- for(int k=0;k<4;++k)
- {
- int xx=x,yy=y;
- while(!a[(xx+n+dx[k])%n][(yy+n+dy[k])%n])
- {
- xx=(xx+n+dx[k])%n,yy=(yy+n+dy[k])%n;
- }
- if(!d[xx][yy])
- {
- d[xx][yy]=d[x][y]+1;
- t[xx][yy]=x*n+y;
- Q.push(make_pair(xx,yy));
- }
- }
- }
- if(!Q.size())
- {
- fout<<-1;
- return 0;
- }
- int sol_x=Q.front().first,sol_y=Q.front().second;
- fout<<d[sol_x][sol_y]<<'\n';
- get_sol(sol_x,sol_y);
- if(d[sol_x][sol_y]==1)
- {
- fout<<"Dreapta";
- return 0;
- }
- if(a[(sol_x+1)%n][sol_y]||a[(sol_x+n-1)%n][sol_y])
- fout<<"Dreapta";
- else
- if(a[sol_x][(sol_y+1)%n]||a[sol_x][(sol_y+n-1)%n])
- fout<<"Jos";
- }
Add Comment
Please, Sign In to add comment