您的位置:首页 > 其它

湖南大学ACM程序设计新生杯大赛(同步赛) I-Piglet treasure hunt Series 1【BFS】

2017-12-24 18:45 267 查看
时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 32768K,其他语言65536K

64bit IO Format: %lld

题目描述

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<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long int
using namespace std;
const int maxn = 1e3 + 10;
const ll mod = 1e8 + 7;
struct node {
int x, y;
};
int m, n;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int gcd(int a, int b) {
if (b == 0) return a;
else return gcd(b, a%b);
}
int judge(int x, int y) {
if (x < 0 || y < 0 || x >= m || y >= n)
return 1;
return 0;
}
char a[maxn][maxn];
int vis[maxn][maxn];
int d[maxn][maxn];
void bfs() {
queue<node> q; node tmp;
for (int i = 0; i < m; i++) {
if (!d[i][0] && a[i][0] == '.') {
tmp.x = i; tmp.y = 0;
q.push(tmp); d[i][0] = 1;
vis[i][0] = 1;
}
if (!d[i][n - 1] && a[i][n - 1] == '.') {
tmp.x = i; tmp.y = n - 1;
q.push(tmp); d[i][n - 1] = 1;
vis[i][n - 1] = 1;
}
}
for (int j = 0; j < n; j++) {
if (!d[0][j] && a[0][j] == '.') {
tmp.x = 0; tmp.y = j;
q.push(tmp); d[0][j] = 1;
vis[0][j] = 1;
}
if (!d[m - 1][j] && a[m - 1][j] == '.') {
tmp.x = m-1; tmp.y = j;
q.push(tmp); d[m - 1][j] = 1;
vis[m - 1][j] = 1;
}
}
while (!q.empty()) {
node nw = q.front();
q.pop();
node t;
for (int i = 0; i < 4; i++) {
t.x = nw.x + dx[i];
t.y = nw.y + dy[i];
if (!judge(t.x, t.y)&&!d[t.x][t.y] && a[t.x][t.y] != '*' ){
d[t.x][t.y] = 1;
vis[t.x][t.y] = 1;
q.push(t);
}
}
}
}
int main()
{
while (~scanf("%d%d", &m, &n) && (m + n)) {
memset(vis, 0, sizeof vis);
memset(d, 0, sizeof d);
for (int i = 0; i < m; i++) {
scanf("%s", a[i]);
}
int p = 0, q = 0;
for (int i = 0; i < m; i+
4000
+) {
for (int j = 0; j < n; j++) {
if (a[i][j] == '.') q++;
}
}
bfs();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) p++;
}
}
int tmp = gcd(p, q);
printf("%d/%d\n", p / tmp, q / tmp);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐