FZU 2150 Fire Game 枚举BFS
来源:互联网 发布:淘宝纳米碳溶胶 编辑:程序博客网 时间:2024/06/05 13:27
考虑的n,和m较小,可以枚举2个放火的位置,然后BFS,
#include<iostream>#include<cstdio>#include<math.h>#include<algorithm>#include<map>#include<set>#include<stack>#include<queue>#include<string.h>#include<cstring>#include<vector>using namespace std;#define INF 0x3f3f3f3f#define mem(x,y) memset(x,y,sizeof(x))typedef long long LL;#define MX 1111#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef pair<int,int> PII;struct Point{ int x,y,t; Point(int x,int y,int t):x(x),y(y),t(t) {}};int n,m;int to[4][2]= {1,0,-1,0,0,1,0,-1};char _map[11][11];bool vis[11][11];int bfs(int x1,int y1,int x2,int y2){ mem(vis,0); queue<Point> my; my.push(Point(x1,y1,0)); my.push(Point(x2,y2,0)); vis[x1][y1]=vis[x2][y2]=1; int ans=0; while(!my.empty()) { Point u=my.front(); my.pop(); for(int i=0; i<4; i++) { int xx=u.x+to[i][0],yy=u.y+to[i][1],tt=u.t+1; if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||_map[xx][yy]=='.') continue; ans=max(ans,tt); vis[xx][yy]=1; my.push(Point(xx,yy,tt)); } } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(_map[i][j]=='#'&&vis[i][j]==0) return INF; return ans;}int main(){ // freopen("input.txt","r",stdin); int T;//// cin>>T; int tt=0; while(T--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%s",_map[i]+1);
//一开始也可以先bfs下判断联通块的个数,大于2的直接输出-1就好 int ans=INF; for(int x1=1; x1<=n; x1++) for(int y1=1; y1<=m; y1++) for(int x2=x1; x2<=n; x2++)//这里加上一个强行减枝,x2要>=x1,不然会超时。 for(int y2=1; y2<=m; y2++) if(_map[x1][y1]=='#'&&_map[x2][y2]=='#')ans=min(ans,bfs(x1,y1,x2,y2)); printf("Case %d: %d\n",++tt,ans==INF?-1:ans); } return 0;}
0 0
- FZU 2150 Fire Game 枚举BFS
- FZU - 2150----Fire Game(BFS+枚举)
- 枚举,bfs(FZU 2150,Fire Game)
- FZU 2150 Fire Game(枚举两个源点的bfs)
- FZU NO.2150 Fire Game(枚举+BFS)
- fzu 2150 Fire Game(BFS)
- fzu 2150 Fire Game(BFS)
- FZU 2150 Fire Game(BFS)
- FZU--2150--Fire Game【BFS】
- FZU 2150Fire game-BFS
- FZU 2150 Fire Game(BFS)
- FZU 2150 Fire Game(bfs)
- FZU - 2150 Fire Game(BFS)
- FZU 2150 Fire Game(bfs)
- FZU-2150-Fire Game【BFS】
- FZU 2150 Fire Game (BFS)
- FZU 2150 Fire Game BFS
- 【BFS】FZU 2150 Fire Game
- jee、spring、spring mvc、mybatis 学习(二)
- nodejs安装任何模块失败问题-error Windows_NT 6.1.7601
- 162. Find Peak Element
- go的变量、常量、类型别名
- Android .不一样的辅助服务,打开一个全新视角
- FZU 2150 Fire Game 枚举BFS
- MFC中listControl控件右键双击响应事件
- Python3中替代Python2中cmp()函数的新函数(gt,ge,eq,le,lt)
- JS学习笔记-CSS篇(一)
- 快速排序一次排序的应用
- bzoj1012(线段树或单调队列)
- 蒙特·卡罗方法-转
- JP1.1
- kei文件在用word打开时中文是乱码的解决办法