洛谷920A Water The Garden
题目大意:一片长条形田地 有几个水龙头 一秒钟可以向周围浇一格 问多少秒能浇完整个地
解题思路:普普通通bfs
代码块:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int, int> p;
int arrA[205]={0};
int nowIndex = 1;
int sum = 0;
queue< p > q;
int n, k;
void bfs();
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>k;
for(int i = 0; i < k; i++){
int x;
cin>>x;
arrA[x] = 1;
q.push(p(x,nowIndex));
sum++;
}
bfs();
cout<<nowIndex<<endl;
while(q.size()){
q.pop();
}
sum = 0;
memset(arrA,0,sizeof(arrA));
nowIndex = 1;
}
return 0;
}
void bfs(){
while(q.size()){
if(sum == n){
return;
}
int x = q.front().first;
int index1 = q.front().second;
if(x - 1 >= 1 && arrA[x - 1] == 0){
sum++;
nowIndex = index1 + 1;
arrA[x - 1] = 1;
q.push(p(x - 1, index1+1));
}
if(x + 1 <= n && arrA[x + 1] == 0){
sum++;
nowIndex = index1 + 1;
arrA[x + 1] = 1;
q.push(p(x + 1, index1+1));
}
q.pop();
}
}