蓝桥杯每周一题

本文介绍了扫雷游戏的规则,并给出了一个4x4的示例。文章还说明了输入和输出格式,以及如何处理输入数据。样例输入和输出展示了扫雷字段的表示方式及其解析方法。

每周一题之2  Mineweep(扫雷)

Minesweeper (扫雷)
PC/UVa IDs: 110102/10189,
Popularity: A,
Success rate: high Level: 1

测试地址:https://vjudge.net/problem/UVA-10189

[问题描述]
Have you ever played Minesweeper? It’s a cute little game which comes within a certain Operating
System which name we can’t really remember. Well, the goal of the game is to find where are all the mines within a M × N field. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the following 4 × 4 field with 2 mines (which are represented by an ‘*’ character):
*...
....
.*..
....
If we would represent the same field placing the hint numbers described above, we would end up
with:
*100
2210
1*10
1110
As you may have already noticed, each square may have at most 8 adjacent squares.

[输入]
The input will consist of an arbitrary number of fields. The first line of each field contains two integers
n and m (0 < n, m ≤ 100) which stands for the number of lines and columns of the field respectively.
The next n lines contains exactly m characters and represent the field.
Each safe square is represented by an ‘.’ character (without the quotes) and each mine square
is represented by an ‘*’ character (also without the quotes). The first field line where n = m = 0
represents the end of input and should not be processed.

[输出]
对于每对整数 i 和 j,按原来的顺序输出 i 和 j,然后输出二者之间的整数中的最大循环节长度。这三个整数应该用单个空格隔开,且在同一行输出。对于读入的每一组数据,在输出中应位于单独的一行。


 

[样例输入]
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0

[样例输出]
Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100
*/

 

每周一题解析:

#include "stdafx.h"
#include<iostream>
#include <string>
#include<queue>
#include<algorithm>
using namespace std;
int m, n;//N为行数,M为列数
int C = 1;//次数
char map[102][102];//扫雷地图
int b[101][101] = { 0 };
int main()
{
	while (cin>>n>>m)
	{
		if (m == 0 || n == 0)
		{
			C++;
				continue;
		}
		for (int i = 0; i < n; i++) {
			for (int  j = 0; j < m; j++)
			{
				cin >> map[i][j];
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (map[i][j] == '.')
				{
					if (map[i - 1][j] == '*') {
						b[i][j]++;
					}
					if (map[i + 1][j] == '*') {
						b[i][j]++;
					}
					if (map[i - 1][j-1] == '*') {
						b[i][j]++;
					}
					if (map[i - 1][j+1] == '*') {
						b[i][j]++;
					}
					if (map[i + 1][j-1] == '*') {
						b[i][j]++;
					}
					if (map[i + 1][j+1] == '*') {
						b[i][j]++;
					}
					if (map[i ][j-1] == '*') {
						b[i][j]++;
					}if (map[i ][j+1] == '*') {
						b[i][j]++;
					}
					map[i][j] = b[i][j] + '0';
				}
				else
				{
					continue;
				}
			}
		}
		cout << "Filed #" << C << endl;
		C++;
		for (int i = 0; i < n; i++)
		{
			for (int  j = 0; j < m; j++)
			{
				cout << map[i][j] ;
			}
			cout << endl;
		}
		cout << endl;
	}
	 
    return 0;
}

 

### 蓝桥杯 C++ 填空 示例及解析 #### 目背景 蓝桥杯竞赛中的填空通常考察选手的基础编程能力以及逻辑思维能力。这类目往往不需要完整的程序实现,而是通过观察规律或者简单计算得出答案。 --- #### 示例一:序列求和问 给定一个正整数 \( n \),定义如下递推关系: \[ f(1) = 1, \quad f(n) = f(n-1) + n^2 \text{ 当 } n > 1 \] 输入一个正整数 \( n \),输出对应的 \( f(n) \) 的值。 ##### 解析 此问可以通过简单的循环累加来解决。以下是具体的代码实现: ```cpp #include <iostream> using namespace std; long long calculate_f(int n) { long long result = 0; for (int i = 1; i <= n; ++i) { result += i * i; // 累加平方项 } return result; } int main() { int n; cin >> n; cout << calculate_f(n) << endl; return 0; } ``` 上述代码的核心在于利用循环逐步累积每一项的平方值[^4]。 --- #### 示例二:日期转换问 假设某年有 \( A \) 天工作日(周一至周五),\( B \) 天周末(周六、周日)。已知总天数为 \( N \),问至少需要几周才能完成这些天的工作安排? ##### 已知条件 每周固定包含 5 个工作日和 2 个休息日。因此每周期间总数为 \( T = A*5 + B*2 \)[^2]。 ##### 解法思路 先计算完整周数所需的天数部分,再处理剩余不足一周的部分。具体算法见下述伪码描述: ```cpp ll total_days(ll workdays_per_week, ll weekend_per_week, ll target_total){ ll weekly_sum = workdays_per_week * 5 + weekend_per_week * 2; ll full_weeks = target_total / weekly_sum; ll remaining_days = target_total % weekly_sum; if(remaining_days != 0){ full_weeks++; } return full_weeks * 7; } ``` 该方法能够有效应对各种规模的数据范围,并且时间复杂度仅为 O(1)[^3]。 --- #### 示例三:最大子数组乘积 给出长度不超过 100 的整型数组 `arr` ,求数组中连续若干个元素的最大乘积是多少?如果存在负数,则需特别注意边界情况。 ##### 实现细节 采用动态规划的思想维护当前可能的最大正值与最小负值状态转移方程分别为 max_dp[i]=max(max_dp[i−1]*nums[i],min_dp[i−1]*nums[i],nums[i]) 和 min_dp[i]=min(min_dp[i−1]*nums[i],max_dp[i−1]*nums[i],nums[i])[^5]。 最终返回整个过程中记录下的全局最优解即可满足需求。 ```cpp #include <vector> #include <algorithm> double findMaxProduct(vector<int>& nums){ double current_max=nums[0]; double current_min=nums[0]; double global_max=nums[0]; for(size_t i=1;i<nums.size();i++){ if(nums[i]<0){ swap(current_max,current_min); } current_max=max((double)nums[i],current_max*nums[i]); current_min=min((double)nums[i],current_min*nums[i]); global_max=max(global_max,current_max); } return global_max; } ``` 以上即为针对不同类型的典型蓝桥杯 C++ 填空及其解答方式介绍[^6]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值