zoukankan      html  css  js  c++  java
  • 201512-2 消除类游戏 (水题,暴力)

    问题描述
      消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消除。当有多处可以被消除时,这些地方的棋子将同时被消除。
      现在给你一个n行m列的棋盘,棋盘中的每一个方格上有一个棋子,请给出经过一次消除后的棋盘。
      请注意:一个棋子可能在某一行和某一列同时被消除。
    输入格式
      输入的第一行包含两个整数n, m,用空格分隔,分别表示棋盘的行数和列数。
      接下来n行,每行m个整数,用空格分隔,分别表示每一个方格中的棋子的颜色。颜色使用1至9编号。
    输出格式
      输出n行,每行m个整数,相邻的整数之间使用一个空格分隔,表示经过一次消除后的棋盘。如果一个方格中的棋子被消除,则对应的方格输出0,否则输出棋子的颜色编号。
    样例输入
    4 5
    2 2 3 1 2
    3 4 5 1 4
    2 3 2 1 3
    2 2 2 4 4
    样例输出
    2 2 3 0 2
    3 4 5 0 4
    2 3 2 0 3
    0 0 0 4 4
    样例说明
      棋盘中第4列的1和第4行的2可以被消除,其他的方格中的棋子均保留。
    样例输入
    4 5
    2 2 3 1 2
    3 1 1 1 1
    2 3 2 1 3
    2 2 3 3 3
    样例输出
    2 2 3 0 2
    3 0 0 0 0
    2 3 2 0 3
    2 2 0 0 0
    样例说明
      棋盘中所有的1以及最后一行的3可以被同时消除,其他的方格中的棋子均保留。
    评测用例规模与约定
      所有的评测用例满足:1 ≤ n, m ≤ 30。
    析:直接分别从行和列都扫一下是不是有连续的多个相同数字即可。
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 30 + 5;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int a[maxn][maxn];
    int ans[maxn][maxn];
    
    int main(){
        while(scanf("%d %d", &n, &m) == 2){
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < m; ++j)
                    scanf("%d", &a[i][j]);
            memcpy(ans, a, sizeof a);
            for(int i = 0; i < n; ++i){
                int cnt = 1;
                for(int j = 1; j < m; ++j){
                    if(a[i][j] == a[i][j-1])  ++cnt;
                    else{
                        if(cnt > 2){
                            for(int k = j-1; k >= j-cnt; --k)
                                ans[i][k] = 0;
                        }
                        cnt = 1;
                    }
                    if(j == m-1 && cnt > 2)
                        for(int k = j; k > j-cnt; --k)
                                ans[i][k] = 0;
                }
            }
    
            for(int i = 0; i < m; ++i){
                int cnt = 1;
                for(int j = 1; j < n; ++j){
                    if(a[j][i] == a[j-1][i])  ++cnt;
                    else{
                        if(cnt > 2){
                            for(int k = j-1; k >= j-cnt; --k)
                                ans[k][i] = 0;
                        }
                        cnt = 1;
                    }
                    if(j == n-1 && cnt > 2)
                        for(int k = j; k > j-cnt; --k)
                                ans[k][i] = 0;
                }
            }
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < m; ++j)
                    if(j != m-1)  printf("%d ", ans[i][j]);
                    else  printf("%d
    ", ans[i][j]);
        }
        return 0;
    }
    
  • 相关阅读:
    php-beanstalkd消息队列类分享
    php curl抓取类分享
    df -i 100%时处理方法
    php生成红包
    PHP牛牛游戏算法
    Yii 框架不同逻辑处理方法统一事务处理
    Linux上web服务器搭建
    Linux 上pcntl安装步骤
    PHP错误级别设置
    day40 ,epoll,数据库相关概念
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5853122.html
Copyright © 2011-2022 走看看