题目有点坑,注意先输入N,后输入M,优化在图的最外层再添加一层
// File Name: nyoj92.cpp
// Author: bo_jwolf
// Created Time: 2013年04月30日 星期二 15:20:24
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<queue>
using namespace std;
const int maxn = 1500 ;
int mapp[ maxn ][ maxn ];
int visited[ maxn ][ maxn ];
int m , n ;
int dis[ 4 ][ 2] = { { -1 , 0 } , { 1 , 0 } , { 0 , 1 } , { 0 , -1 } };
struct node
{
int x , y ;
}next , cur ;
void bfs( int x , int y )
{
// cout << "aaaaaaaaaaaa";
int i;
queue< node > Q ;
cur.x = x ;
cur.y = y ;
Q.push( cur );
while( !Q.empty() )
{
cur = Q.front();
Q.pop();
for( i = 0 ; i < 4 ; i++ )
{
next.x = cur.x + dis[ i ][ 0 ] ;
next.y = cur.y + dis[ i ][ 1 ] ;
if( next.x < 0 || next.x > m + 1 || next.y < 0 || next.y > n + 1 || mapp[ next.x ][ next.y ] == 0 )//注意顺序,放在最前面判断,可能会出现bug
continue ;
mapp[ next.x ][ next.y ] = 0 ;
Q.push( next ) ;
// cout<< "aaaaaaaaaaa";
}
}
}
int main()
{
int Case ;
cin >> Case ;
int i , j ;
while( Case-- )
{
cin >> n >> m;
for( i = 0 ; i <= m ; i++ )
mapp[ i ][ 0 ] = mapp[ i ][ n + 1 ] = 1 ;
for( j = 0 ; j <= n ; j++ )
mapp[ 0 ][ j ] = mapp[ m + 1 ][ j ] = 1;
for( i = 1 ;i <= m ; i++ )
for( j = 1 ; j <= n ; j++ )
cin >> mapp[ i ][ j ] ;
bfs( 0 , 0 ) ;
for( i = 1 ; i <= m ; i++ )
for( j = 1 ; j <= n ; j++ )
if( j == n )
cout << mapp[ i ][ j ] << endl ;
else
cout << mapp[ i ][ j ] << ' ' ;
}
return 0;
}