nicuvlad76

Untitled

Jan 27th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 101
  3. using namespace std;
  4.  
  5. ifstream fin("rj.in");
  6. ofstream fout("rj.out");
  7.  
  8. const int di[] = {0 , 0 , -1 , 1};
  9. const int dj[] = {-1 , 1 , 0 , 0};
  10. int a[N][N],b[N][N],n,m;
  11. int li,ci,ls,cs;
  12. char s[N];
  13. int dl[]={-1,-1,-1,1,1,1,0,0};
  14. int dc[]={1,0,-1,1,0,-1,1,-1};
  15. void Citire()
  16. {
  17. fin>>n>>m;fin.get();
  18. for(int i=1;i<=n;i++)
  19. {
  20. fin.getline(s,N);
  21. for(int j=0;j<m;j++)
  22. if(s[j]==' ')a[i][j+1]=b[i][j+1]=0;
  23. else if(s[j]=='X')a[i][j+1]=b[i][j+1]=-1;
  24. else if(s[j]=='R'){li=i; ci=j+1;}
  25. else {ls=i; cs=j+1;}
  26. }
  27. }
  28.  
  29. bool Inside(int i, int j){return 1<=i && i<=n && 1<=j && j<=m;}
  30. void Lee(int lin, int col, int c[N][N])
  31. {
  32. int L[N*N]={0}, C[N*N]={0};
  33. int pr, ul;
  34. int v1, v2;
  35. int lv, cv;
  36. pr=ul=1;
  37. L[1]=lin;
  38. C[1]=col;
  39. c[lin][col]=1;
  40. while(pr<=ul)
  41. { v1=L[pr]; v2=C[pr++];
  42. for(int i=0;i<8;i++)
  43. {
  44. lv=v1+dl[i];
  45. cv=v2+dc[i] ;
  46. if(c[lv][cv]==0 && Inside(lv, cv))
  47. {
  48. c[lv][cv]=c[v1][v2]+1;
  49. L[++ul]=lv; C[ul]=cv;
  50. }
  51. }
  52. }
  53. }
  54. void Afisare()
  55. {
  56. Lee(li,ci,a);
  57. Lee(ls,cs,b);
  58. int mini=N*N, lf, cf;
  59. for(int i=1;i<=n;i++)
  60. for(int j=1;j<=m;j++)
  61. if(a[i][j]==b[i][j] && a[i][j]<mini && a[i][j]>0)
  62. {
  63. mini=a[i][j];lf=i;cf=j;
  64.  
  65. }
  66. fout<<mini<<" "<<lf<<" "<<cf;
  67. }
  68. int main()
  69. {
  70. Citire();
  71. Afisare();
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment