d406: 倒水時間 https://zerojudge.tw/ShowProblem?problemid=d406
WA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int x_bound, y_bound;
int pipe[105][105];
int vst[105][105];
int time_table[105][105];
struct point{
int y;
int x;
int ptime;
point(int _y, int _x, int _ptime){
y = _y;
x = _x;
ptime = _ptime;
}
};
int find_start(){
for(int i=0 ; i<x_bound ; i++){
if(pipe[0][i]==1){
return i;
}
}
return 0;
}
deque<point> q;
void bfs(int ability){
int start = find_start();
int px, py, pt;
vst[0][start] = 1;
q.push_back(point(0,start,1));
if(ability==2){
while(!q.empty()){
px = q.front().x;
py = q.front().y;
pt = q.front().ptime;
vst[py][px] = 1;
time_table[py][px] = pt;
q.pop_front();
if(px-1>=0 && vst[py][px-1]==0 && pipe[py][px-1]==1){
q.push_back(point(py, px-1, pt+1));
vst[py][px-1] = 1;
}
if(px+1<x_bound && vst[py][px+1]==0 && pipe[py][px+1]==1){
q.push_back(point(py, px+1, pt+1));
vst[py][px+1] = 1;
}
if(py+1<y_bound && vst[py+1][px]==0 && pipe[py+1][px]==1){
q.push_back(point(py+1, px, pt+1));
vst[py+1][px] = 1;
}
}
}
else if(ability==1){
while(!q.empty()){
px = q.front().x;
py = q.front().y;
pt = q.front().ptime;
vst[py][px] = 1;
time_table[py][px] = pt;
q.pop_front();
if(px+1<x_bound && vst[py][px+1]==0 && pipe[py][px+1]==1){
q.push_back(point(py, px+1, pt+1));
vst[py][px+1] = 1;
}
if(py+1<y_bound && vst[py+1][px]==0 && pipe[py+1][px]==1){
q.push_back(point(py+1, px, pt+1));
vst[py+1][px] = 1;
}
if(py-1>=0 && vst[py-1][px]==0 && pipe[py-1][px]==1){
q.push_back(point(py-1, px, pt+1));
vst[py-1][px] = 1;
}
}
}
else if(ability==3){
while(!q.empty()){
px = q.front().x;
py = q.front().y;
pt = q.front().ptime;
vst[py][px] = 1;
time_table[py][px] = pt;
q.pop_front();
if(px-1>=0 && vst[py][px-1]==0 && pipe[py][px-1]==1){
q.push_back(point(py, px-1, pt+1));
vst[py][px-1] = 1;
}
if(py+1<y_bound && vst[py+1][px]==0 && pipe[py+1][px]==1){
q.push_back(point(py+1, px, pt+1));
vst[py+1][px] = 1;
}
if(py-1>=0 && vst[py-1][px]==0 && pipe[py-1][px]==1){
q.push_back(point(py-1, px, pt+1));
vst[py-1][px] = 1;
}
}
}
}
int main(void){
int ability;
int testcase = 0;
for(int i=0 ; i<105 ; i++ ){
for(int j=0 ; j<105 ; j++ ){
vst[i][j] = 0;
time_table[i][j] = 0;
pipe[i][j] = 0;
}
}
while(scanf("%d", &ability) != EOF){
testcase++;
q.clear();
scanf("%d %d", &y_bound, &x_bound);
for(int i=0; i<y_bound ; i++ ){
for(int j=0 ; j<x_bound ; j++ ){
scanf("%d", &pipe[i][j]);
vst[i][j] = 0;
time_table[i][j] = 0;
}
}
bfs(ability);
printf("Case %d:\n", testcase);
for(int i=0; i<y_bound ; i++ ){
printf("%d", time_table[i][0]);
for(int j=1 ; j<x_bound ; j++ ){
printf(" %d", time_table[i][j]);
}
printf("\n");
}
}
return 0;
}