题目链接:戳此进入
思路:数据范围比较小,每次进两个点BFS,搜完后判断一下,是否还有没有烧的草,如果有就标记。
优化思路:纯暴力会有点重复,我们只需要保证这两个点如果搜索过就跳过。
优化AC :时间差一半
#include<iostream>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 15;
const int INF = 999;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int dist[maxn][maxn];
bool supvis[maxn][maxn][maxn][maxn];
int n, m;
int X[4] = {1, -1, 0, 0};
int Y[4] = {0, 0, 1, -1};
struct node{
int x, y;
int step;
node(){
step = 0;
}
};
bool check(node now){
if(now.x < 1 || now.x > n || now.y < 1 || now.y > m){
return 0;
}
return 1;
}
int BFS(node s1, node s2){
memset(vis, 0, sizeof(vis));
memset(dist, 0, sizeof(dist));
vis[s1.x][s1.y] = 1;
vis[s2.x][s2.y] = 1;
dist[s1.x][s2.y] = 0;
dist[s1.x][s2.y] = 0;
queue<node> q;
q.push(s1);
q.push(s2);
node front, next;
int ans = 0;
while(!q.empty()){
front = q.front();
q.pop();
for(int i = 0; i < 4; i ++){
next.x = front.x + X[i];
next.y = front.y + Y[i];
if(!check(next))
continue;
if(vis[next.x][next.y])
continue;
if(maze[next.x][next.y] == '.')
continue;
vis[next.x][next.y] = 1;
next.step = front.step + 1;
q.push(next);
ans = max(ans, next.step);
}
}
bool flag = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(maze[i][j] == '#' && vis[i][j] == 0){
flag = 1;
}
}
}
if(flag)
return -1;
else
return ans;
}
int main(){
int T;
scanf("%d", &T);
int q = 1;
while(T--){
scanf("%d%d", &n, &m);
getchar();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m;j ++){
scanf("%c", &maze[i][j]);
}
getchar();
}
int ans = INF;
memset(supvis, 0, sizeof(supvis));
node fire1, fire2;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(maze[i][j] == '#'){
fire1.x = i;
fire1.y = j;
for(int k = 1; k <= n; k++){
for(int z = 1; z <= m; z++){
if(maze[k][z] == '#' && supvis[i][j][k][z] == 0){
fire2.x = k;
fire2.y = z;
supvis[i][j][k][z] = 1;
supvis[k][z][i][j] = 1;
int flag = BFS(fire1, fire2);
if(flag != -1) {
ans = min(ans, flag);
}
}
}
}
}
}
}
printf("Case %d: ", q++);
if(ans == INF){
printf("-1\n");
}else{
printf("%d\n", ans);
}
}
return 0;
}
普通AC:
#include<iostream>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 15;
const int INF = (1 << 31) - 1;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int dist[maxn][maxn];
int n, m;
int X[4] = {1, -1, 0, 0};
int Y[4] = {0, 0, 1, -1};
struct node{
int x, y;
int step;
node(){
step = 0;
}
};
bool check(node now){
if(now.x < 1 || now.x > n || now.y < 1 || now.y > m || maze[now.x][now.y] == '.'){
return 0;
}
return 1;
}
int BFS(node s1, node s2){
memset(vis, 0, sizeof(vis));
memset(dist, 0, sizeof(dist));
vis[s1.x][s1.y] = 1;
vis[s2.x][s2.y] = 1;
dist[s1.x][s2.y] = 0;
dist[s1.x][s2.y] = 0;
queue<node> q;
q.push(s1);
q.push(s2);
node front, next;
int ans = 0;
while(!q.empty()){
front = q.front();
q.pop();
for(int i = 0; i < 4; i ++){
next.x = front.x + X[i];
next.y = front.y + Y[i];
if(check(next) && vis[next.x][next.y] == 0){
dist[next.x][next.y] = dist[front.x][front.y] + 1;
vis[next.x][next.y] = 1;
q.push(next);
}
}
}
bool flag = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(maze[i][j] == '#' && vis[i][j] == 0){
flag = 1;
}
ans = max(dist[i][j], ans);
}
}
if(flag)
return -1;
else
return ans;
}
int main(){
int T;
cin >> T;
int q = 1;
while(T--){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m;j ++){
cin >> maze[i][j];
}
}
int ans = INF;
node fire1, fire2;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(maze[i][j] == '#'){
fire1.x = i;
fire1.y = j;
for(int k = 1; k <= n; k++){
for(int z = 1; z <= m; z++){
if(maze[k][z] == '#'){
fire2.x = k;
fire2.y = z;
int flag = BFS(fire1, fire2);
if(flag != -1){
ans = min(ans, flag);
}
}
}
}
}
}
}
cout << "Case " << q++ << ": ";
if(ans == INF){
cout << -1 << endl;
}else{
cout << ans << endl;
}
}
return 0;
}