#include<stdio.h> #include<queue> using namespace std ; bool map[50][20] ; int vis[50][20] ; int a[50][20] ; int row , col ; int T ; char st[20] ; int move [][2] = {-1 , 0 , 0 , -1 , 1 , 0 , 0 , 1} ; void bfs (int sx , sy) { queue <int> q ; while (!q.empty ()) q.pop () ; vis[sx][sy] = 1 ; q.push (make_pair(sx , sy)) ; while (!q.empty ()) { pair <int> k = q.front () ; q.pop () ; for (int i = 0 ; i < 4 ; i++) { int tx = k.first + move[i][0] ; int ty = k.second + move[i][1] ; if (tx == 0 || ty == 0 || tx > row || ty > col || a[tx][ty] == -1) continue ; vis[tx][ty] = (vis[k.first][k.second] + 1) % 2 ; q.push (make_pair (tx , ty)) ; } } } int main () { freopen ("a.txt" , "r" , stdin) ; int flag ; while (~ scanf ("%d%d" , &row , &col)) { getchar () ; memset (map , -1 , sizeof(map)) ; memset (vis , -1 , sizeof(vis)) ; memset (a , -1 , sizeof(a)) ; int k = 1 ; for (int i = 0 ; i < row ; i++) { gets (st) ; for (int j = 0 ; j < col ; j++) { if (st[j] == ‘*‘) { a[i + 1][j + 1] = k++ ; } } } for (int i = 1 ; i <= row ; i++) { for (int j = 1 ; j <= col ; j++) { if (a[i][j] != -1 && vis[i][j] == -1) { bfs (i , j) ; } } } for (int i = 1 ; i <= row ; i++) { for (int j = 1 ; j <= col ; j++) { printf ("%d " , vis[i][j]) ; } puts ("") ; } puts ("") ; } return 0 ; }
时间: 2024-10-03 15:01:08