湖南大学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 ...
随机推荐
- 配置JNDI数据源
配置JNDI数据源: 在MATE-INF中新建一个context.xml <?xml version="1.0" encoding="UTF-8"?> ...
- java类的静态属性值获取
获取某个类实例的静态属性: public class ErrorCode { private String code; private String message; private ErrorCod ...
- 2017北京国庆刷题Day5 afternoon
期望得分:100+60+100=260 实际得分:0+60+40=100 设图中有m个环,每个环有si条边,有k条边不在环中 ans= (2^s1 -2)*( 2^s2 -2)* (2^s3 -2)… ...
- DELPHI中的快捷方式一览(完全版)
1.SHIFT+鼠标左键 先选中任一控件,按键后可选中窗体(选中控件后按Esc效果一样)2.Shift+F8 调试时弹出CPU窗口.3.Shift+F10 等于 ...
- 巧妙利用JQuery和Servlet来实现跨域请求
在网上看到很多的JQuery跨域请求的文章,比较有意思.这里我发表一个Servlet与JQuery配置实现跨域的代码,供大家参考.不足之处请指教 原理:JavaScript的Ajax不可以跨域,但是可 ...
- 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数
[题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...
- 20155117王震宇 2016-2017-2 《Java程序设计》第八周学习总结
教材学习内容总结 正则表达式 正则表达式是记录文本规则的代码 元字符 ^ :^会匹配行或者字符串的起始位置,有时还会匹配整个文档的起始位置. $ :$会匹配行或字符串的结尾. \b :不会消耗任何字符 ...
- 在Java中,你真的会日期转换吗
1.什么是SimpleDateFormat 在java doc对SimpleDateFormat的解释如下: SimpleDateFormat is a concrete class for form ...
- 简单响应式Bootstrap框架中文官网页面模板
链接:http://pan.baidu.com/s/1o7MQ6RC 密码:kee5
- tomcat和weblogic的区别
Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键技术,所以用户可以用Tomcat开发基于数据库,Servlet和JSP页面的Web应用, ...