-
Notifications
You must be signed in to change notification settings - Fork 2
/
BITMAP - Bitmap.cpp
69 lines (66 loc) · 1.54 KB
/
BITMAP - Bitmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//BITMAP - Bitmap
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll N,M;
ll arr[200][200];
ll dist[200][200];
int main()
{
ll T;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld",&N,&M);
memset(dist,-1,sizeof dist);
memset(arr,-1,sizeof arr);
queue <pair <ll,ll> > q;
ll i,j;
for(i=1;i<=N;i++)
{
string str;
cin>>str;
for(j=1;j<=str.size();j++)
{
arr[i][j]=str[j-1]-'0';
if(arr[i][j]==1)
{
q.push(make_pair(i,j));
dist[i][j]=0;
}
}
}
while(!q.empty())
{
pair <ll,ll> pos=q.front();
q.pop();
if(dist[pos.first+1][pos.second]==-1 && arr[pos.first+1][pos.second]!=-1)
{
dist[pos.first+1][pos.second]=dist[pos.first][pos.second]+1;
q.push(make_pair(pos.first+1,pos.second));
}
if(dist[pos.first-1][pos.second]==-1 && arr[pos.first-1][pos.second]!=-1)
{
dist[pos.first-1][pos.second]=dist[pos.first][pos.second]+1;
q.push(make_pair(pos.first-1,pos.second));
}
if(dist[pos.first][pos.second+1]==-1 && arr[pos.first][pos.second+1]!=-1)
{
dist[pos.first][pos.second+1]=dist[pos.first][pos.second]+1;
q.push(make_pair(pos.first,pos.second+1));
}
if(dist[pos.first][pos.second-1]==-1 && arr[pos.first][pos.second-1]!=-1)
{
dist[pos.first][pos.second-1]=dist[pos.first][pos.second]+1;
q.push(make_pair(pos.first,pos.second-1));
}
}
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
printf("%lld ",dist[i][j]);
printf("\n");
}
}
return 0;
}