[kuangbin带你飞]专题一 简单搜索 J
来源:互联网 发布:标准流程图制作软件 编辑:程序博客网 时间:2024/06/05 16:01
J - Fire!
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
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
#include<bits/stdc++.h>using namespace std;const int maxn=1e3+5;const int INF=0x3f3f3f3f;char ans[maxn][maxn];bool book[maxn][maxn];int fire[maxn][maxn];int n,m,tx,ty;int cnt[4][2]={1,0,-1,0,0,1,0,-1};struct Node { int x,y,num; bool operator < (const Node &b) const { return num>b.num; }};struct Code { int x,y,num; bool operator < (const Code &b) const { return num>b.num; }}code;int check(int x,int y) { if(x==0||x==n-1) { if(ans[x][y]!='#'&&fire[x][y]==0) return 1; } if(y==0||y==m-1) { if(ans[x][y]!='#'&&fire[x][y]==0) return 1; } return 0;}int bfs() { memset(book,false,sizeof(book)); memset(fire,0,sizeof(fire)); Node now,next; Code cnow,cnext; priority_queue<Node>Q; priority_queue<Code>C; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(ans[i][j]=='F') { cnow.x=i,cnow.y=j,cnow.num=0; fire[i][j]=1; C.push(cnow); } } } now.x=tx,now.y=ty,now.num=0; book[tx][ty]=true; Q.push(now); while(!Q.empty()) { now=Q.top(); Q.pop(); if(fire[now.x][now.y]==1) continue; if(check(now.x,now.y)==1) return now.num+1; for(int i=0;i<4;i++) { int dx=now.x+cnt[i][0],dy=now.y+cnt[i][1]; if(dx>=0&&dx<n&&dy>=0&&dy<m&&ans[dx][dy]!='#'&&fire[dx][dy]==0&&book[dx][dy]==false) { next.x=dx,next.y=dy,next.num=now.num+1; book[dx][dy]=true; Q.push(next); } } while(!C.empty()) { cnow=C.top(); if(cnow.num>=next.num) break; C.pop(); for(int i=0;i<4;i++) { int dx=cnow.x+cnt[i][0],dy=cnow.y+cnt[i][1]; if(dx>=0&&dx<n&&dy>=0&&dy<m&&fire[dx][dy]==0&&ans[dx][dy]!='#') { cnext.x=dx,cnext.y=dy,cnext.num=cnow.num+1; fire[dx][dy]=1; C.push(cnext); } } } } return -1;}int main(){ ios::sync_with_stdio(0); cin.tie(0); int T; cin>>T; for(int k=0;k<T;k++) { cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>ans[i][j]; if(ans[i][j]=='J') tx=i,ty=j; } } if(check(tx,ty)==1) {cout<<1<<endl;continue;} int sum=bfs(); if(sum==-1) cout<<"IMPOSSIBLE"<<endl; else cout<<sum<<endl; } return 0;}
- [kuangbin带你飞]专题一 简单搜索-J - Fire!
- [kuangbin带你飞]专题一 简单搜索 J
- [kuangbin带你飞]专题一 简单搜索J - Fire!(UVA 11624)
- [kuangbin带你飞]专题一 简单搜索 J - Fire! UVA 11624
- [kuangbin带你飞]专题一 简单搜索 J UVA 11624
- [kuangbin神带你飞]专题一 简单搜索
- kuangbin带你飞 专题一 简单搜索 (题解)
- Fire Game [kuangbin带你飞]专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 总结
- Kuangbin带你飞专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 D - Fliptile
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- [kuangbin带你飞]专题一 简单搜索 A POJ1321
- [kuangbin带你飞]专题一 简单搜索 B POJ2251
- [kuangbin带你飞]专题一 简单搜索 C POJ3278
- [kuangbin带你飞]专题一 简单搜索 D poj3279
- [kuangbin带你飞]专题一 简单搜索 E POJ1426
- [kuangbin带你飞]专题一 简单搜索 F POJ3126
- 预备知识(1)——Objective-C基础知识
- [kuangbin带你飞]专题一 简单搜索 H
- [kuangbin带你飞]专题一 简单搜索 I
- 关于用js对URL编码
- OpenSSL中文手册之EVP库详解
- [kuangbin带你飞]专题一 简单搜索 J
- [kuangbin带你飞]专题一 简单搜索 N
- 数据挖掘-公开数据集的合集
- UVA 1232 || LA 4108 (线段树)
- nginx:非代理端口的访问
- 写爬虫过程中的一些总结
- 20170729
- React-Native进阶_4.底部标签栏TabBar
- 关于c语言中删除单向链表节点的问题