Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstring>
- #include <iomanip>
- using namespace std;
- ifstream fin("acces1.in");
- ofstream fout("acces1.out");
- const int Dim=1005,Inf=0x3f3f3f3f;
- const int di[]={-1,0,1,0};
- const int dj[]={0,1,0,-1};
- int ipp[1001],jpp[1001],nr,mi=100000000,cnt,u=1,p;
- struct Cell
- {
- int gg,oo;
- };
- int a[Dim][Dim];
- int c[Dim][Dim];
- int n, m, is, js;
- Cell Q[Dim * Dim];
- void Read();
- void Lee();
- bool Ok(int i,int j);
- void Write();
- void refac()
- {
- memset(c,Inf,sizeof(c));
- }
- int main()
- {
- int i,j;
- Read();
- Lee();
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- if(c[i][j]==10000000)
- fout<<'-';
- else
- if(c[i][j]==Inf)
- fout<<'#';
- else
- fout<<c[i][j];
- fout<<' ';
- }
- fout<<'\n';
- }
- fin.close();
- fout.close();
- return 0;
- }
- void Lee()
- {
- int iv,jv,i,j;
- while(p<=u)
- {
- i=Q[p].gg;
- j=Q[p].oo;
- p++;
- for(int d=0;d<4;++d)
- {
- iv=i+di[d];
- jv=j+dj[d];
- if(Ok(iv,jv)&&(c[iv][jv]>c[i][j]+1))
- {
- c[iv][jv]=c[i][j]+1;
- Q[u++]={iv,jv};
- }
- }
- }
- }
- bool Ok(int i,int j)
- {
- if(i<1||i>n||j<1||j>m)
- return false;
- if(a[i][j]==-1)
- return false;
- return true;
- }
- void Read()
- {int i,j;
- char c1;
- fin>>n>>m;
- fin.get();
- for(i=1;i<=n;i++)
- for(j=1;j<=m;j++)
- {
- fin>>c1;
- if(c1=='-')
- a[i][j]=0,c[i][j]=10000000;
- else
- if(c1=='#')
- a[i][j]=-1,c[i][j]=Inf;
- else
- if(c1=='P')
- {
- Q[u].gg=i;
- Q[u++].oo=j;
- c[i][j]=0;
- a[i][j]=-1;
- nr++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement