d406.
倒水時間
| From: [124.11.192.89] |
發表日期
:
2017-08-06 11:38
#include <bits/stdc++.h>
using namespace std;
int arriveTime[105][105];
int pipe[105][105];
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
int N, M , S, casen = 1;
struct Node{
int x, y;
Node( int _y, int _x ){
x = _x;
y = _y;
}
};
int main(){
while ( cin >> S ){
cin >> N >> M;
for ( int i = 1 ; i <= N ; i++ )
for ( int j = 1 ; j <= M ;j ++ )
cin >> pipe[i][j];
for ( int i = 1 ; i <= N ; i++ )
for ( int j = 1 ; j <= M ; j++ )
arriveTime[i][j] = 0;
queue<Node> que;
for ( int i = 1 ; i <= M ; i++ )
if ( pipe[1][i] == 1 ){
arriveTime[1][i] = 1;
que.push(Node(1,i) );
}
while ( !que.empty() ){
Node a = que.front();
que.pop();
for ( int d = 0 ; d < 4 ; d++ ){
if ( S == 2 && dy[d] < 0 ) continue;
int nx = a.x + dx[d];
int ny = a.y + dy[d];
if ( nx < 1 || ny < 1 || nx > M || ny > N )
continue;
if ( pipe[ny][nx] != 1 ) continue;
if ( arriveTime[ny][nx] != 0 ) continue;
arriveTime[ny][nx] = arriveTime[a.y][a.x] + 1;
que.push( Node(ny,nx) );
}
}
cout << "Case " << casen++ << ":" << endl;
for ( int i = 1 ; i <= N ; i++ ){
for ( int j = 1 ; j <= M ; j++ ){
if ( arriveTime[i][j] == 0 )
cout << 0 << " ";
else
cout << arriveTime[i][j] << " ";
}
cout << endl;
}
}
return 0;
}