首页 > 试题广场 >

俄罗斯方块

[编程题]俄罗斯方块
  • 热度指数:29584 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小易有一个古老的游戏机,上面有着经典的游戏俄罗斯方块。因为它比较古老,所以规则和一般的俄罗斯方块不同。
荧幕上一共有 n 列,每次都会有一个 1 x 1 的方块随机落下,在同一列中,后落下的方块会叠在先前的方块之上,当一整行方块都被占满时,这一行会被消去,并得到1分。
有一天,小易又开了一局游戏,当玩到第 m 个方块落下时他觉得太无聊就关掉了,小易希望你告诉他这局游戏他获得的分数。

输入描述:
第一行两个数 n, m
第二行 m 个数,c1, c2, ... , cm , ci 表示第 i 个方块落在第几列
其中 1 <= n, m <= 1000, 1 <= ci <= n


输出描述:
小易这局游戏获得的分数
示例1

输入

3 9
1 1 2 2 2 3 1 2 3

输出

2
package com.shengxi.niuke;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * 题目描述
 * 小易有一个古老的游戏机,上面有着经典的游戏俄罗斯方块。因为它比较古老,所以规则和一般的俄罗斯方块不同。
 * 荧幕上一共有 n 列,每次都会有一个 1 x 1 的方块随机落下,在同一列中,
 * 后落下的方块会叠在先前的方块之上,当一整行方块都被占满时,这一行会被消去,并得到1分。
 * 有一天,小易又开了一局游戏,当玩到第 m 个方块落下时他觉得太无聊就关掉了,
 * 小易希望你告诉他这局游戏他获得的分数。
 * 输入描述:
 * <p>
 * 第一行两个数 n, m
 * 第二行 m 个数,c1, c2, ... , cm , ci 表示第 i 个方块落在第几列
 * 其中 1 <= n, m <= 1000, 1 <= ci <= n
 * <p>
 * 输出描述:
 * <p>
 * 小易这局游戏获得的分数
 * <p>
 * 3 9
 * 1 1 2 2 2 3 1 2 3
 * <p>
 * <p>
 * 2
 * <p>
 * 这里是一个无限多行的俄罗斯方块,所以本质问题是考虑如果避免超长
 *
 * @author yan
 */
public class Day003 {

    /**
     * 列数n
     */
    private static int numColumns;
    private static int[] lists;

    public static void main(String[] args) {
        scan();
        int result = lists[0];
        for (int list : lists) {
            result = Math.min(list, result);
        }
        System.out.println(result);
    }

    private static void scan() {
        Scanner scan = new Scanner(System.in);
        numColumns = scan.nextInt();
        lists = new int[numColumns];
        int numChessPieces = scan.nextInt();
        for (int i = 0; i < numChessPieces; i++) {
            lists[scan.nextInt() - 1]++;
        }
    }
}

发表于 2020-05-20 14:54:23 回复(0)
思路,利用hashmap创建键值对关系,key表示第几列的格子,value表示对应的格子上有多少个方格;遍历m个格子之前先初始化好界面资源。也就是先把从1到n的key放入map,value置为0;然后每次put一个ci,对应的value++;最后找到最小的value即可。
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=1;i<=n;i++)
        {
            map.put(i,0);
        }
        TreeSet<Integer> set = new TreeSet<>();
        while(m-->0)
        {
            int c = sc.nextInt();
            map.put(c,map.get(c)+1);
        }
        sc.close();
        if(map.containsValue(0))
        {
            System.out.println(0);
            return ;
        }
        set.addAll(map.values());
        System.out.println(set.first());
    }
}
发表于 2020-04-26 14:15:09 回复(0)
//没大佬厉害,只能一步一步想,总是不考虑时间复杂度问题唉
import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();
            int m = sc.nextInt();
            int []fa = new int[m];
            int []fa2 = new int[n];
            for(int i=0;i<m;i++){
                fa[i]=sc.nextInt();
            }

            for(int i=1;i<=n;i++){
                int count=0;
               for(int j=0;j<fa.length;j++){
                   if(fa[j]==i){
                      count++;
                   }
                   }
                 fa2[i-1] = count;
            }
        Arrays.sort(fa2);
            System.out.println(fa2[0]);
    }
}
发表于 2020-03-31 22:44:46 回复(0)
使用HashMap的getOrDefault()方法减少代码量
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] c = new int[m];
        for(int i = 0;i < m;i++){
            c[i] = sc.nextInt();
        }
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < m;i++){
            map.put(c[i],map.getOrDefault(c[i],0)+1);
        }
        int count = Integer.MAX_VALUE;      //for循环迭代要最小值,输出的初始值设为最大值即可
        if(map.size() == n){
            for(int i = 0;i < m-1;i++){
                count = Math.min(count,map.get(c[i]));
            }
        }else{
            count = 0;
        }
        
        System.out.println(count);
    }
}

编辑于 2020-03-26 15:16:58 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x = 0;
        boolean isfull = false;
        int min = 0;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < m;i++){
            x = sc.nextInt();
            if(map.get(x) == null){
                map.put(x,1);
            }else{
                int temp = map.get(x) + 1;
                map.put(x,temp);
            }
        }
        for(int j = 1;j <= n;j++){
            if(!map.containsKey(j)){
                isfull = false;
                break;
            }
            isfull = true;
        }
        if(isfull){
            Collection<Integer> values = map.values();
            min = Collections.min(values);
        }
        System.out.println(min);
    }
}

发表于 2020-03-06 22:11:11 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<m;i++){
            int t=sc.nextInt();
            a[t-1]++;
        }
            Arrays.sort(a);
            System.out.print(a[0]);
    }
}


编辑于 2020-02-25 20:48:51 回复(1)
import java.util.Scanner;
public class Main{
    public static void main (String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int min=m;
        int index=0;
        int arrm[]=new int[m];
        for(int i=0;i<arrm.length;i++){
            arrm[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<arrm.length;j++){
                if(i==arrm[j]){
                    index++;
                }
            }
            if(min>=index){
                min=index;
            }
            index=0;
        }
        System.out.println(min);
    }
}
发表于 2019-09-26 11:46:38 回复(0)
import java.util.*;
//思路就是存储每个方块出现的次数,最后求次数最小的方块,即为得分数。
public class Main{
    public static void main(String[] args){
        Map<Integer, Integer> map = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for(int i = 0; i < m; i++){
            int temp = sc.nextInt();
            if(map.containsKey(temp)){
                map.put(temp, map.get(temp) + 1);
            }else{
                map.put(temp, 1);
            }
        }
        //判断每个元素出现的次数,次数最小的就是得分
        int score = Integer.MAX_VALUE;
        if(map.size() == n){ //保证n列上只出现n个不同的方块
            for(Integer key : map.keySet()){
            score = Math.min(score, map.get(key));
        }
        }else{
            score = 0;
        }
        System.out.print(score);
    }
}

发表于 2019-09-17 09:08:20 回复(1)
一个麻烦的解法。。。
import java.util.*;

public class Main {

    static class Element {
        int pos, time;
        public Element(int pos, int time) {
            this.pos = pos;
            this.time = time;
        }
    }

    public static void main(String[] args) {
	    // write your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        PriorityQueue<Element> pq = new PriorityQueue<>(new Comparator<Element>() {
            @Override
            public int compare(Element o1, Element o2) {
                if(o1.time == o2.time) {
                    return 0;
                }
                return o1.time > o2.time ? 1 : -1;
            }
        });
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < m; i++) {
            int pos = sc.nextInt();
            if(map.containsKey(pos)) {
                map.put(pos, map.get(pos) + 1);
            } else {
                map.put(pos,1);
            }
        }

        if(map.size() < n) {
            System.out.println(0);
            return;
        }

        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            pq.offer(new Element(entry.getKey(), entry.getValue()));
        }

        System.out.println(pq.peek().time);

    }
}


发表于 2019-09-13 07:33:32 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String [] args)
    {
        //输入:第一行两个数,
        //第二行m个数, c1, c2, ..., Cm, Ci表示第i个方块落在第几列
        
        //遍历这ci个数,然后, 找出最少的那个值就是得分
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();    //获取到n
        int [] pos = new int[n];
        int m = sc.nextInt();
        for(int i=0; i<m; i++)
        {
           int c = sc.nextInt();
            pos[c-1]++;
        }
        int min = pos[0];
        for(int i=0; i<n; i++)
        {
            min = min<pos[i]?min:pos[i];
        }
        System.out.println(min);
    }
}
发表于 2019-09-01 21:04:22 回复(0)
 import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] arrm = new int[m];
        int[] arrn = new int[n];
        for(int i=0;i<m;i++){
            arrm[i]=sc.nextInt();
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(arrm[i]==j+1)
                    arrn[j]+=1;
            }
        }
        int minp = arrn[0];
        for(int i=0;i<n;i++){
            minp=minp<arrn[i]?minp:arrn[i];
        }
        System.out.println(minp);
    }
}

发表于 2019-08-23 22:32:01 回复(0)
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int res[] = new int[n+1];//将荧幕宽设置为一个数组
        for(int i=0;i<m;i++)
            res[sc.nextInt()]++;//落在哪一列就加1
        Arrays.sort(res);
        System.out.println(res[1]);//找到最矮的那列
    }
}

发表于 2019-08-15 18:33:33 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int score = 0;
		int[][] p = new int[m][n];//用于存放位置
		int[] ci = new int[m];
		for (int i = 0; i < m; i++) {
			ci[i] = sc.nextInt();
		}
		int j = 0;
		int k = 0;
		for (int i = 0; i < m; i++) {
			while(p[j][ci[i]-1]!=0){
				j++;
			}
			p[j][ci[i]-1] = 1;
			j = 0;
			if(isFull(p[k])){
				score++;
				downMove(p);
			}
        }
        
		System.out.println(score);
	}
	public static boolean isFull(int[] s){
		for(int i=0;i<s.length;i++){
			if(s[i]==0){
				return false;
			}
		}
		return true;
	}
	public static  void downMove(int[][] p){
		for(int a=0;a<p.length-1;a++){
			//每一列下移
			for(int b=0;b<p[0].length;b++){
				p[a][b] = p[a+1][b];
                if(a+1==p.length-1){
                    p[a+1][b] = 0;
                }
			}
            
		}
        
	}
}


编辑于 2019-10-06 17:08:46 回复(0)

Java

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int num = sc.nextInt();
            if (!map.containsKey(num)) {
                map.put(num, 1);
            } else {
                map.put(num, map.get(num) + 1);
            }
        }
        int min = Integer.MAX_VALUE;
        if (map.size() == n) {
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if (entry.getValue() < min) {
                    min = entry.getValue();
                }
            }
        } else {
            min = 0;
        }
        System.out.println(min);
    }
}
发表于 2019-06-30 13:51:10 回复(0)