ACM | Inner PK 7

L1-1 最好的文档

有一位软件工程师说过一句很有道理的话:“Good code is its own best documentation.”(好代码本身就是最好的文档)。本题就请你直接在屏幕上输出这句话。

输入格式:

本题没有输入。

输出格式:

在一行中输出 Good code is its own best documentation.。

输入样例:

无 ## 输出样例: Good code is its own best documentation.

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#define N 10
using namespace std;


int main(){
    cout<<"Good code is its own best documentation."<<endl;
    return 0;
}

L1-2 什么是机器学习

什么是机器学习?上图展示了一段面试官与“机器学习程序”的对话:

面试官:9 + 10 等于多少?

答:3

面试官:差远了,是19。

答:16

面试官:错了,是19。

答:18

面试官:不,是19。

答:19

本题就请你模仿这个“机器学习程序”的行为。

输入格式:

输入在一行中给出两个整数,绝对值都不超过 100,中间用一个空格分开,分别表示面试官给出的两个数字 A 和 B。

输出格式:

要求你输出 4 行,每行一个数字。第 1 行比正确结果少 16,第 2 行少 3,第 3 行少 1,最后一行才输出 A+B 的正确结果。

输入样例:

9 10

输出样例:

3
16
18
19

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#define N 10
using namespace std;


int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b-16<<endl;
    cout<<a+b-3<<endl;
    cout<<a+b-1<<endl;
    cout<<a+b<<endl;
    return 0;
}

L1-3 程序员买包子

这是一条检测真正程序员的段子:假如你被家人要求下班顺路买十只包子,如果看到卖西瓜的,买一只。那么你会在什么情况下只买一只包子回家? 本题要求你考虑这个段子的通用版:假如你被要求下班顺路买 \(N\) 只包子,如果看到卖 \(X\) 的,买 \(M\) 只。那么如果你最后买了 \(K\) 只包子回家,说明你看到卖 \(X\) 的没有呢?

输入格式:

输入在一行中顺序给出题面中的 \(N、X、M、K\),以空格分隔。其中 \(N、M\)\(K\) 为不超过 1000 的正整数,\(X\) 是一个长度不超过 10 的、仅由小写英文字母组成的字符串。题目保证 \(N!=M\)

输出格式:

在一行中输出结论,格式为:

  • 如果 \(K=N\),输出 mei you mai X de
  • 如果 \(K=M\),输出 kan dao le mai X de
  • 否则输出 wang le zhao mai X de.其中 X 是输入中给定的字符串 \(X\)

输入样例 1:

10 xigua 1 10

输出样例 1:

mei you mai xigua de

输入样例 2:

10 huanggua 1 1

输出样例 2:

kan dao le mai huanggua de

输入样例 3:

10 shagua 1 250

输出样例 3:

wang le zhao mai shagua de

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#define N 10
using namespace std;


int main(){
    int n,m,k;
    string x;
    cin>>n>>x>>m>>k;
    if(k==n){
        cout<<"mei you mai "<<x<<" de"<<endl;
    }else if(k==m){
        cout<<"kan dao le mai "<<x<<" de"<<endl;
    }else{
        cout<<"wang le zhao mai "<<x<<" de"<<endl;
    }
    return 0;
}

L1-4 进化论

在“一年一度喜剧大赛”上有一部作品《进化论》,讲的是动物园两只猩猩进化的故事。猩猩吕严说自己已经进化了 9 年了,因为“三年又三年”。猩猩土豆指出“三年又三年是六年呐”…… 本题给定两个数字,以及用这两个数字计算的结果,要求你根据结果判断,这是吕严算出来的,还是土豆算出来的。

输入格式:

输入第一行给出一个正整数 \(N\),随后 \(N\) 行,每行给出三个正整数 \(A、B\)\(C\)。其中 \(C\) 不超过 \(10000\),其他三个数字都不超过 \(100\)

输出格式:

对每一行给出的三个数,如果 \(C\)\(A×B\),就在一行中输出 Lv Yan;如果是 \(A+B\),就在一行中输出 Tu Dou;如果都不是,就在一行中输出 zhe du shi sha ya!

输入样例:

3
3 3 9
3 3 6
3 3 12

输出样例:

Lv Yan
Tu Dou
zhe du shi sha ya!

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#define N 10
using namespace std;


int main(){
    int t;
    cin>>t;
    while(t--){
        int a,b,c;
        cin>>a>>b>>c;
        if(a*b==c){
            cout<<"Lv Yan"<<endl;
        }else if(a+b==c){
            cout<<"Tu Dou"<<endl;
        }else{
            cout<<"zhe du shi sha ya!"<<endl;
        }
    }
    return 0;
}

L1-5 猜帽子游戏

宝宝们在一起玩一个猜帽子游戏。每人头上被扣了一顶帽子,有的是黑色的,有的是黄色的。每个人可以看到别人头上的帽子,但是看不到自己的。游戏开始后,每个人可以猜自己头上的帽子是什么颜色,或者可以弃权不猜。如果没有一个人猜错、并且至少有一个人猜对了,那么所有的宝宝共同获得一个大奖。如果所有人都不猜,或者只要有一个人猜错了,所有宝宝就都没有奖。 下面顺序给出一排帽子的颜色,假设每一群宝宝来玩的时候,都是按照这个顺序发帽子的。然后给出每一群宝宝们猜的结果,请你判断他们能不能得大奖。

输入格式:

输入首先在一行中给出一个正整数 \(N(2<N≤100)\),是帽子的个数。第二行给出 \(N\) 顶帽子的颜色,数字 1 表示黑色,2 表示黄色。 再下面给出一个正整数 \(K(≤10)\),随后 \(K\) 行,每行给出一群宝宝们猜的结果,除了仍然用数字 1 表示黑色、2 表示黄色之外,0 表示这个宝宝弃权不猜。 同一行中的数字用空格分隔。

输出格式:

对于每一群玩游戏的宝宝,如果他们能获得大奖,就在一行中输出 Da Jiang!!!,否则输出 Ai Ya

输入样例:

5
1 1 2 1 2
3
0 1 2 0 0
0 0 0 0 0
1 2 2 0 2

输出样例:

Da Jiang!!!
Ai Ya
Ai Ya

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#define N 10
using namespace std;


int main(){
    int n;
    cin>>n;
    int hat[n];
    for(int i=0;i<n;i++){
        cin>>hat[i];
    }
    int t;
    cin>>t;
    int test[n];
    while(t--){
        for(int i=0;i<n;i++){
            cin>>test[i];
        }
        bool flag=true;
        int count=0;
        for(int i=0;i<n;i++){
            if(test[i]==0){
                count++;
            }
        }
        if(count==n){
            cout<<"Ai Ya"<<endl;
            continue;
        }
        for(int i=0;i<n;i++){
            if(test[i]==0 || test[i]==hat[i]){
                continue;
            }else{
                flag=false;
                break;
            }
        }
        if(flag==false){
            cout<<"Ai Ya\n";
        }else{
            cout<<"Da Jiang!!!"<<endl;
        }
    }
    return 0;
}

L1-6 剪切粘贴

分数 15 作者 DAI, Longao 单位 杭州百腾教育科技有限公司 使用计算机进行文本编辑时常见的功能是剪切功能(快捷键:Ctrl + X)。请实现一个简单的具有剪切和粘贴功能的文本编辑工具。

工具需要完成一系列剪切后粘贴的操作,每次操作分为两步:

  • 剪切:给定需操作的起始位置和结束位置,将当前字符串中起始位置到结束位置部分的字符串放入剪贴板中,并删除当前字符串对应位置的内容。例如,当前字符串为 abcdefg,起始位置为 3,结束位置为 5,则剪贴操作后, 剪贴板内容为 cde,操作后字符串变为 abfg。字符串位置从 1 开始编号。
  • 粘贴:给定插入位置的前后字符串,寻找到插入位置,将剪贴板内容插入到位置中,并清除剪贴板内容。例如,对于上面操作后的结果,给定插入位置前为 bf,插入位置后为 g,则插入后变为 abfcdeg。如找不到应该插入的位置,则直接将插入位置设置为字符串最后,仍然完成插入操作。查找字符串时区分大小写。 每次操作后的字符串即为新的当前字符串。在若干次操作后,请给出最后的编辑结果。

输入格式:

输入第一行是一个长度小于等于 200 的字符串 \(S\),表示原始字符串。字符串只包含所有可见 ASCII 字符,不包含回车与空格。

第二行是一个正整数 \(N (1≤N≤100)\),表示要进行的操作次数。

接下来的 \(N\) 行,每行是两个数字和两个 长度不大于 5 的不包含空格的非空字符串,前两个数字表示需要剪切的位置,后两个字符串表示插入位置前和后的字符串,用一个空格隔开。如果有多个可插入的位置,选择最靠近当前操作字符串开头的一个。

剪切的位置保证总是合法的。

输出格式:

输出一行,表示操作后的字符串。

输入样例:

AcrosstheGreatWall,wecanreacheverycornerintheworld
5
10 18 ery cor
32 40 , we
1 6 tW all
14 18 rnerr eache
1 1 e r

输出样例:

he,allcornetrrwecaneacheveryGreatWintheworldAcross

Code

#include <iostream>
#include <string>
using namespace std;

void cutAndPaste(string& str,int cutStart,int cutEnd,const string& before,const string& after) {
    string clipboard=str.substr(cutStart-1,cutEnd-cutStart+1);
    str.erase(cutStart-1,cutEnd-cutStart+1);
    size_t pos=str.find(before+after);
    if (pos!=string::npos){
        str.insert(pos+before.size(),clipboard);
    }else{
        str+=clipboard;
    }
}

int main(){
    string s;
    getline(cin,s);
    int n;
    cin>>n;

    for (int i=0;i<n;++i) {
        int start,end;
        string before,after;
        cin>>start>>end>>before>>after;
        cutAndPaste(s,start,end,before,after);
    }

    cout<<s<<endl;
    return 0;
}

L1-7 分寝室

学校新建了宿舍楼,共有 \(n\) 间寝室。等待分配的学生中,有女生 \(n_0\) 位、男生 \(n_1\) 位。所有待分配的学生都必须分到一间寝室。所有的寝室都要分出去,最后不能有寝室留空。 现请你写程序完成寝室的自动分配。分配规则如下:

  • 男女生不能混住;
  • 不允许单人住一间寝室;
  • 对每种性别的学生,每间寝室入住的人数都必须相同;例如不能出现一部分寝室住 2 位女生,一部分寝室住 3 位女生的情况。但女生寝室都是 2 人一间,男生寝室都是 3 人一间,则是允许的;
  • 在有多种分配方案满足前面三项要求的情况下,要求两种性别每间寝室入住的人数差最小。

输入格式:

输入在一行中给出 3 个正整数 \(n_0、n_1、n\),分别对应女生人数、男生人数、寝室数。数字间以空格分隔,均不超过 \(10^5\)

输出格式:

在一行中顺序输出女生和男生被分配的寝室数量,其间以 1 个空格分隔。行首尾不得有多余空格。

如果有解,题目保证解是唯一的。如果无解,则在一行中输出 No Solution

输入样例 1:

24 60 10

输出样例 1:

4 6

注意:输出的方案对应女生都是 24/4=6 人间、男生都是 60/6=10 人间,人数差为 4。满足前三项要求的分配方案还有两种,即女生 6 间(都是 4 人间)、男生 4 间(都是 15 人间);或女生 8 间(都是 3 人间)、男生 2 间(都是 30 人间)。但因为人数差都大于 4 而不被采用。

输入样例 2:

29 30 10

输出样例 2:

No Solution

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main(){
    int n,g,b;
    cin>>g>>b>>n;
    int ans1,ans2;
    bool flag=false;
    int max=1000000;
    for(int i=1;i<=n-1;i++){
        if(g%i==0 && b%(n-i)==0){
            if(abs(g/i - b/(n-i))<max && g!=i && b!=n-i){
                flag=true;
                ans1=i;
                ans2=n-i;
                max=abs(g/i - b/(n-i));
            }
        }
    }
    if(flag)
        cout<<ans1<<" "<<ans2<<endl;
    else
        cout<<"No Solution"<<endl;
    return 0;
}

L1-8 谁管谁叫爹

《咱俩谁管谁叫爹》是网上一首搞笑饶舌歌曲,来源于东北酒桌上的助兴游戏。现在我们把这个游戏的难度拔高一点,多耗一些智商。 不妨设游戏中的两个人为 \(A\)\(B\)。游戏开始后,两人同时报出两个整数 \(N_A\)\(N_B\)。判断谁是爹的标准如下:

将两个整数的各位数字分别相加,得到两个和 \(S_A\)\(S_B\) 。如果 \(N_A\) 正好是 \(S_B\) 的整数倍,则 \(A\) 是爹;如果 \(N_B\) 正好是 \(S_A\)的整数倍,则 \(B\) 是爹; 如果两人同时满足、或同时不满足上述判定条件,则原始数字大的那个是爹。 本题就请你写一个自动裁判程序,判定谁是爹。

输入格式:

输入第一行给出一个正整数 \(N(≤100)\),为游戏的次数。以下 \(N\) 行,每行给出一对不超过 9 位数的正整数,对应 \(A\)\(B\) 给出的原始数字。题目保证两个数字不相等。

输出格式:

对每一轮游戏,在一行中给出赢得“爹”称号的玩家(\(A\)\(B\) )。

输入样例:

4
999999999 891
78250 3859
267537 52654299
6666 120

输出样例:

B
A
B
A

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int na,nb;
        cin>>na>>nb;
        int t1=na,t2=nb;
        int sa=0,sb=0;
        while(t1>0 || t2>0){
            sa+=t1%10;
            t1/=10;
            sb+=t2%10;
            t2/=10;
        }
        if((na%sb==0 && nb%sa==0) || (na%sb!=0 && nb%sa!=0)){
            if(na>nb)   cout<<"A"<<endl;
            else    cout<<"B"<<endl;
        }else if(na%sb==0){
            cout<<"A"<<endl;
        }else{
            cout<<"B"<<endl;
        }
    }
}

L2-1 堆宝塔

堆宝塔游戏是让小朋友根据抓到的彩虹圈的直径大小,按照从大到小的顺序堆起宝塔。但彩虹圈不一定是按照直径的大小顺序抓到的。聪明宝宝采取的策略如下:

  • 首先准备两根柱子,一根 A 柱串宝塔,一根 B 柱用于临时叠放。
  • 把第 1 块彩虹圈作为第 1 座宝塔的基座,在 A 柱放好。
  • 将抓到的下一块彩虹圈 C 跟当前 A 柱宝塔最上面的彩虹圈比一下,如果比最上面的小,就直接放上去;否则把 C 跟 B 柱最上面的彩虹圈比一下:
    • 如果 B 柱是空的、或者 C 大,就在 B 柱上放好;
    • 否则把 A 柱上串好的宝塔取下来作为一件成品;然后把 B 柱上所有比 C 大的彩虹圈逐一取下放到 A 柱上,最后把 C 也放到 A 柱上。 重复此步骤,直到所有的彩虹圈都被抓完。最后 A 柱上剩下的宝塔作为一件成品,B 柱上剩下的彩虹圈被逐一取下,堆成另一座宝塔。问:宝宝一共堆出了几个宝塔?最高的宝塔有多少层?

输入格式:

输入第一行给出一个正整数 \(N(≤10^3)\),为彩虹圈的个数。第二行按照宝宝抓取的顺序给出 \(N\) 个不超过 100 的正整数,对应每个彩虹圈的直径。

输出格式:

在一行中输出宝宝堆出的宝塔个数,和最高的宝塔的层数。数字间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

11
10 8 9 5 12 11 4 3 1 9 15

输出样例:

4 5

样例解释:

宝宝堆成的宝塔顺次为:

10、8、5
12、11、4、3、1
9
15、9

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main(){
    int n;
    cin>>n;
    int temp;
    queue<int> que;
    for(int i=0;i<n;i++){
        cin>>temp;
        que.push(temp);
    }
    int tower=0,high=0;
    stack<int> sta1,sta2;
    while(!que.empty()){
        if(sta1.empty()){ //可以放A柱
            sta1.push(que.front());
            que.pop();
            continue;
        }
        if(que.front()<=sta1.top()){//可以放A柱
            sta1.push(que.front());
            que.pop();
            continue;
        }
        if(sta2.empty()){ //可以放B柱
            sta2.push(que.front());
            que.pop();
            continue;
        }
        if(sta2.top()<que.front()){
            sta2.push(que.front());
            que.pop();
            continue;
        }
        {//把A柱清空
            tower++;
            if(sta1.size()>high){
                high=sta1.size();
            }
            while(!sta1.empty()){
                sta1.pop();
            }
            while(!sta2.empty()){
                if(que.front()<=sta2.top()){
                    sta1.push(sta2.top());
                    sta2.pop();
                }else{
                    break;
                }
            }
        }
    }
    //剩余的两个柱子
    if(sta1.size()>high){
        high=sta1.size();
    }
    if(sta2.size()>high){
        high=sta2.size();
    }
    cout<<tower+2<<" "<<high<<endl;
    return 0;
}

L2-3 锦标赛

\(2^k\) 名选手将要参加一场锦标赛。锦标赛共有 k 轮,其中第 i 轮的比赛共有 \(2^{k−i}\) 场,每场比赛恰有两名选手参加并从中产生一名胜者。每场比赛的安排如下:

  • 对于第 \(1\) 轮的第 \(j\) 场比赛,由第 \((2j−1)\) 名选手对抗第 \(2j\) 名选手。
  • 对于第 \(i\) 轮的第 \(j\) 场比赛 \((i>1)\),由第 \((i−1)\) 轮第 \((2j−1)\) 场比赛的胜者对抗第 \((i−1)\) 轮第 \(2j\) 场比赛的胜者。
  • \(k\) 轮唯一一场比赛的胜者就是整个锦标赛的最终胜者。

举个例子,假如共有 \(8\) 名选手参加锦标赛,则比赛的安排如下:

  • 第 1 轮共 4 场比赛:选手 1 vs 选手 2,选手 3 vs 选手 4,选手 5 vs 选手 6,选手 7 vs 选手 8。
  • 第 2 轮共 2 场比赛:第 1 轮第 1 场的胜者 vs 第 1 轮第 2 场的胜者,第 1 轮第 3 场的胜者 vs 第 1 轮第 4 场的胜者。
  • 第 3 轮共 1 场比赛:第 2 轮第 1 场的胜者 vs 第 2 轮第 2 场的胜者。

已知每一名选手都有一个能力值,其中第 \(i\) 名选手的能力值为 \(a_i\) 。在一场比赛中,若两名选手的能力值不同,则能力值较大的选手一定会打败能力值较小的选手;若两名选手的能力值相同,则两名选手都有可能成为胜者。

\(l_{i,j}\) 表示第 \(i\) 轮第 \(j\) 场比赛 败者 的能力值,令 \(w\) 表示整个锦标赛最终胜者的能力值。给定所有满足 \(1≤i≤k\)\(1≤j≤2^{k−i}\)\(l_{i,j}\) 以及 \(w\) ,请还原出 \(a_1, a_2, ⋯, a_n\)

输入格式:

第一行输入一个整数 \(k(1≤k≤18)\)表示锦标赛的轮数。

对于接下来 \(k\) 行,第 \(i\) 行输入 \(2_{k−i}\) 个整数 \(l_{i,1},l_{i,2},⋯,l_{i,2^{k−i}} (1≤l_{i,j}≤10^9)\),其中 \(l_{i,j}\) 表示第 \(i\) 轮第 \(j\) 场比赛 败者 的能力值。

接下来一行输入一个整数 \(w(1≤w≤10^9)\)表示锦标赛最终胜者的能力值。

输出格式:

输出一行 \(n\) 个由单个空格分隔的整数 \(a_1,a_2,⋯,a_n\) ,其中 \(a_i\) 表示第 \(i\) 名选手的能力值。如果有多种合法答案,请输出任意一种。如果无法还原出能够满足输入数据的答案,输出一行 No Solution

请勿在行末输出多余空格。

输入样例1:

3
4 5 8 5
7 6
8
9

输出样例1:

7 4 8 5 9 8 6 5

输入样例2:

2
5 8
3
9

输出样例2:

No Solution

L2-4 寻宝图

给定一幅地图,其中有水域,有陆地。被水域完全环绕的陆地是岛屿。有些岛屿上埋藏有宝藏,这些有宝藏的点也被标记出来了。本题就请你统计一下,给定的地图上一共有多少岛屿,其中有多少是有宝藏的岛屿。

输入格式:

输入第一行给出 2 个正整数 \(N\)\(M(1<N×M≤10^5)\),是地图的尺寸,表示地图由 \(N\)\(M\) 列格子构成。随后 \(N\) 行,每行给出 \(M\) 位个位数,其中 0 表示水域,1 表示陆地,2-9 表示宝藏。 注意:两个格子共享一条边时,才是“相邻”的。宝藏都埋在陆地上。默认地图外围全是水域。

输出格式:

在一行中输出 2 个整数,分别是岛屿的总数量和有宝藏的岛屿的数量。

输入样例:

10 11
01000000151
11000000111
00110000811
00110100010
00000000000
00000111000
00114111000
00110010000
00019000010
00120000001

输出样例:

7 2

Code1

1Runtime Error(ACCESS_VIOLATION) + 1WA

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;

int map[1000][1100];
bool used[1000][1100];

int n,m;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //Directions

bool bfs(int i,int j){
    bool treasure=false;
    used[i][j]=true;
    if(map[i][j]>1) treasure=true;
    for(int k=0;k<4;k++){
        if(map[i+dir[k][0]][j+dir[k][1]]>0 && used[i+dir[k][0]][j+dir[k][1]]==false){
            if(bfs(i+dir[k][0],j+dir[k][1]))   treasure=true;
        }
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    memset(map,0,sizeof(map));
    memset(used,false,sizeof(used));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            map[i][j]=getchar()-'0';
        }
        getchar();
    }
    // cout<<endl;
    // for(int i=0;i<=n+1;i++){
    //     for(int j=0;j<=m+1;j++){
    //         cout<<map[i][j];
    //     }
    //     cout<<endl;
    // }
    int island=0,treasure=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(used[i][j]==false && map[i][j]){
                island++;
                if(bfs(i,j)){
                    treasure++;
                }
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}
## Code2 1Runtime Error(ACCESS_VIOLATION) + 1WA
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;

int map[2000][2000];
bool used[2000][2000];

int n,m;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //Directions

bool bfs(int i,int j){
    bool treasure=false;
    used[i][j]=true;
    if(map[i][j]>1) treasure=true;
    for(int k=0;k<4;k++){
        if(map[i+dir[k][0]][j+dir[k][1]]>0 && used[i+dir[k][0]][j+dir[k][1]]==false){
            if(bfs(i+dir[k][0],j+dir[k][1]))   treasure=true;
        }
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    memset(map,0,sizeof(map));
    memset(used,false,sizeof(used));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            map[i][j]=getchar()-'0';
        }
        getchar();
    }
    // cout<<endl;
    // for(int i=0;i<=n+1;i++){
    //     for(int j=0;j<=m+1;j++){
    //         cout<<map[i][j];
    //     }
    //     cout<<endl;
    // }
    int island=0,treasure=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(used[i][j]==false && map[i][j]){
                island++;
                if(bfs(i,j)){
                    treasure++;
                }
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}

Code3

1Runtime Error(ACCESS_VIOLATION) + 3WA 第一次BFS

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;

int map[2000][2000];
bool used[2000][2000];

int n,m;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //Directions

//之前把广搜写成深搜了
bool dfs(int i,int j){
    bool treasure=false;
    used[i][j]=true;
    if(map[i][j]>1) treasure=true;
    for(int k=0;k<4;k++){
        if(map[i+dir[k][0]][j+dir[k][1]]>0 && used[i+dir[k][0]][j+dir[k][1]]==false){
            if(dfs(i+dir[k][0],j+dir[k][1]))   treasure=true;
        }
    }
    return treasure;
}

//BFS
bool bfs(int x,int y){
    queue<pair<int,int> > q;
    bool treasure=false;
    used[x][y]=true;
    for(int i=0;i<4;i++){
        int a=x+dir[i][0],b=y+dir[i][1];
        q.push(make_pair(a,b));
    }
    while(!q.empty()){
        pair<int,int> now=q.front();
        if(map[now.first][now.second] && used[now.first][now.second]==false){
            for(int i=0;i<4;i++){
                int a=now.first+dir[i][0],b=now.second+dir[i][1];
                q.push(make_pair(a,b));
            }
            if(map[now.first][now.second]>1){
                treasure=true;
            }
        }
        used[now.first][now.second]=true;
    // cout<<"*"<<endl;
        q.pop();
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    memset(map,0,sizeof(map));
    memset(used,false,sizeof(used));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            map[i][j]=getchar()-'0';
        }
        getchar();
    }
    // cout<<endl;
    // for(int i=0;i<=n+1;i++){
    //     for(int j=0;j<=m+1;j++){
    //         cout<<map[i][j];
    //     }
    //     cout<<endl;
    // }
    int island=0,treasure=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(used[i][j]==false && map[i][j]){
                island++;
                if(bfs(i,j)){
                    treasure++;
                }
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}

Code4

1Runtime Error(ACCESS_VIOLATION) + 1WA

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;

int map[2000][2000];
bool used[2000][2000];

int n,m;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //Directions

//之前把广搜写成深搜了
bool dfs(int i,int j){
    bool treasure=false;
    used[i][j]=true;
    if(map[i][j]>1) treasure=true;
    for(int k=0;k<4;k++){
        if(map[i+dir[k][0]][j+dir[k][1]]>0 && used[i+dir[k][0]][j+dir[k][1]]==false){
            if(dfs(i+dir[k][0],j+dir[k][1]))   treasure=true;
        }
    }
    return treasure;
}

//BFS
bool bfs(int x,int y){
    queue<pair<int,int> > q;
    bool treasure=map[x][y]>1?true:false;
    used[x][y]=true;
    q.push(make_pair(x,y));
    while(!q.empty()){
        pair<int,int> now=q.front();
        q.pop();
        if(map[now.first][now.second]>1)    treasure=true;
        used[now.first][now.second]=true;
        for(int i=0;i<4;i++){
            int a=now.first+dir[i][0],b=now.second+dir[i][1];
            if(!used[a][b] && map[a][b]){
                used[a][b]=true;
                q.push(make_pair(a,b));
                if(map[a][b]>1){
                    treasure=true;
                }
            }
        }
        // if(map[now.first][now.second] && used[now.first][now.second]==false){
        //     for(int i=0;i<4;i++){
        //         int a=now.first+dir[i][0],b=now.second+dir[i][1];
        //         q.push(make_pair(a,b));
        //     }
        //     if(map[now.first][now.second]>1){
        //         treasure=true;
        //     }
        // }
        // used[now.first][now.second]=true;
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    memset(map,0,sizeof(map));
    memset(used,false,sizeof(used));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            map[i][j]=getchar()-'0';
        }
        getchar();
    }
    // cout<<endl;
    // for(int i=0;i<=n+1;i++){
    //     for(int j=0;j<=m+1;j++){
    //         cout<<map[i][j];
    //     }
    //     cout<<endl;
    // }
    int island=0,treasure=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(used[i][j]==false && map[i][j]){
                island++;
                if(bfs(i,j)){
                    treasure++;
                }
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}

Code5

24分!只剩段错误了!!! 还不知道问题出在哪里

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#define N 1000
using namespace std;

int map[N][N];
bool used[N][N];
int n,m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //Directions

bool bfs(int x,int y){
    queue<pair<int,int> > q;
    bool treasure=map[x][y]>1?true:false;
    used[x][y]=true;
    q.push(make_pair(x,y));
    while(!q.empty()){
        pair<int,int> now=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int a=now.first+dir[i][0],b=now.second+dir[i][1];
            if(a>=0 && a<n && b>=0 && b<m && !used[a][b] && map[a][b]){
                used[a][b]=true;
                q.push(make_pair(a,b));
                if(map[a][b]>1) treasure=true;
            }
        }
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    // memset(map,0,sizeof(map));
    memset(used,false,sizeof(used));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            map[i][j]=getchar()-'0';
        }
        getchar();
    }
    int island=0,treasure=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(used[i][j]==false && map[i][j]){
                island++;
                if(bfs(i,j))    treasure++;
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}

Code6

最后发现改成vector就能过了。??? NICE

#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> islandmap;
int n,m;
int c[4]={0,0,1,-1};
int r[4]={1,-1,0,0};//FourDirections

bool bfs(int x,int y){
    queue<pair<int,int> > q;
    bool treasure=islandmap[x][y]>1?true:false;
    islandmap[x][y]=0;
    q.push(make_pair(x,y));
    while(!q.empty()){
        pair<int,int> now=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int a=now.first+c[i],b=now.second+r[i];
            if(a>=0 && a<n && b>=0 && b<m && islandmap[a][b]){
                q.push(make_pair(a,b));
                if(islandmap[a][b]>1) treasure=true;
                islandmap[a][b]=0;
            }
        }
    }
    return treasure;
}

int main(){
    cin>>n>>m;
    getchar();
    for(int i=0;i<n;i++){
        islandmap.push_back(vector<int>{});
        for(int j=0;j<m;j++){
            islandmap[i].push_back(getchar()-'0');
        }
        getchar();
    }
    int island=0,treasure=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(islandmap[i][j]){
                island++;
                if(bfs(i,j))    treasure++;
            }
        }
    }
    cout<<island<<" "<<treasure<<endl;
    return 0;
}

ACM | Inner PK 7
https://acm.nanyan.cc/posts/fd0d.html
作者
nanyan
发布于
2023年12月17日
许可协议