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:

  1. '#' denotes a wall, you may not pass through it.
  2. 'C' denotes a crystal. You may move through the cell.
  3. '.' 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 Mand 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)的更多相关文章

  1. Day11 - F - A Dangerous Maze LightOJ - 1027

    求期望注意期望的定义,这题我们可以分正负数情况,设所求期望为E 正数: 1/n*x_i 负数:1/n*(E+x_j) 此时概率为1/n,根据期望定义,他回到起点后出去的期望为E,花费回起点的时间为x_ ...

  2. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  3. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  4. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  5. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  6. Borg Maze(bfs+prim)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6971   Accepted: 2345 Description The B ...

  7. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  8. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  9. 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 ...

随机推荐

  1. react-router的基础知识

    一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...

  2. Hex Workshop(16进制编辑利器) 6.7.2绿色版

    软件名称: Hex Workshop 6.7.2绿色版(16进制编辑利器) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 1.8MB 图片预览: 软件简介 ...

  3. linux重要命令

    echo echo命令用于在终端显示字符串或输出变量提取后的值,格式为:"echo [字符串 | $变量]". 将指定字符串输出到终端屏幕: [root@linuxprobe ~] ...

  4. 敏捷开发(九)- Scrum Sprint计划会议2

    本文主要是为了检测你对SCRUM Sprint 计划会议二的了解和使用程度, 通过本文你可以检测一下     1.你们的SCRUM Sprint 计划会议二的过程和步骤    2.SCRUM Spri ...

  5. 虚拟机在 OpenStack 里没有共享存储条件下的在线迁移[转]

    原文链接:http://www.ibm.com/developerworks/cn/cloud/library/1508_wangyx_openstacklivemigrate/ 迁移(Migrati ...

  6. [SOJ] DAG?

    Description 输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n ...

  7. ACM-ICPC之路

    自从了解到了ACM,我就坚定了参加这个比赛的信心.虽然零基础开始,但是阻挡不了我的前进之路.从大一上学期的完成二十道题,到假期完成四十道题:从第一次校赛不了解退出循环方式只完成了一道题,到大一预选赛第 ...

  8. 负载均衡lvs_dr_tcp_http单调度

    准备三台虚拟,均为CentOS6.5 x86_64注意,配置过程中,保持端口的一致性.director (eth0 192.168.1.189, vip eth0:0: 192.168.1.18) D ...

  9. .netER的未来路,关于基础是否重要和应该自己手写代码吗?

    http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...

  10. Fighting For 2017 Season Contest 1

    比赛地址[https://vjudge.net/contest/147011#problem/A].960626 题目一:[http://codeforces.com/problemset/probl ...