• 【西交OJ】Problem A 扫雷


    【西交OJ】Problem A -- 扫雷

    http://202.117.21.117/xjoj/problem_html/105.html

    Description

    相信大家都玩过扫雷的游戏。那是在一个n * m的矩阵里面有一些雷,要你根据一些信息找出雷来。万圣节到了,"余"人国流行起了一种简单的扫雷游戏,这个游戏规则和扫雷一样,如果某个格子没有雷,那么它里面的数字表示和它8连通的格子里面雷的数目。现在棋盘是n * 2的,第一列里面某些格子是雷,而第二列没有雷,如下图:

    由于第一列的雷可能有多种方案满足第二列的数的限制,你的任务即根据第二列的信息确定第一列雷有多少种摆放方案。

    Input

    第一行为N(N <= 10000),第二行有N个数,依次为第二列的格子中的数。

    Output

    一个数,即第一列中雷的摆放方案数。

    Sample Input

    2
    1 1

    Sample Output

    2

    对于两竖的这种情况,其实这要前面两个格子确定了,后面的格子就唯一确定了。

        所以说,只要我们对前面两个格子进行枚举就行了。

      a[1]=2,那么前面两个格子必然就是都有雷。

      a[1]=3    前面只有两个格子,这是不可能的,直接输出0

      a[1]=1   要么第一个格子是雷,要么第二个是雷。

      a[1]=0   两个都不是雷

     1 import java.io.BufferedInputStream;
     2 import java.io.IOException;
     3 import java.util.Arrays;
     4 import java.util.Scanner;
     5 
     6 //author:pz
     7 
     8 public class Main {
     9     private static final int MAX_N = 10005;
    10     static int ans = 0;
    11 
    12     public static void main(String[] args) throws IOException {
    13         Scanner in = new Scanner(new BufferedInputStream(System.in));
    14         final int n = in.nextInt();
    15         int[] a = new int[MAX_N];
    16         for (int i = 1; i <= n; i++) {
    17             a[i] = in.nextInt();
    18         }
    19         work(a, n);
    20         System.out.print(ans);
    21     }
    22 
    23     private static void work(int[] a, int n) {
    24         int[] f = new int[MAX_N];
    25         if (a[1] == 3) {
    26             return;
    27         }
    28         if (a[1] == 2) {
    29             f[1] = 1;
    30             f[2] = 1;
    31             dp(a, f, n);
    32         }
    33         if (a[1] == 1) {
    34             f[1] = 1;
    35             dp(a, f, n);
    36             Arrays.fill(f, 0);
    37             f[2] = 1;
    38             dp(a, f, n);
    39         }
    40         if (a[1] == 0)
    41             dp(a, f, n);
    42         return;
    43     }
    44 
    45     private static void dp(int[] a, int[] f, int n) {
    46         for (int i = 3; i <= n; i++)
    47             f[i] = a[i - 1] - f[i - 1] - f[i - 2];
    48         if (f[n] + f[n - 1] == a[n])
    49             ans++;
    50     }
    51 }
  • 相关阅读:
    【Django】admin后台自定义导出全部数据并且返回自定义中文名
    关于NAT hairpin 功能相关知识
    web
    applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder 错误解决方法
    redis事件
    redis高可用性
    redis持久化
    Dockerfile同时配置tomcat和jar包运行
    Docker注册中心
    Docker构建镜像
  • 原文地址:https://www.cnblogs.com/pengzheng/p/3052728.html
Copyright © 2020-2023  润新知