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

时间限制: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++) {
            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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值