LightOJ1012-Guilty Prince-DFS

Guilty Prince 

Time Limit: 2 second(s)
Memory Limit: 32 MB

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father‘s will. In the way he found that the place was a combination of land and water. Since he didn‘t know how to swim, he was only able to move on the land. He didn‘t know how many places might be his destination. So, he asked your help.

For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.

Now write a program to find the number of cells (unit land) he could reach including the cell he was living.

Input

Input starts with an integer T (≤ 500), denoting the number of test cases.

Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.

There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.

1) ‘.‘ - land

2) ‘#‘ - water

3) ‘@‘ - initial position of prince (appears exactly once in a dataset)

Output

For each case, print the case number and the number of cells he can reach from the initial position (including it).

Sample Input

4

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#[email protected]#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

[email protected]

###.###

..#.#..

..#.#..

Sample Output

Case 1: 45

Case 2: 59

Case 3: 6

Case 4: 13

题意好理解,直接改的HDU1312的板子题。。。

代码:

#include<bits/stdc++.h>
using namespace std;
int direct [4][2]={-1,0,1,0,0,1,0,-1};
char str[25][25];
bool flag[25][25];
int w,h,ans;
void DFS(int x,int y){
    for(int i=0;i<4;i++){
        int p=x+direct[i][0];
        int q=y+direct[i][1];
        if(p>=0&&q>=0&&p<h&&q<w&&flag[p][q]==0&&str[p][q]==‘.‘){
            ans++;
            flag[p][q]=1;
            DFS(p,q);
        }
    }
}
int main(){
    int i,j,k,t,num=0;
    int Dx,Dy;
    while(~scanf("%d",&t)){
      while(t--){
        num++;
        scanf("%d%d",&w,&h);
        if(w==0&&h==0)break;
        memset(flag,0,sizeof(flag));
        getchar();
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                scanf("%c",&str[i][j]);
            if(str[i][j]==‘@‘){
                Dx=i;
                Dy=j;
            }
            } getchar();
        }
        ans=1;
        flag[Dx][Dy]=1;
        DFS(Dx,Dy);
        printf("Case %d: %d\n",num,ans);
      }
    }
    return 0;
}

 

时间: 2024-10-19 11:05:53

LightOJ1012-Guilty Prince-DFS的相关文章

Lightoj 1012 - Guilty Prince

bfs遍历一遍就行了. /* *********************************************** Author :guanjun Created Time :2016/6/15 18:50:31 File Name :1012.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib

LightOJ 1012 简单bfs,水

1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) using names

HDU 4685 Prince and Princess

Prince and Princess Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 468564-bit integer IO format: %I64d      Java class name: Main There are n princes and m princesses. Princess can marry any prince. But prin

HDU 4685 Prince and Princess(二分图 + 强连通)

Problem Description There are n princes and m princesses. Princess can marry any prince. But prince can only marry the princess they DO love. For all princes,give all the princesses that they love. So, there is a maximum number of pairs of prince and

LightOJ1012---Guilty Prince (并查集)

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed h

[CF986E]Prince&#39;s Problem

题意:给一棵带点权$w_i$的树,多次询问$(u,v,x)$,求出$\prod\limits_{i\in\text{path}(u,v)}(w_i,x)$ 因为是乘法,所以可以把路径询问拆成到根询问,这样就可以离线做了 因为求$\gcd$的本质是质因数指数取$\min$,所以在离线dfs时每到一个点就把它的点权质因数分解打上标记然后统计答案即可 具体地,对于$w_x=\prod\limits_{i=1}^kp_i^{a_i}$,我们把每个$p_i$的$1\cdots a_i$次幂乘上$p_i$的

[Codeforces 986E] Prince&#39;s Problem

[题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径积 , 再除以LCA父节点到根的路径积 考虑如何计算根到X路径上每个点与Value的GCD之积 不妨对于每个质数P开一个数组cnt[] , 表示根到当前节点P^i有多少个 , 我们可以在DFS的过程中维护这个数组 将询问离线即可 时间复杂度 : O(V + NlogN + QlogV^2) [代码]

Prince and Princess HDU - 4685(匹配 + 强连通)

Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2336    Accepted Submission(s): 695 Problem Description There are n princes and m princesses. Princess can marry any prince. B

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一