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:

  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[505][505];
int visit[505][505],map[1010];
int to[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
    int x,y;
};

int go(int i,int j)
{
    if(i>=1&&i<=n&&j>=1&&j<=m&&data[i][j]!=‘#‘)
    return 1;
    return 0;
}

int bfs(int flag)
{
    int cnt=0;
    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=0;i<4;i++)
        {
            ed.x=st.x+to[i][0];
            ed.y=st.y+to[i][1];
            if(go(ed.x,ed.y)&&visit[ed.x][ed.y]==0)
            {
                visit[ed.x][ed.y]=flag;
                q.push(ed);
            }
        }
    }
    map[flag]=cnt;
}

int main()
{
    scanf("%d",&t);
    int res=0;
    while(t--)
    {
        res++;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++)
         for(int j=1;j<=m;j++)
          cin>>data[i][j];
        memset(visit,0,sizeof(visit));
        memset(map,0,sizeof(map));
        printf("Case %d:\n",res);
        for(int i=1;i<=q;i++)
        {
           scanf("%d%d",&xi,&yi);
           if(!visit[xi][yi])
           bfs(i);
           printf("%d\n",map[visit[xi][yi]]);
        }
    }
    return 0;
}
时间: 2024-10-17 04:37:05

LightOJ 1337 F - The Crystal Maze (bfs)的相关文章

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

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

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

Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

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

Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边.可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案.这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点.还

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu 5094 Maze (BFS+状压)

题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi.gi=0:墙   1,2,3.... :第1种门,第2种门,第3种门..... 有S把钥匙.给出S个信息:x1,y1,qi    含义是位置(x1,y1)上有一把第qi种的钥匙. 问多多最少花多少秒到达(n,m).若无法到达输出-1. 数据范围: (1<= n, m <=50, 0<= p

POJ Borg Maze (BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10428   Accepted: 3463 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to desc

poj 3026 Borg Maze(bfs+prim)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10810   Accepted: 3574 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to desc

POJ 3026:Borg Maze(BFS建图+prim+MST)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8250   Accepted: 2762 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr