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. Reprint: Serialization

    Having just recently ran into some major serialization issues I’m going to list some of the errors a ...

  2. mysql循环插入数据库中数据。

    DELIMITER ;; CREATE PROCEDURE test_insert () BEGIN DECLARE i INT DEFAULT 1; WHILE i<100 DO insert ...

  3. OllyDBG V1.10聆风听雨汉化版

    软件名称:OllyDBG V1.10聆风听雨汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 3.84MB 图片预览: 软件简介: Ollydbg2. ...

  4. hibernate子查询

    对于支持子查询的数据库,Hibernate支持在查询中使用子查询.一个子查询必须被圆括号包围起来(经常是SQL聚集函数的圆括号). 甚至相互关联的子查询(引用到外部查询中的别名的子查询)也是允许的. ...

  5. Unity3DGUI:鼠标click

    Input函数监测鼠标操作 鼠标点击事件 鼠标双击事件

  6. C语言温度

    #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or a ...

  7. PAT乙级1002. 写出这个数 (20)

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  8. c语言对齐问题

    引言 考虑下面的结构体定义: typedef struct{ char c1; short s; char c2; int i; }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始 ...

  9. c#如何使两个方法并行运行

    static void Main(string[] args)        {            Parallel.Invoke(Foo, Bar);        } static void ...

  10. 安卓工程中定义的app_name等报错解决办法 工程上有叹号

    类似于"app_name" is not translated in af, am, ar, be, bg, ca, cs, da, de, el, en-rGB, es, es- ...