FZU 2150(DFS+BFS)
2016-05-06 14:58
411 查看
Problem 2150 Fire Game
Accept: 1357 Submit: 4807
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.
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
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.
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Case 1: 1Case 2: -1Case 3: 0Case 4: 2
Submit Back Status Discuss
题意:你可以从2个地方点火,问能否把所有的草烧光,在一个联通块里的草火会传递,如果可以烧掉所有的草输出最少的时间
题解:这一题一定要小心的写,稍微写的不好就超时了。。。。。。。。。。。,开始思路是对的,但是一直TLE,先说思路,先使用DFS判断联通块的个数,枚举2个点进行BFS找最短时间,这里可以先把所有的草的点记录下来,双重循环枚举跑一下,(开始是使用四重循环,枚举2个点,现在想一下可能会重复的跑2个点,导致了超时)。注意这里如果有一棵草是搜不出来的,要特判一下
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
char s
;
int vis
;
int vx[] = { 0, 0, 1, -1 };
int vy[] = { 1, -1, 0, 0 };
int kuai,tot;
int n, m;
bool judge(int x, int y)
{
return (x >= 0 && x<n&&y >= 0 && y<m);
}
int bfs(int x, int y, int x2, int y2)
{
int sum = 0;
int tans = -1;
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
vis[x2][y2] = 1;
pair<int, pair<int, int> > top;
queue<pair<int, pair<int, int> > >q;
q.push(make_pair(0, make_pair(x, y)));
q.push(make_pair(0, make_pair(x2, y2)));
while (!q.empty())
{
sum++;
top = q.front(); q.pop();
tans = max(tans, top.first);
for (int i = 0; i<4; i++)
{
int tx = top.second.first + vx[i];
int ty = top.second.second + vy[i];
int dep = top.first;
if (!judge(tx, ty) || vis[tx][ty] || s[tx][ty] != '#')
continue;
vis[tx][ty] = 1;
q.push(make_pair(dep + 1, make_pair(tx, ty)));
}
}
if(sum==tot)
return tans;
return inf;
}
void dfs(int x, int y)
{
vis[x][y] = 0;
for (int i = 0; i<4; i++)
{
int xx = vx[i] + x;
int yy = vy[i] + y;
if (judge(xx, yy) && vis[xx][yy] && s[xx][yy] == '#')
{
dfs(xx, yy);
}
}
}
vector<pair<int, int> >pos;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
int _time_jc = clock();
#endif
int t, k = 1;
scanf("%d", &t);
while (t--)
{
pos.clear();
scanf("%d%d", &n, &m);
tot = 0;kuai = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '#')
{
vis[i][j] = 1;
tot++;
pos.push_back(make_pair(i, j));
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j<m; j++)
{
if (vis[i][j])
{
dfs(i, j);
kuai++;
}
}
}
if (tot == 1)
{
printf("Case %d: %d\n", k++, 0);
continue;
}
int realans = inf;
if (kuai <=2)
{
for (int i = 0; i<pos.size(); i++)
{
for (int j = i+1; j<pos.size(); j++)
{
realans=min(realans,(bfs(pos[i].first, pos[i].second, pos[j].first, pos[j].second)));
}
}
}
printf("Case %d: %d\n", k++, realans==inf?-1:realans);
}
return 0;
}
Accept: 1357 Submit: 4807
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem 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 gridswhich 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: 1Case 2: -1Case 3: 0Case 4: 2Submit Back Status Discuss
题意:你可以从2个地方点火,问能否把所有的草烧光,在一个联通块里的草火会传递,如果可以烧掉所有的草输出最少的时间
题解:这一题一定要小心的写,稍微写的不好就超时了。。。。。。。。。。。,开始思路是对的,但是一直TLE,先说思路,先使用DFS判断联通块的个数,枚举2个点进行BFS找最短时间,这里可以先把所有的草的点记录下来,双重循环枚举跑一下,(开始是使用四重循环,枚举2个点,现在想一下可能会重复的跑2个点,导致了超时)。注意这里如果有一棵草是搜不出来的,要特判一下
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
char s
;
int vis
;
int vx[] = { 0, 0, 1, -1 };
int vy[] = { 1, -1, 0, 0 };
int kuai,tot;
int n, m;
bool judge(int x, int y)
{
return (x >= 0 && x<n&&y >= 0 && y<m);
}
int bfs(int x, int y, int x2, int y2)
{
int sum = 0;
int tans = -1;
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
vis[x2][y2] = 1;
pair<int, pair<int, int> > top;
queue<pair<int, pair<int, int> > >q;
q.push(make_pair(0, make_pair(x, y)));
q.push(make_pair(0, make_pair(x2, y2)));
while (!q.empty())
{
sum++;
top = q.front(); q.pop();
tans = max(tans, top.first);
for (int i = 0; i<4; i++)
{
int tx = top.second.first + vx[i];
int ty = top.second.second + vy[i];
int dep = top.first;
if (!judge(tx, ty) || vis[tx][ty] || s[tx][ty] != '#')
continue;
vis[tx][ty] = 1;
q.push(make_pair(dep + 1, make_pair(tx, ty)));
}
}
if(sum==tot)
return tans;
return inf;
}
void dfs(int x, int y)
{
vis[x][y] = 0;
for (int i = 0; i<4; i++)
{
int xx = vx[i] + x;
int yy = vy[i] + y;
if (judge(xx, yy) && vis[xx][yy] && s[xx][yy] == '#')
{
dfs(xx, yy);
}
}
}
vector<pair<int, int> >pos;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
int _time_jc = clock();
#endif
int t, k = 1;
scanf("%d", &t);
while (t--)
{
pos.clear();
scanf("%d%d", &n, &m);
tot = 0;kuai = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '#')
{
vis[i][j] = 1;
tot++;
pos.push_back(make_pair(i, j));
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j<m; j++)
{
if (vis[i][j])
{
dfs(i, j);
kuai++;
}
}
}
if (tot == 1)
{
printf("Case %d: %d\n", k++, 0);
continue;
}
int realans = inf;
if (kuai <=2)
{
for (int i = 0; i<pos.size(); i++)
{
for (int j = i+1; j<pos.size(); j++)
{
realans=min(realans,(bfs(pos[i].first, pos[i].second, pos[j].first, pos[j].second)));
}
}
}
printf("Case %d: %d\n", k++, realans==inf?-1:realans);
}
return 0;
}
相关文章推荐
- Oracle字符集转换
- 获得某字符串(matString)中某个字符(string)第n次出现的位置
- win 安装ionic和cordova
- Codeforces Round #350 (Div. 2)A,B,C,D1
- jQuery 获取屏幕高度、宽度
- 加密算法全解析
- NYIT 36 最长公共子序列
- Servlet3异步特性+Ajax+Websocket
- FreeRTOS高级篇3---FreeRTOS调度器启动过程分析
- Android 在代码中控制View的填充方式和宽度、高度
- [置顶] ViewPager的onPageScrolled回调不完整
- 开发板烧录
- iOS 开发,调用打电话,发短信,打开网址
- poj1523(割点)
- vuejs资料收集
- Acrobat将pdf转换成图片之坑爹货
- SpringMVC登录案例
- 很久没来过了。。。说说现状。聊聊我的域名人生
- 大数据学习资源(上)
- 单链表归并排序