-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpreadDistance.cpp
107 lines (82 loc) · 2.19 KB
/
SpreadDistance.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
//-------------------------- USEFUL MACROS --------------------------------------
#define ff first
#define ss second
#define pb push_back
#define nf string::npos
#define ll long long
#define str to_string
#define inf 1e18
//#define mod 1000000007 //1e9+7
#define round(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcountll(x)
#define deb(x) cout << #x << " = " << x << "\n"
#define debc(c) cout << #c << "= [" ; for(auto i : c) cout<< i <<" "; cout<<"]\n"
#define sortall(v) sort(v.begin(), v.end())
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define input_vec(vec,n) for(ll i=0; i<n; i++ ){ ll temp; cin>>temp; vec.pb(temp); }
#define input_arr(arr,n) for(ll i=0; i<n; i++){ cin>>arr[i]; }
#define fast_io ios_base::sync_with_stdio(false) ; cin.tie(NULL)
// --------------------------------------------------------------------------------
void solve()
{
int n , m ;
cin>> n >> m ;
int input[n][m] ;
int output[n][m];
int di[]= { 0 ,0 , 1 , -1};
int dj[]={ 1,-1, 0, 0} ;
queue<pair<int,int>> q ;
for(int i = 0 ; i<n ; i++)
{
for(int j = 0 ; j<m ; j++)
{
cin>> input[i][j] ;
if(input[i][j]){
output[i][j] = 0 ;
q.push({i,j}) ;
}
else
output[i][j] = INT_MAX ;
}
}
while(not q.empty())
{
pair<int,int> tp = q.front() ;
int i = tp.first ;
int j = tp.ss ;
q.pop() ;
for(int k = 0 ; k<4 ; k++)
{
if( ((i+di[k]) >= 0) and ((i+di[k]) <n) and ((j+dj[k]) >=0) and ((j+dj[k]) <m) )
{
int i_ = i + di[k] , j_ = j+ dj[k] ;
if(output[i_][j_] > 1 + output[i][j])
{
q.push({i_,j_}) ;
output[i_][j_] = 1 + output[i][j] ;
}
}
}
}
for(int i =0 ; i<n ; i++)
{
for(int j = 0 ; j<m ; j++)
{
cout<<output[i][j]<<" " ;
}
cout<<endl;
}
}
signed main()
{
fast_io;
int t = 1 ;
while(t--)
{
solve();
}
return 0;
}
//<-------------------------------->