zoukankan      html  css  js  c++  java
  • 【BZOJ1088】[SCOI2005] 扫雷Mine(分类讨论)

    点此看题面

    大致题意: 给你一个(2*n)的扫雷棋盘,现让你根据第二列的信息确定第一列有多少种摆法。

    扫雷性质

    听说这是一道动态规划+数学题。

    其实,根据扫雷游戏某个性质只要确定了第一个格子是否有雷,就可以确定整列雷的分布情况

    因此,最多只可能有两种摆法。

    这样一来,只要对第一个格子是否有雷分类讨论即可,遇到合法的情况就将(ans)(1)

    如何确定整列雷的分布情况

    我们再来说一下应如何根据第一个格子是否有雷来确定整列雷的分布情况。

    我们可以用(a_i)来存储第二列的信息,并用(s_i)来表示第(i)位是否有雷。

    则对于第(x)位,不难发现(s_{x-2}+s_{x-1}+s_x=a_{x-1})

    由于(a_{x-1})是题目给出的,所以确定了(s_{x-2})(s_{x-1}),我们就可以得到(s_x=a_{x-1}-s_{x-2}-s_{x-1})

    然后,我们就可以对(s_x)是否合法进行判断,从而判断是否存在不合法情况。

    代码

    #include<bits/stdc++.h>
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)<(y)?(x):(y))
    #define uint unsigned int
    #define LL long long
    #define ull unsigned long long
    #define swap(x,y) (x^=y,y^=x,x^=y)
    #define abs(x) ((x)<0?-(x):(x))
    #define INF 1e9
    #define Inc(x,y) ((x+=(y))>=MOD&&(x-=MOD))
    #define ten(x) (((x)<<3)+((x)<<1))
    #define N 10000
    using namespace std;
    int n,a[N+5],s[N+5];
    class FIO
    {
    	private:
    		#define Fsize 100000
    		#define tc() (FinNow==FinEnd&&(FinEnd=(FinNow=Fin)+fread(Fin,1,Fsize,stdin),FinNow==FinEnd)?EOF:*FinNow++)
    		#define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
    		int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
    	public:
    		FIO() {FinNow=FinEnd=Fin;}
    		inline void read(int &x) {x=0,f=1;while(!isdigit(ch=tc())) f=ch^'-'?1:-1;while(x=ten(x)+(ch&15),isdigit(ch=tc()));x*=f;}
    		inline void read_char(char &x) {while(isspace(x=tc()));}
    		inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
    		inline void write(int x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
    		inline void write_char(char x) {pc(x);}
    		inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
    		inline void end() {fwrite(Fout,1,FoutSize,stdout);}
    }F;
    inline bool check(int x)//检验s[1]=x的情况是否可行
    {
    	register int i;
    	for(s[1]=x,i=2;i<=n+1;++i) 
    	{
    		s[i]=a[i-1]-s[i-1]-s[i-2];//计算出s[i]
    		if(s[i]&&s[i]^1) return false;//如果s[i]不等于0或1,说明不合法,返回false
    	}
    	return !s[n+1];//如果最后一位没有多余,返回true,否则返回false
    }
    int main()
    {
        register int i;
        for(F.read(n),i=1;i<=n;++i) F.read(a[i]);
        return F.write(check(0)+check(1)),F.end(),0;//分两种情况讨论
    }
    
  • 相关阅读:
    python变量和常量
    python运算符
    python 数据类型强制转换
    Python 变量的缓存机制
    Python格式化输出
    Python 自动类型转换
    Python 六大标准基础数据类型
    Python 基础
    pyhton 初识
    计算机基础
  • 原文地址:https://www.cnblogs.com/chenxiaoran666/p/BZOJ1088.html
Copyright © 2011-2022 走看看