1256:献给阿尔吉侬的花束

题目网址:信息学奥赛一本通(C++版)在线评测系统

题目介绍:

1256:献给阿尔吉侬的花束


时间限制: 1000 ms         内存限制: 65536 KB
提交数:17588    通过数: 7603

【题目描述】

阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。

迷宫用一个R×C的字符矩阵来表示。字符S表示阿尔吉侬所在的位置,字符E表示奶酪所在的位置,字符#表示墙壁,字符.表示可以通行。阿尔吉侬在1个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。

【输入】

第一行是一个正整数T(1 ≤ T ≤ 10),表示一共有T组数据。

每一组数据的第一行包含了两个用空格分开的正整数R和C(2 ≤ R, C ≤ 200),表示地图是一个R×C的矩阵。

接下来的R行描述了地图的具体内容,每一行包含了C个字符。字符含义如题目描述中所述。保证有且仅有一个S和E。

【输出】

对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。每组数据的输出结果占一行。

【输入样例】

3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.

【输出样例】

5
1
oop!

样例代码:

#include<bits/stdc++.h>
using namespace std;
char mat[205][205];
int n, m, vis[205][205];
int ans[205][205];
struct node {
	int x, y;
};
bool good(int x, int y) {
	if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && mat[x][y] == '.') {
		return true;
	}
	return false;
}//判断越界问题,标记问题和字符问题
int qx, qy, zx, zy;
int flag;
void bfs() {
	queue<node> q;
	q.push({qx, qy});
	vis[qx][qy] = 1;
	while (!q.empty()) {
		node t = q.front();
		int x = t.x;
		int y = t.y;
		q.pop();
		if (x == zx && y == zy) {
			flag = 1;
			return;
		}//找到
		if (good(x + 1, y)) {
			q.push({x + 1, y});
			vis[x + 1][y] = 1;
			ans[x + 1][y] = ans[x][y] + 1;
		}
		if (good(x - 1, y)) {
			q.push({x - 1, y});
			vis[x - 1][y] = 1;
			ans[x - 1][y] = ans[x][y] + 1;
		}
		if (good(x, y + 1)) {
			q.push({x, y + 1});
			vis[x][y + 1] = 1;
			ans[x][y + 1] = ans[x][y] + 1;
		}
		if (good(x, y - 1)) {
			q.push({x, y - 1});
			vis[x][y - 1] = 1;
			ans[x][y - 1] = ans[x][y] + 1;
		}//联通
	}
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		flag=0;
		memset(vis,0,sizeof(vis));
		memset(ans,0,sizeof(ans));
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> mat[i][j];//输入
				if (mat[i][j] == 'S') {
					qx = i;
					qy = j;
					mat[i][j] = '.';
				}
				if (mat[i][j] == 'E') {
					zx = i;
					zy = j;
					mat[i][j] = '.';
				}
			}
		}
		bfs();
        //判断
		if (flag == 1) {
			cout << ans[zx][zy] << endl;
		} else {
			cout << "oop!" << endl;
		}
	}
}

#include<bits/stdc++.h>
using namespace std;
char mat[205][205];
int n, m, vis[205][205];
int ans[205][205];
struct node {
    int x, y;
};
bool good(int x, int y) {
    if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && mat[x][y] == '.') {
        return true;
    }
    return false;
}//判断越界问题,标记问题和字符问题
int qx, qy, zx, zy;
int flag;
void bfs() {
    queue<node> q;
    q.push({qx, qy});
    vis[qx][qy] = 1;
    while (!q.empty()) {
        node t = q.front();
        int x = t.x;
        int y = t.y;
        q.pop();
        if (x == zx && y == zy) {
            flag = 1;
            return;
        }//找到
        if (good(x + 1, y)) {
            q.push({x + 1, y});
            vis[x + 1][y] = 1;
            ans[x + 1][y] = ans[x][y] + 1;
        }
        if (good(x - 1, y)) {
            q.push({x - 1, y});
            vis[x - 1][y] = 1;
            ans[x - 1][y] = ans[x][y] + 1;
        }
        if (good(x, y + 1)) {
            q.push({x, y + 1});
            vis[x][y + 1] = 1;
            ans[x][y + 1] = ans[x][y] + 1;
        }
        if (good(x, y - 1)) {
            q.push({x, y - 1});
            vis[x][y - 1] = 1;
            ans[x][y - 1] = ans[x][y] + 1;
        }//联通
    }
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        flag=0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        cin >> n >> m;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> mat[i][j];//输入
                if (mat[i][j] == 'S') {
                    qx = i;
                    qy = j;
                    mat[i][j] = '.';
                }
                if (mat[i][j] == 'E') {
                    zx = i;
                    zy = j;
                    mat[i][j] = '.';
                }
            }
        }
        bfs();
        //判断
        if (flag == 1) {
            cout << ans[zx][zy] << endl;
        } else {
            cout << "oop!" << endl;
        }
    }
}

  • 27
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值