fzu2150 搜索
来源:互联网 发布:mysql limit 编辑:程序博客网 时间:2024/06/06 04:44
Description
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
Sample Output
#include <iostream>#include <string.h>#include <stdio.h>#include <queue>#include <vector>using namespace std;int n,m;char str[20][20],str2[20][20];int d[20][20],vis[20][20];int dx[4]= {0,0,1,-1};int dy[4]= {-1,1,0,0};int flag,tot;struct point{ int x,y,num;};vector<point>G[105];void dfs(int x,int y){ vis[x][y]=1; point p; p.x=x; p.y=y; p.num=0; G[flag].push_back(p); int nx,ny; for(int i=0; i<4; i++) { nx=x+dx[i]; ny=y+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#') { vis[nx][ny]=1; dfs(nx,ny); } }}int min(int a,int b){ if(a<b) return a; return b;}int max(int a,int b){ if(a>b) return a; return b;}int bfs(point xx){ int maxd=-2; point now; memset(vis,0,sizeof(vis)); vis[xx.x][xx.y]=1; //vis[yy.x][yy.y]=1; queue<point>que; que.push(xx); //que.push(yy); while(!que.empty()) { point tmp=que.front(); que.pop(); int nx,ny; for(int i=0; i<4; i++) { nx=tmp.x+dx[i]; ny=tmp.y+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#') { now.x=nx; vis[nx][ny]=1; now.y=ny; now.num=tmp.num+1; que.push(now); maxd=max(now.num,maxd); } } } return maxd;}int bfs2(point xx,point yy){ int maxd=-2; point now; memset(vis,0,sizeof(vis)); vis[xx.x][xx.y]=1; vis[yy.x][yy.y]=1; queue<point>que; que.push(xx); que.push(yy); while(!que.empty()) { point tmp=que.front(); que.pop(); int nx,ny; for(int i=0; i<4; i++) { nx=tmp.x+dx[i]; ny=tmp.y+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#') { now.x=nx; vis[nx][ny]=1; now.y=ny; now.num=tmp.num+1; que.push(now); maxd=max(now.num,maxd); } } } return maxd;}int main(){ int t; int T=0; int num; cin>>t; while(cin>>n>>m) { G[1].clear(); G[2].clear(); num=0; memset(str,0,sizeof(0)); for(int i=0; i<n; i++) { cin>>str[i]; for(int j=0; j<m; j++) if(str[i][j]=='#') num++; } cout<<"Case "<<++T<<": "; if(num<=2) { cout<<0<<endl; continue; } flag=0; tot=0; memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(str[i][j]=='#'&&vis[i][j]==0) { flag++; dfs(i,j); } } } int ans1=999,ans2=999; if(flag>=3) { cout<<-1<<endl; continue; } int temp=0,ttp=0; if(flag==1) { for(int i=0;i<G[1].size();i++) { for(int j=i+1;j<G[1].size();j++) { ttp=bfs2(G[1][i],G[1][j]); ans1=min(ttp,ans1); } } } else { for(int i=1; i<=flag; i++) { for(int j=0;j<G[i].size();j++) { temp=bfs(G[i][j]); //cout<<"temp=="<<temp<<endl; if(i==1) ans1=min(ans1,temp); if(i==2) ans2=min(ans2,temp); } } } // cout<<ans1<<" "<<ans2<<endl; // cout<<flag<<"=flag"<<endl; if(flag==1) { cout<<ans1<<endl; } else { cout<<max(ans1,ans2)<<endl; } } return 0;}
- fzu2150 搜索
- FZU2150双源BFS,暴力搜索
- FZU2150
- FZU2150 Fire Game(搜索题:bfs)
- BFS广度优先搜索(10)--fzu2150(基础题)
- FZU2150->BFS
- FZU2150 fire
- FZU2150 Fire Game (bfs)
- FZU2150,暴力广搜
- FZU2150 Fire Game
- FZU2150 Fire Game
- fzu2150(广搜)
- FZU2150 Fire Game
- fzu2150 Fire Game bfs
- FZU2150 fire 双搜bfs
- FZU2150 Fire Game (BFS)
- FZU2150-Fire Game
- FZU2150 Fire Game
- Realm简单使用
- genymotion的 INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
- oracle-函数
- JAVA_深入理解HashMap
- Android编译示例代码
- fzu2150 搜索
- IOS 中使用token机制来验证用户的安全性
- Android系统框架
- DatePicker只显示月日,不显示年
- LLDB 调试 UI 的技巧
- Android开发工具锦集
- failed to load java type corresponding to e=web-app@http://java.sun.com/xml/ns/javaee
- 完整java开发中JDBC连接数据库代码和步骤
- wget获取https地址报错 --no-check-certificate