[FZOJ2150]Fire Game 技巧BFS
2015-11-26 15:18
176 查看
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
题意:两个熊孩子放火烧草坪,每秒会向上下左右四个方向的草坪扩展一格,问选哪两个点可以让草坪最快烧光,如果烧不光要特别说明。
记下所有#的位置,枚举每两个不相同位置的#,跑BFS。
题意:两个熊孩子放火烧草坪,每秒会向上下左右四个方向的草坪扩展一格,问选哪两个点可以让草坪最快烧光,如果烧不光要特别说明。
记下所有#的位置,枚举每两个不相同位置的#,跑BFS。
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; typedef struct P { int x; int y; int w; P() {} P(int xx, int yy, int ww) : x(xx), y(yy), w(ww) {} }P; const int maxn = 15; const int inf = 0x7f7f; const int dx[4] = {0, 0, 1, -1}; const int dy[4] = {1, -1, 0, 0}; int n, m; int ans; bool vis[maxn][maxn]; char G[maxn][maxn]; vector<P> v; int bfs(P a, P b) { int cur; queue<P> q; vis[a.x][a.y] = vis[b.x][b.y] = 1; q.push(a), q.push(b); while(!q.empty()) { P t = q.front(); q.pop(); for(int i = 0; i < 4; i++) { cur = t.w; int xx = t.x + dx[i]; int yy = t.y + dy[i]; if(xx >= 0 && yy >= 0 && xx < n && yy < m && !vis[xx][yy] && G[xx][yy] == '#') { vis[xx][yy] = 1; q.push(P(xx, yy, t.w+1)); } } } return cur; } int main() { // freopen("in", "r", stdin); int T; scanf("%d", &T); for(int _ = 1; _ <= T; _++) { printf("Case %d: ", _); v.clear(); ans = inf; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) { scanf("%s", G[i]); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(G[i][j] == '#') { v.push_back(P(i, j, 0)); } } } for(int i = 0; i < v.size(); i++) { for(int j = i; j < v.size(); j++) { memset(vis, 0, sizeof(vis)); int t = bfs(v[i], v[j]); int flag = 1; for(int k = 0; k < n; k++) { if(!flag) break; for(int l = 0; l < m; l++) { if(G[k][l] == '#' && !vis[k][l]) { flag = 0; break; } } } if(flag) { ans = min(ans, t); } } } if(ans == inf) { printf("-1\n"); } else { printf("%d\n", ans); } } return 0; }
相关文章推荐
- 1.3 hibernate annotation环境搭建及简单的例子
- 读取文件2+root画图
- ubuntu系统中PATH变量被修改,导致系统一直停留在登陆界面
- Assembly的load, loadfrom, loadfile方法总结
- 请求,转发及session
- php 接受json数据时有转义字符处理办法
- 支付宝拼接URL的¬ify_url显示不正常
- hadoop之combiners编程
- 英文短信和聊天时常用的缩写
- node
- Qt树形控件QTreeView使用1——节点的添加删除操作
- Predicate<T>与Func<T, bool>泛型委托
- node
- Inno Setup命令行安装卸载参数
- iOS开发之涂鸦板
- PAT1098 Insertion or Heap Sort
- Linux 的pthread_create 和 pthread_join 函数的一些新的理解
- ORACLE11g:No Dialect mapping for JDBC type: -9,2011解决方案
- 学习JavaScript设计模式(单例模式)
- Andriod中绘(画)图----Canvas的使用详解