【I - Fire Game】

80 篇文章 0 订阅
80 篇文章 0 订阅

思路:

  • 想到了枚举,没敢写,是不会分析算法可行性。

代码:

  • 枚举 + bfs:156ms 228kB
//156ms		228kB 


#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 15;
const int moveto[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};

bool mp [maxn][maxn];
bool vis[maxn][maxn];
int M,N;
int ans;
struct pt{
	int x,y;
	int step;
	pt(int x,int y,int step) : x(x) , y(y) , step(step) {} ;
};
vector<pt> V;
queue<pt>  Q;

void bfs(pt a , pt b){
	memset(vis , false , sizeof(vis));
	int curans = 0;
	Q.push(a);
	Q.push(b);
	vis[a.x][a.y] = vis[b.x][b.y] = true;
	while(!Q.empty()){
		pt cur = Q.front() ; Q.pop();
		int x = cur.x;
		int y = cur.y;
		for(int i=0;i<4;i++){
			int nx = x + moveto[i][0];
			int ny = y + moveto[i][1];
			if(!mp[nx][ny])
				continue;
			if(vis[nx][ny])
				continue;
			vis[nx][ny] = true;
			Q.push(pt(nx , ny , cur.step + 1));
			curans = cur.step + 1;
		}
	}
	for(int i=1;i<=M;i++)
		for(int j=1;j<=N;j++)
			if(mp[i][j] != vis[i][j])
				return;
	ans = ans > curans ? curans : ans;
	return ;
}

int main(){
	int T;cin>>T;
	for(int t=1;t<=T;t++){
		V.clear();
		memset(mp , false , sizeof(mp));
		ans = INF;//int ans = INF 不会报错,是一个很隐蔽的BUG 
		cin>>M>>N;
		for(int i=1;i<=M;i++)
			for(int j=1;j<=N;j++){
				char temp;
				cin>>temp;
				mp[i][j] = temp == '#' ? true : false;
				if(mp[i][j])
					V.push_back(pt(i,j,0));
			}
		for(vector<pt>::iterator i = V.begin() ; i != V.end() ; i++)
			for(vector<pt>::iterator j = i ; j != V.end() ; j++)
				bfs(*i , *j);
		if(ans == INF)
			ans = -1;
		cout<<"Case "<<t<<": "<<ans<<endl;//写成"case"还 WA了一发,都不是 PE。不要急。 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值