zoukankan      html  css  js  c++  java
  • LightOJ 1012.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

    .#.........

    .#.#######.

    .#.#.....#.

    .#.#.###.#.

    .#.#..@#.#.

    .#.#####.#.

    .#.......#.

    .#########.

    ...........

    11 6

    ..#..#..#..

    ..#..#..#..

    ..#..#..###

    ..#..#..#@.

    ..#..#..#..

    ..#..#..#..

    7 7

    ..#.#..

    ..#.#..

    ###.###

    ...@...

    ###.###

    ..#.#..

    ..#.#..

    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
    ",num,ans);
          }
        }
        return 0;
    }

                 

  • 相关阅读:
    Hystrix服务降级
    postman使用教程12-预处理(pre-request) 发送请求
    postman使用教程11- sign 签名预处理(pre-request)
    postman使用教程10-请求前参数预处理(pre-request)
    postman使用教程9-点 code 按钮生成代码段
    postman使用教程8-设置断言(Tests脚本编写)
    postman使用教程7-参数化引用外部文件(txt/csv/json)测试数据
    postman使用教程6-引用随机变量($guid,$timestamp,$randomInt)
    postman使用教程5-Test脚本中自定义变量(参数关联 提取 token 和引用 token )
    postman使用教程4-集合变量(collection variables)的使用
  • 原文地址:https://www.cnblogs.com/ZERO-/p/7156729.html
Copyright © 2011-2022 走看看