zoukankan      html  css  js  c++  java
  • 湖南大学ACM程序设计新生杯大赛(同步赛)I

    题目描述

    Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and it is inadvertently caught in the peach blossom trap.

    Fortunately, it has got a map of peach blossom trap. You can think of it as a matrix of R row and C column. ‘.’ stand for the road you can walk. ‘*’ means there is a peach tree planted in this grid, and obviously you can’t go into it.

    The pig can only walk up to four adjacent squares in the upper, lower, left and right directions at a time. The outside of the matrix is the paradise of freedom, and of course, it may never go out.

    Though it has got the map, but doesn't know where it is in the peach blossom trap now, that means it could be at any ‘.’ in the matrix. It finds you smart to tell it the probability it can get out of peach blossom trap, please tell him the answer in the form of p/q. 

     

    输入描述:

    Multiple groups of test case. (no more than 100 groups. )
    The first line of each group contains two numbers R and C,(0<=R, C<=1000), representing the number of rows and the number of columns of peach blossom trap, respectively. Stop the program when R and C are both 0.
    Then there are next R lines, each line contains C characters, either '.' or '*'.
    It is guarantee at least one grid is'. '.

    输出描述:

    For each test case, output the answer in the form of p/q on each line. Notice that p/q must be the fraction in lowest terms.
    示例1

    输入

    5 5
    *..*.
    **.**
    *.*.*
    *...*
    *****
    3 3
    ***
    *.*
    ***
    0 0

    输出

    4/9
    0/1

    说明

    In the first sample, the number of grids the pig may appear is 9 , of which there are 4 grids it can escape from the trap, so the answer is 4/9.

    题解

    连通块。

    从边缘开始找出所有连通块即可。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
     
    const int maxn = 1010;
    char s[maxn][maxn];
    int r, c;
    int b[maxn][maxn];
    int p, q;
     
    int dir[4][2] = {
      {-1, 0},
      {1, 0},
      {0, -1},
      {0, 1},
    };
     
    int gcd(int a, int b) {
      if(b == 0) return a;
      return gcd(b, a % b);
    }
     
    int out(int x, int y) {
      if(x < 0 || x >= r) return 1;
      if(y < 0 || y >= c) return 1;
      return 0;
    }
     
    void dfs(int x, int y) {
      b[x][y] = 1;
      p ++;
      for(int i = 0; i < 4; i ++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if(out(tx, ty)) continue;
        if(s[tx][ty] == '*') continue;
        if(b[tx][ty]) continue;
        dfs(tx, ty);
      }
    }
     
    int main() {
      while(~scanf("%d%d", &r, &c)) {
        if(r == 0 && c == 0) break;
        for(int i = 0; i < r; i ++) {
          scanf("%s", s[i]);
        }
        for(int i = 0; i < r; i ++) {
          for(int j = 0; j < c; j ++) {
            b[i][j] = 0;
          }
        }
     
        p = 0, q = 0;
        for(int i = 0; i < r; i ++) {
          for(int j = 0; j < c; j ++) {
            if(s[i][j] == '.') q ++;
            if(s[i][j] == '*') continue;
            if(b[i][j]) continue;
            if(i == 0 || i == r - 1 || j == 0 || j == c - 1) {
              dfs(i, j);
            }
          }
        }
     
        int g = gcd(p, q);
        if(q == 0) {
          printf("0/0
    ");
          continue;
        }
        printf("%d/%d
    ", p / g, q / g);
      }
      return 0;
    }
    

      

  • 相关阅读:
    webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)
    webservice系统学习笔记8-简单的权限校验
    JS获取浏览器高宽度,屏幕分辨率和一些定位空隙等
    基于jquery ui修改的不依赖第三方的背景透明的弹出div
    1个比较简单的使用java反射机制获取前台数据进行数据封装的例子
    J2EE项目集成SAP的BO报表
    php-------unset销毁变量并释放内存
    微信小程序------MD5加密(支持中文和不支持中文)和网络请求(get和post)
    谈谈http与https
    php--------合并2个数字键数组的值
  • 原文地址:https://www.cnblogs.com/zufezzt/p/8099039.html
Copyright © 2011-2022 走看看