language: C++ 4.7.2 (gcc-4.7.2)
date: 364 days 13 hours ago
link:
visibility: public
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
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef pair < int, int >ii;
int R, C, i, j;
queue < ii > myQueue;
 
int visit[100][100];
int dist[100][100];
void bfs(ii s)
{
    int i, j;
    int count = 0;
    ii node;
    memset(visit, 0, sizeof(visit));
    memset(dist, 0, sizeof(dist));
    myQueue.push(s);
    dist[node.first][node.second] = 0;
 
    while (!myQueue.empty()) {
        node = myQueue.front();
        myQueue.pop();
        if (visit[node.first][node.second])
            continue;
        visit[node.first][node.second] = 1;
 
        //cout << node.first << " " << node.second << "\n";
        i = node.first;
        j = node.second;
 
        if (j - 1 < R && j - 1 >= 0) {
            myQueue.push(make_pair(i, j - 1));
            if(dist[i][j - 1] == 0)
            dist[i][j - 1] = dist[i][j] + 1;
        }
        if (j + 1 < R && j + 1 >= 0) {
            myQueue.push(make_pair(i, j + 1));
            if(dist[i][j+1] == 0)
            dist[i][j + 1] = dist[i][j] + 1;
        }
        if (i - 1 < C && i - 1 >= 0) {
            myQueue.push(make_pair(i - 1, j));
            if(dist[i-1][j] == 0)
            dist[i - 1][j] = dist[i][j] + 1;
        }
        if (i + 1 < C && i + 1 >= 0) {
            myQueue.push(make_pair(i + 1, j));
            if(dist[i+1][j] == 0)
            dist[i + 1][j] = dist[i][j] + 1;
        }
 
    }
 
 
}
 
 
 
int main()
{
 
    char input[100][100];
    scanf("%d %d", &R, &C);
    for (i = 0; i < R; i++)
        scanf("%s", &input[i]);
    int GRID[R][C];
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++)
            GRID[i][j] = input[i][j] - '0';
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++) {
            if (GRID[i][j] == 1)
                bfs(make_pair(i, j));
        }
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            printf("%d", dist[i][j]);
        }
        printf("\n");
    }
   
}