2017年蓝桥杯省赛C/C++ A组

题目转载至:

https://wenku.baidu.com/view/951dab772a160b4e767f5acfa1c7aa00b52a9d2d.html

本题解不一定全对,欢迎指出错误。

第一题

 

1.标题:迷宫

X星球的一处迷宫游乐场建在某个小山坡上。

它是由10x10相互连通的小房间组成的。

房间的地板上写着一个很大的字母。

我们假设玩家是面朝上坡的方向站立,则:

L表示走到左边的房间,

R表示走到右边的房间,

U表示走到上坡方向的房间,

D表示走到下坡方向的房间。

X星球的居民有点懒,不愿意费力思考。

他们更喜欢玩运气类的游戏。这个游戏也是如此!

开始的时候,直升机把100名玩家放入一个个小房间内。

玩家一定要按照地上的字母移动。

迷宫地图如下:

------------

UDDLUULRUL

UURLLLRRRU

RRUURLDLRD

RUDDDDUUUU

URUDLLRRUU

DURLRLDLRL

ULLURLLRDU

RDLULLRDDD

UUDDUDUDLL

ULRDLUURRR

------------

请你计算一下,最后,有多少玩家会走出迷宫?

而不是在里边兜圈子。

请提交该整数,表示走出迷宫的玩家数目,不要填写任何多余的内容。

如果你还没明白游戏规则,可以参看一个简化的4x4迷宫的解说图:

 

图1迷宫

 

代码:

 

#include <cstdio>

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;


char maps[30][30] = {};
bool book[30][30] = {};
int cnt = 0;


void dfs(int stx, int sty)
{
    if(stx < 1 || stx > 10 || sty < 1 || sty > 10)
    {
        cnt++;
        return;
    }
    if(!book[stx][sty] && maps[stx][sty] == 'U')
    {
        book[stx][sty] = true;
        dfs(stx - 1, sty);
        //book[stx][sty] = false;
    }
    if(!book[stx][sty] && maps[stx][sty] == 'D')
    {
        book[stx][sty] = true;
        dfs(stx + 1, sty);
        //book[stx][sty] = false;
    }
    if(!book[stx][sty] && maps[stx][sty] == 'L')
    {
        book[stx][sty] = true;
        dfs(stx, sty - 1);
        //book[stx][sty] = false;
    }
    if(!book[stx][sty] && maps[stx][sty] == 'R')
    {
        book[stx][sty] = true;
        dfs(stx, sty + 1);
        //book[stx][sty] = false;
    }


}
int main()
{
    ///10 9
    char ch;
    for(int i = 1; i <= 10; i++)
    {
        for(int j = 1; j <= 10; j++)
        {
            cin >> maps[i][j];
        }
        getchar();
    }
    for(int i = 1; i <= 10; i++)
    {
        for(int j = 1; j <= 10; j++)
        {
            //book[i][j] = true;
            dfs(i, j);
            book[i][j] = false;
            memset(book, 0, sizeof(book));
        }
    }
    cout << cnt << endl;
    return 0;
}
/**
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
*/

答案:31

第二题    跳蚱蜢

 

有9只盘子,排成1个圆圈。

其中8只盘子内装着8只蚱蜢,有一个是空盘。

我们把这些蚱蜢顺时针编号为 1~8

每只蚱蜢都可以跳到相邻的空盘中,

也可以再用点力,越过一个相邻的蚱蜢跳到空盘中。

请你计算一下,如果要使得蚱蜢们的队形改为按照逆时针排列,

并且保持空盘的位置不变(也就是1-8换位,2-7换位,...),至少要经过多少次跳跃?

注意:要求提交的是一个整数,请不要填写任何多余内容或说明文字。

 

图2. 跳蚱蜢

 

思路:广搜

 

代码:

#include<cstdio>

#include <cstdlib>
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;


int minn = 999999999, cnt = 0;
int nexts[4] = {1, 2, -1, -2};
map<string, int>mps;
struct node
{
    string str;
    int step;
    node(string st, int ss = 0)
    {
        str = st;
        step = ss;
    }
    node() {};
};
bool check(string str)
{
    return (str == "087654321" || str == "876543210");
}
int getz(string str)
{
    for(int i = 0; i < str.size(); i++)
    {
        if(str[i] == '0') return i;
    }
}
int bfs()
{
    queue<node>first;
    first.push(node("012345678", 0));
    while(!first.empty())
    {
        if(check(first.front().str))
        {
            return first.front().step;
        }
        string inits = first.front().str, ex;
        int steps = first.front().step;
        first.pop();
        int zero = getz(inits), one;
        cnt++;
        //if(first.front().step > 10 || cnt > 10000000) cout << first.front().step << "  " << cnt << endl;
        for(int i = 0; i < 4; i++)
        {
            cnt++;
            ex = inits;
            if(zero + nexts[i] > 8)
            {
                one = (zero + nexts[i]) % 9;
            }
            else if(zero + nexts[i] < 0)
            {
                one = zero + nexts[i] + 9;
            }
            else
            {
                one = zero + nexts[i];
            }
            if(zero > 8 || zero < 0 || one < 0 || one > 8)
            {
                cout << zero << " + " << nexts[i] << " = " << one << endl;
                system("pause");
            }
            swap(ex[zero], ex[one]);
            bool flag = true;
            if(mps[ex] == 0)
            {
                mps[ex] = 1;
                //cout << ex << endl;
                if(check(ex)) return steps + 1;
                first.push(node(ex, steps + 1));
            }
        }
    }
    return -1;
}
int main()
{
    printf("%d\n", bfs());
    return 0;

}

答案:20

第三题

 

二阶魔方就是只有2层的魔方,只由8个小块组成。

小明很淘气,他只喜欢3种颜色,所有把家里的二阶魔方重新涂了颜色,如下:

前面:橙色

右面:绿色

上面:黄色

左面:绿色

下面:橙色

后面:黄色

请你计算一下,这样的魔方被打乱后,一共有多少种不同的状态。

如果两个状态经过魔方的整体旋转后,各个面的颜色都一致,则认为是同一状态。

请提交表示状态数的整数,不要填写任何多余内容或说明文字。

  

                                                                                          图3 魔方状态

代码:

 

第四题    方块分割

 

6x6的方格,沿着格子的边线剪开成两部分。

要求这两部分的形状完全相同。

如图4-1,4-2,4-3:就是可行的分割法。

试计算:

包括这3种分法在内,一共有多少种不同的分割方法。

注意:旋转对称的属于同一种分割法。

请提交该整数,不要填写任何多余的内容或说明文字。

                                                                                          图4-1 方格分割

 

思路:一开始我真不知道怎么做,看了题解后才恍然大悟。

 

题解:http://blog.csdn.net/Mai_Dreizehn/article/details/70196045

#include<cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;


bool book[23][23] = {};
int cnt = 0;
int nextx[4] = {-1, 0, 1, 0};
int nexty[4] = {0, -1, 0, 1};
void dfs(int stx, int sty)
{
    if(stx == 0 || stx == 6 || sty == 0 || sty == 6)
    {
        cnt++;
        return;
    }
    for(int i = 0; i < 4; i++)
    {
        int xx = stx + nextx[i];
        int yy = sty + nexty[i];
        if(!book[xx][yy])
        {
            book[xx][yy] = true;
            book[6 - xx][6 - yy] = true;
            dfs(xx, yy);
            book[xx][yy] = false;
            book[6 - xx][6 - yy] = false;
        }
    }
}
int main()
{
    book[3][3] = true;
    dfs(3, 3);
    cout << cnt / 4 << endl;
    return 0;

}

答案:509

第五题   代码填空

 

 

由 A,B,C 这3个字母就可以组成许多串。

比如:"A","AB","ABC","ABA","AACBB" ....

现在,小明正在思考一个问题:

如果每个字母的个数有限定,能组成多少个已知长度的串呢?

他请好朋友来帮忙,很快得到了代码,

解决方案超级简单,然而最重要的部分却语焉不详。

请仔细分析源码,填写划线部分缺少的内容。

 

#include <stdio.h>

// a个A,b个B,c个C 字母,能组成多少个不同的长度为n的串。

int f(int a, int b, int c, int n)

{

if(a<0 || b<0 || c<0) return 0;

if(n==0) return 1;

 

return ______________________________________ ;  // 填空

}

int main()

{

printf("%d\n", f(1,1,1,2));

printf("%d\n", f(1,2,3,3));

return 0;

}

 

对于上面的测试数据,小明口算的结果应该是:

6

19

 

注意:只填写划线部分缺少的代码,不要提交任何多余内容或说明性文字。

填空:a[i][j] = a[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 1 : 0);

 

6. 标题:最大公共子串

最大公共子串长度问题就是:

求两个串的所有子串中能够匹配上的最大长度是多少。

比如:"abcdkkk" 和 "baabcdadabc",

可以找到的最长的公共子串是"abcd",所以最大公共子串长度为4。

下面的程序是采用矩阵法进行求解的,这对串的规模不大的情况还是比较有效的解法。

请分析该解法的思路,并补全划线部分缺失的代码。

 

#include <stdio.h>

#include <string.h>

 

#define N 256

int f(const char* s1, const char* s2)

{

int a[N][N];

int len1 = strlen(s1);

int len2 = strlen(s2);

int i,j;

 

memset(a,0,sizeof(int)*N*N);

int max = 0;

for(i=1; i<=len1; i++){

for(j=1; j<=len2; j++){

if(s1[i-1]==s2[j-1]) {

a[i][j] = __________________________;  //填空

if(a[i][j] > max) max = a[i][j];

}

}

}

 

return max;

}

 

int main()

{

printf("%d\n", f("abcdkkk", "baabcdadabc"));

return 0;

}

 

注意:只提交缺少的代码,不要提交已有的代码和符号。也不要提交说明性文字。

答案:这个能对就行了

第七题

 

题意:像(x|xx)这样的序列中,我们需要取‘|’左边的‘x’数目与右边的最大值。

思路:这题就是考代码能力的题,我也只是想了些小技巧做出来的。首先,把连续的‘x’的数目记录下来,并把‘(‘记为-1,‘|’记为-2,’)’记为-3,然后把这些都存到一个数组中,并判断当数组中存在连续数列:-1, >0, -2, >0, -3,(即是:(x|xx)这样的出现)时就进行计算,并用另一个数组进行记录,以此类推,直至‘|’符号数目为零。

代码:

#include <iostream>

#include <string>
#include <cstring>
#include <algorithm>
using namespace std;


int maps[3][340] = {}, sum = 0;
///((xx|xxx)x|(x|xx))xx
///xx(((xx|xx)x|xx)x(x|xx)|x)
void dfs(int step, int cnt)
{
    int flag = 1, f1, ans;
    while(step)
    {
        //cout << cnt << "  " << step <<endl;
        ans = 0;
        flag = (flag + 1) % 2;
        f1 = (flag + 1) % 2;
        for(int i = 1; i <= cnt; i++)
        {
            //cout << i << "  " << maps[i][flag] << "  " << maps[ans][f1] << endl;
            if(maps[flag][i]== -1 &&maps[flag][i+1]>= 0&&maps[flag][i+2]==-2&&maps[flag][i+3]>=0 &&maps[flag][i+4]==-3)
            {
                maps[f1][++ans] = max(maps[flag][i + 1], maps[flag][i + 3]);
                step--;
                int cc = i;
                //cout << i << endl;
                i += 4;
                if(maps[flag][cc - 1] > 0)
                {
                    while(maps[flag][cc - 1] > 0)
                    {
                        maps[f1][ans] += maps[flag][--cc];
                        //cout << cc << "  " << maps[f1][ans];
                    }
                }
                //cout << endl;
                if(maps[flag][i + 1] > 0)
                {
                    while(maps[flag][i + 1] > 0)
                    {
                        maps[f1][ans] += maps[flag][++i];
                        //cout << i << "  " << maps[f1][ans];
                    }
                    //cout << endl;
                }
            }
            else
            {
                if(maps[flag][i] > 0 && maps[flag][i + 1] > 0) continue;
                if(maps[flag][i] > 0 && maps[flag][i - 1] > 0) continue;
                if(maps[flag][i] > 0 && maps[flag][i + 1] == -1 && maps[flag][i + 2] > 0) continue;
                maps[f1][++ans] = maps[flag][i];
            }
        }
        /*for(int i = 1; i <= ans; i++)
        {
            cout << maps[f1][i] << "  ";
        }
        cout << endl;
        cout << "*** " << endl;*/
        memset(maps[flag], 0, sizeof(maps[flag]));
        cnt = ans;
    }
    cout << maps[f1][1] << endl;
}
int main()
{
    string str;
    while(cin >> str)
    {
        sum = 0;
        int cnt = 0;
        for(int i = 0; i < str.size(); i++)
        {
            if(str[i] == 'x')
            {
                int cc = i;
                while(str[i] == 'x')
                    i++;
                maps[0][++cnt] = i - cc;
                i--;
            }
            if(str[i] == '(') maps[0][++cnt] = -1;
            if(str[i] == '|')
            {
                sum++;
                maps[0][++cnt] = -2;
            }
            if(str[i] == ')') maps[0][++cnt] = -3;
        }
        /*for(int i = 1; i <= cnt; i++)
        {
            cout << maps[0][i] << "  ";
        }
        cout << endl;*/
        dfs(sum, cnt);
        memset(maps, 0, sizeof(maps));

    }

    return 0;

}

 

9. 标题: 分巧克力

    儿童节那天有K位小朋友到小明家做客。小明拿出了珍藏的巧克力招待小朋友们。

    小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形。

 

    为了公平起见,小明需要从这 N 块巧克力中切出K块巧克力分给小朋友们。切出的巧克力需要满足:

 

    1. 形状是正方形,边长是整数  

    2. 大小相同  

 

例如一块6x5的巧克力可以切出6块2x2的巧克力或者2块3x3的巧克力。

 

当然小朋友们都希望得到的巧克力尽可能大,你能帮小Hi计算出最大的边长是多少么?

 

输入

第一行包含两个整数N和K。(1 <= N, K <= 100000)  

以下N行每行包含两个整数Hi和Wi。(1 <= Hi, Wi <= 100000)

输入保证每位小朋友至少能获得一块1x1的巧克力。   

 

输出

输出切出的正方形巧克力最大可能的边长。

 

样例输入:

2 10  

6 5  

5 6  

 

样例输出:

2

 

资源约定:

峰值内存消耗(含虚拟机) < 256M

CPU消耗  < 1000ms

 

 

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

 

注意:

main函数需要返回0;

只使用ANSI C/ANSI C++ 标准;

不要调用依赖于编译环境或操作系统的特殊函数。

所有依赖的函数必须明确地在源文件中 #include <xxx>

不能通过工程设置而省略常用头文件。

 

提交程序时,注意选择所期望的语言类型和编译器类型。

思路:二分法

代码:

#include<cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 100045;
int arr[maxn][3] = {};
void cal(int n, int k, int maxx)
{
    int ll = 1, rr = maxx;
    while(ll < rr)
    {
        int mid = (ll + rr) / 2;
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            cnt += (arr[i][1] / mid) * (arr[i][2] / mid);
        }
        if(cnt > k)
        {
            ll = mid + 1;
        }
        else rr = mid - 1;
    }
    cout << rr << endl;
}
int main()
{
    int n, k;
    while(scanf("%d %d", &n, &k) != EOF)
    {
        int maxx = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= 2; j++)
            {
                scanf("%d", &arr[i][j]);
                maxx = max(maxx, arr[i][j]);
            }
        }
        cal(n, k, maxx);
    }
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值