2021_HUASOJ_周测5_题解代码

A题:

#include <iostream>
#include <queue>
 
using namespace std;
 
int n, m, a[109][109];
struct node {   /// 表示一个坐标
    int x, y;
    node (int x, int y) : x(x), y(y) {}  /// 自定义的一个构造函数,用于后续创建一个结构体对象
    node () {}  /// 由于自定义了构造函数,系统将不再提供参数表为空的构造函数,故自己写一个,用于创建对象 no         
}no;
int xx[] = {-1, 0, 0, 1}, yy[] = {0, -1, 1, 0};  /// 定义四种前进方向
 
int main(){
    cin >> n >> m;
    for(int i = 0; i < n; i ++) {
        getchar();  /// 读取多余的换行符
        for(int j = 0; j < m; j ++) {
            a[i][j] = getchar();
            a[i][j] -= '0';  /// 将读入的字符转化成对应的数字
        }
    }
    queue<node> qu;  /// 定义一个结构体队列
    for(int i = 0; i < n && qu.empty(); i ++) {  /// 找到起点
        for(int j = 0; j < m && qu.empty(); j ++) {
            if(a[i][j] == 2) qu.push(node(i, j));
        }
    }
    int p = 0, ans = 0;  /// 记录是否走到了出口和走到出口的步数
    while(!p) {   /// 一定可以走到出口
        ans ++;   /// 记录前进的步数
        int x = qu.size();   /// 记录当前队列中坐标的个数
        for(int i = 0; i < x; i ++) {   /// 执行一轮广搜,记录在当前行进步数下可以到达的坐标                  
            no = qu.front();  qu.pop();
            for(int i = 0; i < 4; i ++) {  /// 尝试可以行进的四个方向
                int nx = no.x + xx[i], ny = no.y + yy[i];
                if(a[nx][ny]) {  /// 如果走到的位置不是墙
                    if(a[nx][ny] == 3) p = 1;   /// 走到了出口
                    a[nx][ny] = 0;  /// 将走过的位置变成墙
                    qu.push(node(nx, ny));  /// 将当前位置放入队列
                }
            }
        }
    }
    cout << ans - 1;  /// 输出走过的 1 的个数
    return 0;
}

B题:

解法一:开桶( n^2 )

#include <iostream>
 
using namespace std;
 
int n, m, a[4][1009];
int b[8009];   /// 由于我们将基准提高了 2000 所以对应的 m 应该提高 4000
 
int main(){
    cin >> n;
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < n; j ++)
            cin >> a[i][j];
    cin >> m;
    m += 4000;
    for(int i = 0; i < n; i ++) {  /// 先计算前面两个数组能够合成的数
        for(int j = 0; j < n; j ++) {
            b[a[1][i] + a[0][j] + 2000] ++;  /// 同一个数可能有多个来源
        }
    }
    long long ans = 0;
    int x;
    for(int i = 0; i < n; i ++) {  /// 计算后面能够合成的数
        for(int j = 0; j < n; j ++) {
            x = m - (a[2][i] + a[3][j] + 2000);  /// 计算我需要的“另一半”
            if(x >= 0 && x <= 4000) ans += b[x]; /// 如果这个值可能存在
        }
    }
    cout << ans;
    return 0;
}

解法二:双指针遍历( n^2logn )
思路来源:张治强

#include <iostream>
#include <algorithm>
 
using namespace std;
 
int n, a[4][1009], m;
int x[1000009], y[1000009], t1, t2;
int to[1000009];
 
int main() {
    cin >> n;
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < n; j ++)
            cin >> a[i][j];
    cin >> m;
    for(int i = 0; i < n; i ++)  /// 分别计算前后两组能组成的值,分别装入 x、y 数组中
        for(int j = 0; j < n; j ++){
            x[t1 ++] = a[0][i] + a[1][j];
            y[t2 ++] = a[2][i] + a[3][j];
        }
    sort(x, x + t1);  sort(y, y + t2);  /// 排序两个数组
    to[0] = 1;
    for(int i = 1; i < t2; i ++) {   /// 计算当前位置和前面有多少个与自己相同的数 (包含当前位置)
        to[i] = 1;
        if(y[i] == y[i - 1]) to[i] += to[i - 1];
    }
    long long ans = 0;  /// 答案可能达到 n^4
    for(int i = 0, j = t2 - 1; i < t1 && ~j; i ++) {  /// i 和 j 分别标记 x、y 数组,其中 i 从前往后,j 从后往前        
        while (~j && x[i] + y[j] > m)  /// 和过大,减少 y 数组提供的值
            j --;
        if(~j && x[i] + y[j] == m) ans += to[j];
    }  /// (~(-1)) == 0
    cout << ans;
    return 0;
}

C题:

#include <iostream>
#include <algorithm>
 
using namespace std;
 
long long a, b, p;
 
long long mul(long long x, long long y){   /// 快速乘
    long long ans = 0;
    while(y){
        if(y & 1) ans = (ans + x) % p;
        x = x * 2 % p;
        y >>= 1;
    }   return ans;
}
long long power(){  /// 快速幂
    long long ans = min(1ll, a);
    while(b){
        if(b & 1) ans = mul(ans, a) % p;
        a = mul(a, a) % p;
        b >>= 1;
    }   return ans;
}
int main(){
    int t;
    cin >> t;
    while(t --){
        cin >> a >> b >> p;
        cout << power() << endl;
    }
    return 0;
}

D题:

#include <iostream>
#include <cstdio>
 
using namespace std;
 
int n, m, a[509][509];  /// 记录给出的字符矩阵
int b[509][509];  /// 记录当前位置上面有多少个和自己相同的字符
int xx[] = {-1, 1, -1, 1}, yy[] = {-1, -1, 1, 1};/// 对应后面四个位置的四种走法
 
int fu(int x, int y) {
    int ans = 1, cou = 0;  /// 最少有一种蝴蝶
    int x1[] = {x, x, x, x}, y1[] = {y, y, y, y};/// 记录 左上、左下、右上、右下 四个位置
    while(1) {
        cou ++;  /// 记录蝴蝶的种数
        for(int i = 0; i < 4; i ++) { /// 枚举四种走法
            x1[i] += xx[i];  y1[i] += yy[i];
        }
        if(a[x1[0]][y1[0]] != a[x1[1]][y1[1]] || a[x1[0]][y1[0]] != 1
           || a[x1[2]][y1[2]] != a[x1[3]][y1[3]] || a[x1[3]][y1[3]] != 2) /// 判断当前的四个顶点是否符合蝴蝶的定义
            break;  /// 不再是蝴蝶
        if(b[x1[1]][y1[1]] >= cou * 2 && b[x1[3]][y1[3]] >= cou * 2) /// 边长和种数有关,如果这个边长下四个点之间的填充符也成立       
            ans ++;
    }
    return ans;
}
int main(){
    cin >> n >> m;
    char c;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m ;j ++) {
            cin >> c;  /// 自动跳过换行符
            if(c == 'X') a[i][j] = 1;  /// 分别标记两种字符
            else a[i][j] = 2;
        }
    }
    int sum;  /// 记录上面和自己相同字符的个数
    for(int i = 1; i <= m; i ++) {  /// 由于记录的是上下位置,故先枚举列后枚举行
        for(int j = 1; j <= n; j ++) {
            if(a[j][i] != a[j - 1][i]) sum = 0;  /// 不等就置零
            b[j][i] = sum;
            sum ++;
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++) {
        for(int j = 1; j <= m; j ++) {
            if(a[i][j] == 1) {  /// 如果这个位置是 X
                ans += fu(i, j);  /// 求出以这个位置为中心的蝴蝶的种数
            }
        }
    }
    cout << ans;
    return 0;
}

E题:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值