FZU 2150 Fire Game(BFS)
题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-1
思路 :这个题因为可以同时向上下左右同时烧过去,所以一看我就知道是BFS,但是知道没用啊,我做不出来啊,我一开始没想过来,以为两个人烧很麻烦,其实就是向普通的那样做,不过来个6重for循环就行了,其实就是看看有几个块,如果块的个数超过两个就为-1,两个的话,分别开始烧,哪个块里有最长路,就是烧的那个时间,不过你要枚举有最长路的那个的块的每一个可烧的点,从而找出时间最短的,1个块的时候也是一样的,求出最长路,枚举每个点的时候求最短时间。这个题看着芳姐的代码才理解。。。。。
不过做的时候没有去判断几个块,因为你BFS找的时候就处理了。
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <queue>
- const int INF = ;
- int n,m ;
- char ch[][] ;
- int cnt ;
- int vis[][] ;
- int dir[][] = {{,},{,-},{,},{-,}} ;
- struct node
- {
- int x,y ;
- int step ;
- }q1,q2 ,mapp[];
- using namespace std ;
- int BFS(int x1,int y1,int x2,int y2)
- {
- int maxx = ;
- queue<node>que ;
- q1.x = x1,q1.y = y1,q1.step = ;
- q2.x = x2,q2.y = y2,q2.step = ;
- que.push(q1) ;
- que.push(q2) ;
- //memset(vis,0,sizeof(vis)) ;
- while(!que.empty())
- {
- struct node st1, st = que.front() ;
- que.pop() ;
- for(int i = ; i < ; i++)
- {
- int xx = st.x+dir[i][] ;
- int yy = st.y+dir[i][] ;
- if(!vis[xx][yy] && ch[xx][yy] == '#' && (xx >= &&xx < n&&yy>=&&yy<m))
- {
- vis[xx][yy] = ;
- st1.x = xx ,st1.y = yy ,st1.step = st.step+ ;
- que.push(st1) ;
- }
- }
- maxx = max(maxx,st.step) ;
- }
- return maxx ;
- }
- int main()
- {
- int T ;
- scanf("%d",&T) ;
- for(int i = ; i <= T ;i++)
- {
- scanf("%d %d",&n,&m) ;
- cnt = ;
- for(int j = ; j < n ; j++)
- {
- scanf("%s",ch[j]) ;
- for(int k = ; k < m ; k++)
- if(ch[j][k] == '#')
- {
- cnt++ ;
- mapp[cnt].x = j ;
- mapp[cnt].y = k ;
- }
- }
- printf("Case %d: ",i) ;
- if(cnt <= )
- {
- printf("0\n") ;
- continue ;
- }
- int minn = INF ;
- for(int j = ; j < cnt ; j++)
- {
- for(int k = j ; k < cnt ; k++)
- {
- memset(vis,,sizeof(vis)) ;
- vis[mapp[j].x][mapp[j].y] = ;
- vis[mapp[k].x][mapp[k].y] = ;
- bool flag = false ;
- int minnn = BFS(mapp[j].x,mapp[j].y,mapp[k].x,mapp[k].y) ;
- for(int h = ; h < n ; h++)
- {
- for(int l = ; l < m ; l++)
- {
- if(ch[h][l] != '#') continue ;
- if(!vis[h][l]){flag = true ; break ;}
- }
- if(flag) break ;
- }
- if(!flag) minn = min(minn,minnn) ;
- }
- }
- if(minn == INF) printf("-1\n") ;
- else printf("%d\n",minn) ;
- }
- return ;
- }
这个是ZP的,写的差不多,不过用时100多ms,所以贴出来瞻仰一下
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- #include<iostream>
- #include<math.h>
- #include<queue>
- using namespace std;
- #define LL __int64
- int map[][];
- int num[][][][];
- int vis[][];
- int n,m;
- struct list
- {
- int x;
- int y;
- int t;
- }p,q;
- queue<struct list >que;
- int xx[]={,-,,};
- int yy[]={,,,-};
- void dos(int i,int j,int a,int b,int t)
- {
- while(!que.empty())que.pop();
- num[i][j][a][b]=;
- p.x=a;
- p.y=b;
- p.t=;
- que.push(p);
- vis[a][b]=;
- while(!que.empty())
- {
- p=que.front();
- que.pop();
- num[i][j][p.x][p.y]=p.t;
- for(int i=;i<;i++)
- {
- q.x=p.x+xx[i];
- q.y=p.y+yy[i];
- q.t=p.t+;
- if(q.x<||q.x>n||q.y<||q.y>m)continue;
- if(vis[q.x][q.y])continue;
- if(map[q.x][q.y]==)continue;
- que.push(q);
- vis[q.x][q.y]=;
- }
- }
- }
- char str[];
- int main()
- {
- int T;
- int i,j,a,b,t,k;
- scanf("%d",&T);
- for(int _=;_<=T;_++)
- {
- memset(map,,sizeof(map));
- scanf("%d%d",&n,&m);
- for(i=;i<=n;i++)
- {
- for(j=;j<=m;j++)
- {
- for(k=;k<=n;k++)
- {
- for(t=;t<=m;t++)
- {
- num[i][j][k][t]=;
- }
- }
- }
- }
- for(i=;i<=n;i++)
- {
- scanf("%s",str);
- for(j=;j<m;j++)
- {
- if(str[j]=='#')
- {
- map[i][j+]=;
- }
- }
- }
- for(i=;i<=n;i++)
- {
- for(j=;j<=m;j++)
- {
- if(map[i][j]==)continue;
- memset(vis,,sizeof(vis));
- dos(i,j,i,j,);
- }
- }
- //cout<<num[2][1][2][1]<<endl;
- //cout<<num[2][3][2][3]<<endl;
- int minn=;
- for(i=;i<=n;i++)
- {
- for(j=;j<=m;j++)
- {
- if(map[i][j]==)continue;
- for(a=;a<=n;a++)
- {
- for(b=;b<=m;b++)
- {
- if(map[a][b]==)continue;
- int tt=;
- for(t=;t<=n;t++)
- {
- for(k=;k<=m;k++)
- {
- if(map[t][k]==)continue;
- tt=max(tt,min(num[i][j][t][k],num[a][b][t][k]));
- }
- }
- minn=min(minn,tt);
- }
- }
- }
- }
- printf("Case %d: ",_);
- if(minn==)
- {
- cout<<-<<endl;
- }
- else cout<<minn<<endl;
- }
- return ;
- }
我还要贴一下会神的,因为限时1000ms,然后会神的代码就正好跑了1000ms。。。。。
- #include <iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<stdlib.h>
- #include<vector>
- #include<cmath>
- #include<queue>
- #include<set>
- using namespace std;
- #define N 110
- #define LL long long
- #define INF 0xfffffff
- const double eps = 1e-;
- const double pi = acos(-1.0);
- const double inf = ~0u>>;
- char s[][];
- struct node
- {
- int x,y;
- int num;
- }p[N];
- int vis[][],dis[][] = {,,,-,-,,,},n,m;
- int judge(int x,int y)
- {
- if(x<||x>n||y<||y>m) return ;
- if(vis[x][y]) return ;
- if(s[x][y]!='#') return ;
- return ;
- }
- int bfs(int x1,int y1,int x2,int y2)
- {
- queue<node>q;
- node t1,t2;
- t1.num = ;t1.x = x1,t1.y = y1;
- t2.num = ;t2.x = x2,t2.y = y2;
- q.push(t1);
- q.push(t2);
- int mm=;
- while(!q.empty())
- {
- t1 = q.front();q.pop();
- for(int i = ; i < ;i++)
- {
- int tx = t1.x+dis[i][];
- int ty = t1.y+dis[i][];
- if(judge(tx,ty))
- {
- vis[tx][ty] = ;
- t2.x = tx,t2.y = ty,t2.num = t1.num+;
- q.push(t2);
- }
- }mm = max(mm,t1.num);
- }
- return mm;
- }
- int main()
- {
- int t,i,j,kk=,g;
- cin>>t;
- while(t--)
- {
- g = ;
- cin>>n>>m;
- for(i = ; i <= n; i++)
- for(j = ; j <= m ;j++)
- {
- cin>>s[i][j];
- if(s[i][j]=='#')
- {
- g++;
- p[g].x = i;
- p[g].y = j;
- }
- }
- int minz = INF;
- for(i = ; i <= g ; i++)
- for(j = i ;j <=g ;j++)
- {
- memset(vis,,sizeof(vis));
- vis[p[i].x][p[i].y] = ;
- vis[p[j].x][p[j].y] = ;
- int t1 = bfs(p[i].x,p[i].y,p[j].x,p[j].y);
- int flag = ;
- for(int o = ; o <= n;o++)
- {
- for(int e = ; e <= m; e++)
- if(s[o][e]=='#')
- {
- if(!vis[o][e])
- {
- flag = ;
- break;
- }
- }
- if(flag) break;
- }
- if(!flag)
- {
- minz = min(minz,t1);
- }
- }
- printf("Case %d: ",++kk);
- if(minz!=INF)
- cout<<minz<<endl;
- else
- puts("-1");
- }
- return ;
- }
FZU 2150 Fire Game(BFS)的更多相关文章
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU Problem 2150 Fire Game(bfs)
这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- 【FZU - 2150】Fire Game(bfs)
--> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...
- FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)
Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...
- FZU 2150 Fire Game (高姿势bfs--两个起点)
Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- Fire Game--FZU2150(bfs)
http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...
随机推荐
- angularjs Failed to read the 'selectionStart' property from 'HTMLInputElement':
在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ion ...
- mysql 导入导出sql文件
使用mysqldump导出sql文件 目前清楚的mysqldump语法是: mysqldump -h[hostname] -u[username] -p [databasename] > [sq ...
- oracle交集,并集,差集
引自:http://www.2cto.com/database/201308/238777.html [sql] create table test1 ( name ), NN ) ); insert ...
- Jquery 实现Xml文件内容处理
用JS对XMl文件处理实现和用JS处理一般的Dom元素一样; 加载一个Xml内容与新建一个Dom元素基本相同 如: 1.新建一个Dom元素的Jquey语法为:$("<p>hell ...
- html转图片
using System.IO; using System.Drawing; using System.Threading; using System.Windows.Forms; public cl ...
- mac os 10.10 pod install errors
/System/Library/Frameworks/Ruby.framework/Versions//gems/rake-/bin/rake RUBYARCHDIR=/Library/Ruby/Ge ...
- UIMenuController/UIPasteboard(1) 制作一个可以粘贴复制的Label
效果如下: 苹果只放出来了 UITextView,UITextField,webView三个控件的剪贴板,所以我们要自定义可以复制粘贴的控件,首先需要打开UIResponder的两个方法: - ( ...
- xps文档打印后winform界面文字丢失
最近做的xps文档打印功能,绝对的一波三折,一开始开发的时候,始终用的是xps writer 虚拟打印机,测试的时候也是,一直没有发现问题,但是真正到用户使用的时候,接上正式打印机,打印时候没有问题, ...
- windows 使用excel导出的问题
解决 window server2008 r2 没有注册Ofiice组件的方法 .NET下在用Microsoft.Office.Interop.Excel及word 操作Excel和Word时, ...
- Python 列表实现字典的get功能
字典有一个很好用的方法,就是get,既可以预防KeyError异常,也可以为不存在的key设置一个默认的value 例如: v=d.get('k','default') 而列表没有一个类似的方法,如果 ...