LightOJ 1337 F - The Crystal Maze (bfs)
Description
You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As the name suggests, the maze is full of crystals. Your task is to collect as many crystals as possible.
To be more exact, the maze can be modeled as an M x N 2D grid where M denotes the number of rows and N denotes the number of columns. There are three types of cells in the grid:
- A '#' denotes a wall, you may not pass through it.
- A 'C' denotes a crystal. You may move through the cell.
- A '.' denotes an empty cell. You may move through the cell.
Now you are given the map of the maze, you want to find where to land such that you can collect maximum number of crystals. So, you are spotting some position x, y and you want to find the maximum number of crystals you may get if you land to cell (x, y). And you can only move vertically or horizontally, but you cannot pass through walls, or you cannot get outside the maze.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing three integers M, N and Q (2 ≤ M, N ≤ 500, 1 ≤ Q ≤ 1000). Each of the next M lines contains N characters denoting the maze. You can assume that the maze follows the above restrictions.
Each of the next Q lines contains two integers xi and yi (1 ≤ xi ≤ M, 1 ≤ yi ≤ N) denoting the cell where you want to land. You can assume that cell (xi, yi) is empty i.e. the cell contains '.'.
Output
For each case, print the case number in a single line. Then print Q lines, where each line should contain the maximum number of crystals you may collect if you land on cell (xi, yi).
Sample Input
1
4 5 2
..#..
.C#C.
##..#
..C#C
1 1
4 1
Sample Output
Case 1:
1
2
//解题思路:看这道题的输入会发现容易超时,第一反应就是需要记忆化搜索,我选择用 bfs 来寻找这一片连通区域,以及水晶的数量 cnt;
//是连通区域的点用相同的 flag 进行标记,每一个不同的 flag 都对应唯一一个 cnt ;
//在每次输入时都要看是否 visit
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring> using namespace std;
int n,m,q,xi,yi,t;
char data[][];
int visit[][],map[];
int to[][]={{,},{,-},{,},{-,}}; struct node
{
int x,y;
}; int go(int i,int j)
{
if(i>=&&i<=n&&j>=&&j<=m&&data[i][j]!='#')
return ;
return ;
} int bfs(int flag)
{
int cnt=;
node st,ed;
queue <node> q;
st.x=xi;
st.y=yi;
visit[xi][yi]=flag;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
visit[st.x][st.y]=flag;
if(data[st.x][st.y]=='C')
cnt++;
for(int i=;i<;i++)
{
ed.x=st.x+to[i][];
ed.y=st.y+to[i][];
if(go(ed.x,ed.y)&&visit[ed.x][ed.y]==)
{
visit[ed.x][ed.y]=flag;
q.push(ed);
}
}
}
map[flag]=cnt;
} int main()
{
scanf("%d",&t);
int res=;
while(t--)
{
res++;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>data[i][j];
memset(visit,,sizeof(visit));
memset(map,,sizeof(map));
printf("Case %d:\n",res);
for(int i=;i<=q;i++)
{
scanf("%d%d",&xi,&yi);
if(!visit[xi][yi])
bfs(i);
printf("%d\n",map[visit[xi][yi]]);
}
}
return ;
}
LightOJ 1337 F - The Crystal Maze (bfs)的更多相关文章
- Day11 - F - A Dangerous Maze LightOJ - 1027
求期望注意期望的定义,这题我们可以分正负数情况,设所求期望为E 正数: 1/n*x_i 负数:1/n*(E+x_j) 此时概率为1/n,根据期望定义,他回到起点后出去的期望为E,花费回起点的时间为x_ ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- Borg Maze(bfs+prim)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6971 Accepted: 2345 Description The B ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
随机推荐
- 【IE6的疯狂之九】li在IE中底部空行的BUG
曾经写过[IE6的疯狂之六]li在IE中底部3像素的BUG(增加浮动解决问题),原文地址:http://www.css88.com/archives/421: IE6 BUG大全: http://ww ...
- AndroidPullToRefresh拉动效果配置
最近用了 开源的 AndroidPullToRefresh 库,但是发现拉动时的效果有个很奇怪的地方,无论上下拉动,当列表滚动到顶部或底部时,会瞬间弹出半个列表高度的拉动提示,感觉很不舒服,这种提示根 ...
- R安装
linux: 在编译R之前,需要通过yum安装以下几个程序: #yum install gcc-gfortran #否则报”configure: error: No F77 ...
- matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)”
matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)” 解决办法:traindata ...
- 解决Windows内存问题的两个小工具RamMap和VMMap(这个更牛更好)
来源:http://www.cr173.com/html/13006_1.html .net程序内存监测分配工具(CLR Profiler for .NET Framework 4)官方安装版 类型: ...
- 【Python之路】第九篇--Python基础之线程、进程和协程
进程与线程之间的关系 线程是属于进程的,线程运行在进程空间内,同一进程所产生的线程共享同一内存空间,当进程退出时该进程所产生的线程都会被强制退出并清除.线程可与属于同一进程的其它线程共享进程所拥有的全 ...
- win10 Incredibuild 兼容
电脑换了win10,所有的软件都需要重新安装. win10的体验还是很不错的.但是也有一堆的问题.原来的vs2010今天无法再使用分布式编译IncrediBuild. 现象是,虽然可以连上分布式编译的 ...
- incomplete type is not allowed
keil环境下,报错#70: incomplete type is not allowed,解决 mqtt_conf.h 定义了一个结构体 mqtt_buffer.h #include <std ...
- Python基础篇-day8
本节目录1.抽象接口2.静态方法.类方法.属性方法3.类的特殊方法 3.1 __doc__ 表示类的描述信息(注释) 3.2 __module__ 和 __class__ 3.3 __init__ 构 ...
- dao代码模板
提供数据源以及回收资源的工具类DbUtils: public class DbUtils { private static ComboPooledDataSource dataSource = new ...