Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- typedef
- struct
- {
- int x;
- int y;
- }
- point;
- using namespace std;
- ifstream cin("INPUT.TXT");
- ofstream cout("OUTPUT.TXT");
- void in(vector < vector <char> >& a, vector < vector <int> >& p, point& st,point& fn)
- {
- for (int i=1;i<=a.size()-2;i++)
- for (int j=1;j<=a.size()-2;j++)
- {
- cin>>a[i][j];
- if (a[i][j]=='.') p[i][j]=0; else
- if (a[i][j]=='@')
- {
- p[i][j]=1;
- st.x=i;
- st.y=j;
- } else
- if (a[i][j]=='X')
- {
- p[i][j]=0;
- fn.x=i;
- fn.y=j;
- }
- }
- }
- int main()
- {
- int n;
- cin>>n;
- vector <vector <char> >a(n+2, vector <char> (n+2, 'O'));
- vector <vector <int> >p(n+2,vector <int>(n+2,-1));
- queue <point> q;
- point st,fn;
- in(a,p,st,fn);
- q.push(st);
- while (!q.empty())
- {
- point t=q.front();
- q.pop();
- point u;
- if (p[t.x-1][t.y]==0)
- {
- p[t.x-1][t.y]=p[t.x][t.y]+1;
- u.x=t.x-1;
- u.y=t.y;
- q.push(u);
- }
- if (p[t.x+1][t.y]==0)
- {
- p[t.x+1][t.y]=p[t.x][t.y]+1;
- u.x=t.x+1;
- u.y=t.y;
- q.push(u);
- }
- if (p[t.x][t.y-1]==0)
- {
- p[t.x][t.y-1]=p[t.x][t.y]+1;
- u.x=t.x;
- u.y=t.y-1;
- q.push(u);
- }
- if (p[t.x][t.y+1]==0)
- {
- p[t.x][t.y+1]=p[t.x][t.y]+1;
- u.x=t.x;
- u.y=t.y+1;
- q.push(u);
- }
- }
- if (p[fn.x][fn.y]==0) cout<<"No"; else
- {
- a[fn.x][fn.y]='+';
- while (fn.x!=st.x || fn.y!=st.y)
- {
- if (p[fn.x-1][fn.y]==p[fn.x][fn.y]-1) fn.x--; else
- if (p[fn.x+1][fn.y]==p[fn.x][fn.y]-1) fn.x++; else
- if (p[fn.x][fn.y-1]==p[fn.x][fn.y]-1) fn.y--; else
- if (p[fn.x][fn.y+1]==p[fn.x][fn.y]-1) fn.y++;
- if (!(fn.x==st.x && fn.y==st.y)) a[fn.x][fn.y]='+';
- }
- cout<<"Yes"<<endl;
- for (int i=1;i<=n;i++)
- {
- for (int j=1;j<=n;j++) cout<<a[i][j];
- cout<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement