湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1
题目描述
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.
输入
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\n");
continue;
}
printf("%d/%d\n", p / g, q / g);
}
return 0;
}
湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1的更多相关文章
- 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2
题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array
题目描述 Given an array A with length n a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han
题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)B - Build
题目描述 In country A, some roads are to be built to connect the cities.However, due to limited funds, ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation
题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number
题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons
题目描述 Yuanyuan Long is a dragon like this picture? I don’t know, ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks
题目描述 BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?
题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...
随机推荐
- spring整合hibernate时报错:org.hibernte.engine.transaction.spi.transactioncontext
错误提示:Caused by:java.lang.ClassNotFoundException: org.hibernte.engine.transaction.spi.transactioncont ...
- OpenCV---Numpy数组的使用以及创建图片
一:对头像的所有像素进行访问,并UI图像进行像素取反 (一)for循环取反 import cv2 as cv import numpy as np def access_pixels(image): ...
- Google Map API使用详解(一)——Google Map开发背景知识
一.谷歌地图主页 谷歌地图对应不同的地区都会有一些专门的主页,首次登陆时会显示这些地区.比如,香港的:http://maps.google.com.hk,台湾的:http://maps.google. ...
- IntelliJ Idea key shortcuts
>Default explaination Official IntelliJ Idea 常用快捷键列表 Shortcuts Ctrl+Shift + Enter,语句完成 "!&qu ...
- hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
题目链接:http://csustacm.com:4803/problem/1006 题目: 思路:正如题目一样,本题是一个hash,比赛的时候用的字典树,但是不知道为什么一直RE(听学长说要动态开点 ...
- 机器学习-kNN(1)
一 kNN算法简介 kNN(K-Nearest Neighbor)工作原理:存在一个样本数据集合,也称为训练样本集,并且样本集中每个数据都存在标签,即我们知道样本集中每一数据与所属分类对应的关系.输入 ...
- flask插件系列之flask_session会话机制
flask_session是flask框架实现session功能的一个插件,用来替代flask自带的session实现机制. 配置参数详解 SESSION_COOKIE_NAME 设置返回给客户端的c ...
- [转载]hazard pointer
hazard pointer 转载自: http://hi.baidu.com/rodimus/item/f6539cc179894f2f47d5c0ef 这是用于解决多线程并发下内存的回收,一块内存 ...
- js固定小数位数 .toFixed()
toFixed(num)法可把 Number 四舍五入为指定小数位数的数字. num为需要固定的位数 var num=2;console.log(num.toFixed(2));//2.00;var ...
- Window文本在Linux中出现的^M问题
问题:在Windows中写了一个shell脚本在Linux中死活不能运行,怎么也查不出错误,原来是格式问题. 原因:Windows/DOS系统的换行符是/r/n,Unix/Linux系统的换行符是/n ...