题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
题意:在任意两处点火,求最短时间烧光所有草堆。
规模:1 <= T <=100, 1 <= n <=10, 1 <= m <=10
类型:暴力+双搜
分析:题目很好理解,枚举两个起点进行双搜就好。但以前没有敲过双搜,中间出了不少状况,wa了很多发,主要问题是在“如何使两个queue完成当前时间下的搜索”。
时间复杂度&&优化:
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAX_N = ;
const int MAX_M = ;
const int inf = ;
const int mod = ;
int n,m;
int a[][];
int b[][];
struct point{
int x,y;
int step;
};
int dx[]={,,,-};
int dy[]={,-,,};
bool judge(int x,int y){
if(x>=&&x<n&&y>=&&y<m&&b[x][y]==){
b[x][y]=;
//cout<<"!"<<endl;
return true;
}
return false;
}
int bfs(int ax,int ay,int bx,int by){
int res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
b[i][j]=a[i][j];
}
}
b[ax][ay]=;
b[bx][by]=;
queue<point>q,p;
while(!q.empty()){q.pop();}
while(!p.empty()){p.pop();}
point st1,st2;
st1.x=ax;st1.y=ay;st1.step=;
st2.x=bx;st2.y=by;st2.step=;
q.push(st1);p.push(st2);
int stp=;
point tmp,tmp2;
while(!q.empty() || !p.empty()){
if(!q.empty()){
int key_step=q.front().step;
while(!q.empty()&&q.front().step==key_step){
tmp=q.front();
//cout<<tmp.x<<" "<<tmp.y<<" "<<tmp.step<<endl;
q.pop();
stp=tmp.step;
res=max(res,stp);
for(int i=;i<;i++){
tmp2.x=tmp.x+dx[i];
tmp2.y=tmp.y+dy[i];
tmp2.step=tmp.step+;
if(judge(tmp2.x,tmp2.y))q.push(tmp2);
}
// if(ax==1&&ay==1&&bx==7&&by==2){
// cout<<stp<<" "<<tmp.x<<" "<<tmp.y<<endl;
// }
}
}
if(!p.empty()){
int key_step=p.front().step;
while(!p.empty()&&p.front().step==key_step){
tmp=p.front();
p.pop();
stp=tmp.step;
res=max(res,stp);
for(int i=;i<;i++){
tmp2.x=tmp.x+dx[i];
tmp2.y=tmp.y+dy[i];
tmp2.step=tmp.step+;
if(judge(tmp2.x,tmp2.y))p.push(tmp2);
}
// if(ax==1&&ay==1&&bx==7&&by==2){
// cout<<stp<<" "<<tmp.x<<" "<<tmp.y<<endl;
// }
}
}
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(b[i][j]==)return -;
}
}
// //cout<<stp<<" "<<ax<<" "<<ay<<" "<<bx<<" "<<by<<endl;
// if(stp==3)cout<<3<<" "<<ax<<" "<<ay<<" "<<bx<<" "<<by<<endl;
// if(stp==4)cout<<4<<" "<<ax<<" "<<ay<<" "<<bx<<" "<<by<<endl;
return res;
}
int solve(){
int res=inf;
for(int i=;i<n*m;i++){
if(a[i/m][i%m]==)
for(int j=i+;j<n*m;j++){
if(a[j/m][j%m]==){
// cout<<i<<" "<<j<<endl;
int k=bfs(i/m,i%m,j/m,j%m);
if(k!=-)res=min(res,k);
}
}
}
if(res==inf)return -;
else return res;
}
int main()
{
//freopen("test.txt","r",stdin);
int T;char c;
cin>>T;
for(int z=;z<=T;z++){
memset(a,,sizeof(a));
cin>>n>>m;
int tot=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>c;
if(c=='#'){a[i][j]=;tot++;}
else a[i][j]=;
}
}
if(tot<=){printf("Case %d: %d\n",z,);}
else printf("Case %d: %d\n",z,solve());
}
return ;
}
