fork download
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. int cha[1001][1001];
  5. char a[1001][1001];
  6. int dx[4]={-1,1,0,0};
  7. int dy[4]={0,0,-1,1};
  8. char huong[4]={'U','D','L','R'};
  9. int n,m,a1,a2;
  10. string cc="";
  11. signed main()
  12. {
  13.  
  14. queue<pair<int,int>>q;
  15. cin>>n>>m;
  16. for (int i=1;i<=n;i++)
  17. {
  18. for (int j=1;j<=m;j++)
  19. {
  20. cin>>a[i][j];
  21. if (a[i][j]=='M')
  22. {
  23. q.push({i,j});
  24. }
  25. else if (a[i][j]=='A')
  26. {
  27. a1=i;
  28. a2=j;
  29. }
  30. }
  31. }
  32. q.push({a1,a2});
  33. while (!q.empty())
  34. {
  35. int x=q.front().first;
  36. int y=q.front().second;
  37. q.pop();
  38. if (a[x][y]=='A'&&(x==1||x==n||y==1||y==m))
  39. {
  40. cout<<"YES"<<"\n";
  41. while (x!=a1||y!=a2)
  42. {
  43. int k=cha[x][y];
  44. cc=huong[k]+cc;
  45. x=x-dx[k];
  46. y=y-dy[k];
  47. }
  48. cout<<cc.size()<<"\n";
  49. cout<<cc;
  50. return 0;
  51. }
  52. for (int i=0;i<4;i++)
  53. {
  54. int xx=x+dx[i];
  55. int yy=y+dy[i];
  56. if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]=='.')
  57. {
  58. a[xx][yy]=a[x][y];
  59. if (a[xx][yy]=='A') cha[xx][yy]=i;
  60. q.push({xx,yy});
  61. }
  62. }
  63. }
  64. cout<<"NO";
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
NO