点我看题目

题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-1

思路 :这个题因为可以同时向上下左右同时烧过去,所以一看我就知道是BFS,但是知道没用啊,我做不出来啊,我一开始没想过来,以为两个人烧很麻烦,其实就是向普通的那样做,不过来个6重for循环就行了,其实就是看看有几个块,如果块的个数超过两个就为-1,两个的话,分别开始烧,哪个块里有最长路,就是烧的那个时间,不过你要枚举有最长路的那个的块的每一个可烧的点,从而找出时间最短的,1个块的时候也是一样的,求出最长路,枚举每个点的时候求最短时间。这个题看着芳姐的代码才理解。。。。。

不过做的时候没有去判断几个块,因为你BFS找的时候就处理了。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <queue>
  5.  
  6. const int INF = ;
  7. int n,m ;
  8. char ch[][] ;
  9. int cnt ;
  10. int vis[][] ;
  11. int dir[][] = {{,},{,-},{,},{-,}} ;
  12. struct node
  13. {
  14. int x,y ;
  15. int step ;
  16. }q1,q2 ,mapp[];
  17.  
  18. using namespace std ;
  19.  
  20. int BFS(int x1,int y1,int x2,int y2)
  21. {
  22. int maxx = ;
  23. queue<node>que ;
  24. q1.x = x1,q1.y = y1,q1.step = ;
  25. q2.x = x2,q2.y = y2,q2.step = ;
  26. que.push(q1) ;
  27. que.push(q2) ;
  28. //memset(vis,0,sizeof(vis)) ;
  29. while(!que.empty())
  30. {
  31. struct node st1, st = que.front() ;
  32. que.pop() ;
  33. for(int i = ; i < ; i++)
  34. {
  35. int xx = st.x+dir[i][] ;
  36. int yy = st.y+dir[i][] ;
  37. if(!vis[xx][yy] && ch[xx][yy] == '#' && (xx >= &&xx < n&&yy>=&&yy<m))
  38. {
  39. vis[xx][yy] = ;
  40. st1.x = xx ,st1.y = yy ,st1.step = st.step+ ;
  41. que.push(st1) ;
  42. }
  43. }
  44. maxx = max(maxx,st.step) ;
  45. }
  46. return maxx ;
  47. }
  48.  
  49. int main()
  50. {
  51. int T ;
  52. scanf("%d",&T) ;
  53. for(int i = ; i <= T ;i++)
  54. {
  55. scanf("%d %d",&n,&m) ;
  56. cnt = ;
  57. for(int j = ; j < n ; j++)
  58. {
  59. scanf("%s",ch[j]) ;
  60. for(int k = ; k < m ; k++)
  61. if(ch[j][k] == '#')
  62. {
  63. cnt++ ;
  64. mapp[cnt].x = j ;
  65. mapp[cnt].y = k ;
  66. }
  67. }
  68. printf("Case %d: ",i) ;
  69. if(cnt <= )
  70. {
  71. printf("0\n") ;
  72. continue ;
  73. }
  74. int minn = INF ;
  75. for(int j = ; j < cnt ; j++)
  76. {
  77. for(int k = j ; k < cnt ; k++)
  78. {
  79. memset(vis,,sizeof(vis)) ;
  80. vis[mapp[j].x][mapp[j].y] = ;
  81. vis[mapp[k].x][mapp[k].y] = ;
  82. bool flag = false ;
  83. int minnn = BFS(mapp[j].x,mapp[j].y,mapp[k].x,mapp[k].y) ;
  84. for(int h = ; h < n ; h++)
  85. {
  86. for(int l = ; l < m ; l++)
  87. {
  88. if(ch[h][l] != '#') continue ;
  89. if(!vis[h][l]){flag = true ; break ;}
  90. }
  91. if(flag) break ;
  92. }
  93. if(!flag) minn = min(minn,minnn) ;
  94. }
  95. }
  96.  
  97. if(minn == INF) printf("-1\n") ;
  98. else printf("%d\n",minn) ;
  99. }
  100. return ;
  101. }

这个是ZP的,写的差不多,不过用时100多ms,所以贴出来瞻仰一下

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<math.h>
  6. #include<queue>
  7. using namespace std;
  8. #define LL __int64
  9. int map[][];
  10. int num[][][][];
  11. int vis[][];
  12. int n,m;
  13. struct list
  14. {
  15. int x;
  16. int y;
  17. int t;
  18. }p,q;
  19. queue<struct list >que;
  20. int xx[]={,-,,};
  21. int yy[]={,,,-};
  22. void dos(int i,int j,int a,int b,int t)
  23. {
  24. while(!que.empty())que.pop();
  25. num[i][j][a][b]=;
  26. p.x=a;
  27. p.y=b;
  28. p.t=;
  29. que.push(p);
  30. vis[a][b]=;
  31. while(!que.empty())
  32. {
  33. p=que.front();
  34. que.pop();
  35. num[i][j][p.x][p.y]=p.t;
  36. for(int i=;i<;i++)
  37. {
  38. q.x=p.x+xx[i];
  39. q.y=p.y+yy[i];
  40. q.t=p.t+;
  41. if(q.x<||q.x>n||q.y<||q.y>m)continue;
  42. if(vis[q.x][q.y])continue;
  43. if(map[q.x][q.y]==)continue;
  44. que.push(q);
  45. vis[q.x][q.y]=;
  46. }
  47. }
  48. }
  49. char str[];
  50. int main()
  51. {
  52. int T;
  53. int i,j,a,b,t,k;
  54. scanf("%d",&T);
  55. for(int _=;_<=T;_++)
  56. {
  57. memset(map,,sizeof(map));
  58. scanf("%d%d",&n,&m);
  59. for(i=;i<=n;i++)
  60. {
  61. for(j=;j<=m;j++)
  62. {
  63. for(k=;k<=n;k++)
  64. {
  65. for(t=;t<=m;t++)
  66. {
  67. num[i][j][k][t]=;
  68. }
  69. }
  70. }
  71. }
  72. for(i=;i<=n;i++)
  73. {
  74. scanf("%s",str);
  75. for(j=;j<m;j++)
  76. {
  77. if(str[j]=='#')
  78. {
  79. map[i][j+]=;
  80. }
  81. }
  82. }
  83. for(i=;i<=n;i++)
  84. {
  85. for(j=;j<=m;j++)
  86. {
  87. if(map[i][j]==)continue;
  88. memset(vis,,sizeof(vis));
  89. dos(i,j,i,j,);
  90. }
  91. }
  92. //cout<<num[2][1][2][1]<<endl;
  93. //cout<<num[2][3][2][3]<<endl;
  94. int minn=;
  95. for(i=;i<=n;i++)
  96. {
  97. for(j=;j<=m;j++)
  98. {
  99. if(map[i][j]==)continue;
  100. for(a=;a<=n;a++)
  101. {
  102. for(b=;b<=m;b++)
  103. {
  104. if(map[a][b]==)continue;
  105. int tt=;
  106. for(t=;t<=n;t++)
  107. {
  108. for(k=;k<=m;k++)
  109. {
  110. if(map[t][k]==)continue;
  111. tt=max(tt,min(num[i][j][t][k],num[a][b][t][k]));
  112. }
  113. }
  114. minn=min(minn,tt);
  115. }
  116. }
  117. }
  118. }
  119. printf("Case %d: ",_);
  120. if(minn==)
  121. {
  122. cout<<-<<endl;
  123. }
  124. else cout<<minn<<endl;
  125. }
  126. return ;
  127. }

我还要贴一下会神的,因为限时1000ms,然后会神的代码就正好跑了1000ms。。。。。

  1. #include <iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stdlib.h>
  6. #include<vector>
  7. #include<cmath>
  8. #include<queue>
  9. #include<set>
  10. using namespace std;
  11. #define N 110
  12. #define LL long long
  13. #define INF 0xfffffff
  14. const double eps = 1e-;
  15. const double pi = acos(-1.0);
  16. const double inf = ~0u>>;
  17. char s[][];
  18. struct node
  19. {
  20. int x,y;
  21. int num;
  22. }p[N];
  23. int vis[][],dis[][] = {,,,-,-,,,},n,m;
  24. int judge(int x,int y)
  25. {
  26. if(x<||x>n||y<||y>m) return ;
  27. if(vis[x][y]) return ;
  28. if(s[x][y]!='#') return ;
  29. return ;
  30. }
  31. int bfs(int x1,int y1,int x2,int y2)
  32. {
  33. queue<node>q;
  34. node t1,t2;
  35. t1.num = ;t1.x = x1,t1.y = y1;
  36. t2.num = ;t2.x = x2,t2.y = y2;
  37. q.push(t1);
  38. q.push(t2);
  39. int mm=;
  40. while(!q.empty())
  41. {
  42. t1 = q.front();q.pop();
  43.  
  44. for(int i = ; i < ;i++)
  45. {
  46. int tx = t1.x+dis[i][];
  47. int ty = t1.y+dis[i][];
  48. if(judge(tx,ty))
  49. {
  50. vis[tx][ty] = ;
  51. t2.x = tx,t2.y = ty,t2.num = t1.num+;
  52. q.push(t2);
  53. }
  54. }mm = max(mm,t1.num);
  55. }
  56. return mm;
  57. }
  58. int main()
  59. {
  60. int t,i,j,kk=,g;
  61. cin>>t;
  62. while(t--)
  63. {
  64. g = ;
  65. cin>>n>>m;
  66. for(i = ; i <= n; i++)
  67. for(j = ; j <= m ;j++)
  68. {
  69. cin>>s[i][j];
  70.  
  71. if(s[i][j]=='#')
  72. {
  73. g++;
  74. p[g].x = i;
  75. p[g].y = j;
  76. }
  77. }
  78. int minz = INF;
  79. for(i = ; i <= g ; i++)
  80. for(j = i ;j <=g ;j++)
  81. {
  82. memset(vis,,sizeof(vis));
  83. vis[p[i].x][p[i].y] = ;
  84. vis[p[j].x][p[j].y] = ;
  85. int t1 = bfs(p[i].x,p[i].y,p[j].x,p[j].y);
  86. int flag = ;
  87. for(int o = ; o <= n;o++)
  88. {
  89. for(int e = ; e <= m; e++)
  90. if(s[o][e]=='#')
  91. {
  92. if(!vis[o][e])
  93. {
  94. flag = ;
  95. break;
  96. }
  97. }
  98. if(flag) break;
  99. }
  100. if(!flag)
  101. {
  102. minz = min(minz,t1);
  103. }
  104. }
  105. printf("Case %d: ",++kk);
  106. if(minz!=INF)
  107. cout<<minz<<endl;
  108. else
  109. puts("-1");
  110. }
  111. return ;
  112. }

FZU 2150 Fire Game(BFS)的更多相关文章

  1. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  2. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

  3. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  4. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  5. 【FZU - 2150】Fire Game(bfs)

    --> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...

  6. 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 ...

  7. 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 ...

  8. foj 2150 Fire Game(bfs暴力)

         Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...

  9. Fire Game--FZU2150(bfs)

    http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...

随机推荐

  1. angularjs Failed to read the 'selectionStart' property from 'HTMLInputElement':

    在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ion ...

  2. mysql 导入导出sql文件

    使用mysqldump导出sql文件 目前清楚的mysqldump语法是: mysqldump -h[hostname] -u[username] -p [databasename] > [sq ...

  3. oracle交集,并集,差集

    引自:http://www.2cto.com/database/201308/238777.html [sql] create table test1 ( name ), NN ) ); insert ...

  4. Jquery 实现Xml文件内容处理

    用JS对XMl文件处理实现和用JS处理一般的Dom元素一样; 加载一个Xml内容与新建一个Dom元素基本相同 如: 1.新建一个Dom元素的Jquey语法为:$("<p>hell ...

  5. html转图片

    using System.IO; using System.Drawing; using System.Threading; using System.Windows.Forms; public cl ...

  6. mac os 10.10 pod install errors

    /System/Library/Frameworks/Ruby.framework/Versions//gems/rake-/bin/rake RUBYARCHDIR=/Library/Ruby/Ge ...

  7. UIMenuController/UIPasteboard(1) 制作一个可以粘贴复制的Label

    效果如下:   苹果只放出来了 UITextView,UITextField,webView三个控件的剪贴板,所以我们要自定义可以复制粘贴的控件,首先需要打开UIResponder的两个方法: - ( ...

  8. xps文档打印后winform界面文字丢失

    最近做的xps文档打印功能,绝对的一波三折,一开始开发的时候,始终用的是xps writer 虚拟打印机,测试的时候也是,一直没有发现问题,但是真正到用户使用的时候,接上正式打印机,打印时候没有问题, ...

  9. windows 使用excel导出的问题

    解决 window server2008  r2 没有注册Ofiice组件的方法   .NET下在用Microsoft.Office.Interop.Excel及word 操作Excel和Word时, ...

  10. Python 列表实现字典的get功能

    字典有一个很好用的方法,就是get,既可以预防KeyError异常,也可以为不存在的key设置一个默认的value 例如: v=d.get('k','default') 而列表没有一个类似的方法,如果 ...