B1970 [Ahoi2005]Code 矿藏编码 暴力模拟

小詹从哪整出来这么多水题?%%%这个题用栈模拟一下,然后直接暴力就行了。。。一开始还没想到,用的dfs,我太菜了。。。

题干:

Description

依次对每份进行编码,得S1,S2,S3,S4。该矿藏区的编码S为2S1S2S3S4。 例如上图中,矿藏区的编码为:2021010210001。 小联希望你能根据给定的编码统计出这片矿藏区一共有多少格子区域是平地。

Input

第一行有一个整数K,表示有矿藏区的规模为 (1 < K < 50)。第二行有一串编码,有0、1组成,长度不超过200,表示该矿藏区的编码。

Output

单行输出一个整数,表示矿藏区中一共有多少格子是平地。

Sample Input

2
2021010210001


Sample Output

9

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
double tot = 0;
int k,l;
stack <int> st;
char s[150];
int main()
{
    read(k);
    scanf("%s",s + 1);
    l = strlen(s + 1);
    st.push(k);
    duke(i,1,l)
    {
        int t = st.top();st.pop();
        if(s[i] == '2')
        {
            duke(j,1,4)
            {
                st.push(t - 1);
//                i++;
            }
        }
        if(s[i] == '0')
        {
//            cout<<t<<endl;
            tot += pow(2,t) * pow(2,t);
        }
//        cout<<i<<endl;
    }
    printf("%.0lf
",tot);
    return 0;
}
原文地址:https://www.cnblogs.com/DukeLv/p/9556455.html