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 ...
随机推荐
- TCP connect EADDRNOTAVAIL(99)错误原因分析
转自:http://blog.chinaunix.net/uid-20662820-id-3371081.html 关于TCP connect 返回错误99,可以能大家都会遇到,这里就分析一下这个错误 ...
- JavaScriptSerializer返回一条Json,页面获取值问题,数据绑定
一,后台处理数据方法 JavaScriptSerializer serializer = new JavaScriptSerializer(); string js = serializer.Seri ...
- vue实例生命周期
实例生命周期 var vm = new Vue({ data: { a: 1 }, created: function () { // `this` 指向 vm 实例 console.log('a i ...
- java 打开浏览器 url
public class openBrowers { public static void main(String[] args) { try { //String url = "http: ...
- Openjudge-NOI题库-Pell数列
题目描述 Description Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > ...
- 关于mongodb的一些笔记
1.以服务的形式安装mongodb dos -- 进入到mongodb的bin目录下,执行 D:\mongodb\bin>mongod --logpath D:\mongodb\logs\mon ...
- 定时帧:NSTimer和CADisplayLink
学习参考了:http://www.jianshu.com/p/c35a81c3b9ebhttps://zsisme.gitbooks.io/ios-/content/chapter11/frame-t ...
- open("","r")与open("","rb")的效率区别
fd = open("C:\Users\william\Desktop\dup_file - Copy (3).txt","r")for i in fd: pr ...
- NFV FD.io VPP VM 系统性能调优
Host Setting: 1.关闭power savings mode在BIOS中 2.设置 /sys/devices/system/cpu/cpu*/cpufreq/scaling_governo ...
- 实战荟萃-UI篇
一. 前言 平时在处理问题的时候,经常会遇到一些奇奇怪怪的问题,今天在这里将其记录下来.这里将会列举几个常用的UI问题进行讲解 二. 导航栏 iOS导航栏绝对是个巨坑.和很多朋友聊天都是自己实现了一套 ...