Advertisement
a53

Acces1

a53
Oct 14th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. ifstream fin("acces1.in");
  7. ofstream fout("acces1.out");
  8.  
  9. const int Dim=1005,Inf=0x3f3f3f3f;
  10. const int di[]={-1,0,1,0};
  11. const int dj[]={0,1,0,-1};
  12.  
  13. int ipp[1001],jpp[1001],nr,mi=100000000,cnt,u=1,p;
  14. struct Cell
  15. {
  16. int gg,oo;
  17. };
  18.  
  19. int a[Dim][Dim];
  20. int c[Dim][Dim];
  21. int n, m, is, js;
  22. Cell Q[Dim * Dim];
  23.  
  24. void Read();
  25. void Lee();
  26. bool Ok(int i,int j);
  27. void Write();
  28.  
  29.  
  30. void refac()
  31. {
  32. memset(c,Inf,sizeof(c));
  33. }
  34.  
  35. int main()
  36. {
  37. int i,j;
  38. Read();
  39. Lee();
  40. for(i=1;i<=n;i++)
  41. {
  42. for(j=1;j<=m;j++)
  43. {
  44. if(c[i][j]==10000000)
  45. fout<<'-';
  46. else
  47. if(c[i][j]==Inf)
  48. fout<<'#';
  49. else
  50. fout<<c[i][j];
  51. fout<<' ';
  52. }
  53. fout<<'\n';
  54. }
  55. fin.close();
  56. fout.close();
  57. return 0;
  58. }
  59.  
  60.  
  61. void Lee()
  62. {
  63. int iv,jv,i,j;
  64. while(p<=u)
  65. {
  66. i=Q[p].gg;
  67. j=Q[p].oo;
  68. p++;
  69. for(int d=0;d<4;++d)
  70. {
  71. iv=i+di[d];
  72. jv=j+dj[d];
  73. if(Ok(iv,jv)&&(c[iv][jv]>c[i][j]+1))
  74. {
  75. c[iv][jv]=c[i][j]+1;
  76. Q[u++]={iv,jv};
  77. }
  78. }
  79. }
  80. }
  81.  
  82. bool Ok(int i,int j)
  83. {
  84. if(i<1||i>n||j<1||j>m)
  85. return false;
  86. if(a[i][j]==-1)
  87. return false;
  88. return true;
  89. }
  90.  
  91. void Read()
  92. {int i,j;
  93. char c1;
  94. fin>>n>>m;
  95. fin.get();
  96. for(i=1;i<=n;i++)
  97. for(j=1;j<=m;j++)
  98. {
  99. fin>>c1;
  100. if(c1=='-')
  101. a[i][j]=0,c[i][j]=10000000;
  102. else
  103. if(c1=='#')
  104. a[i][j]=-1,c[i][j]=Inf;
  105. else
  106. if(c1=='P')
  107. {
  108. Q[u].gg=i;
  109. Q[u++].oo=j;
  110. c[i][j]=0;
  111. a[i][j]=-1;
  112. nr++;
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement