zoukankan      html  css  js  c++  java
  • 秋招编程真题:方阵中的数的影响过程

    有一个方阵数据,数据之间会相互影响。

    影响规则如下:

    1.负数既不被影响,也不影响其他数

    2.较大的数会影响其‘上下左右’四个方向的数,将他们改变为该较大数减1.

    请模拟影响过程。

    输入:

    第一行,一个数字n,n代表方阵的行数和列数。

    第二行~第n+1行,每行有n个数。第i行的n个数,代表方阵的第i行的n个数。

    eg:

    4

    1 4 2 3 

    -1 4 7 -1

    0 3 8 2

    2 7 3 5

    输出描述:

    输出影响过后的方阵。

    eg:

    4 5 6 5

    -1 6 7 -1

    6 7 8 7

    6 7 7 6

    示例程序:

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    struct point{
        int x;
        int y;
    };
    
    void getmax(queue<point>& res, vector<vector<bool> >& visited, vector<vector<int> >& map, int& n){
        int max = 0;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(!visited[i][j] && map[i][j] > max){
                    max = map[i][j];
                }
            }
        }
    
        if(max == 0) return ;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(!visited[i][j] && map[i][j] == max){
                    point cur;
                    cur.x = i;
                    cur.y = j;
                    visited[i][j] = 1;
                    res.push(cur);
                }
            }
        }
    }
    
    int main(){
        int n;
        while(cin>>n){
            vector<vector<int> > map(n, vector<int>(n) );
            vector<vector<bool> > visited(n, vector<bool>(n,0) );
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    cin>>map[i][j];
                    if(map[i][j] < 0) visited[i][j] = 1;
                }
            }
    
            vector<vector<int> > dir = {{-1,0},{1,0},{0,1},{0,-1}};
            queue<point> temp;
            getmax(temp, visited, map, n);
            while( !temp.empty() ){
                while( !temp.empty() ){
                    point cur_point = temp.front();
                    temp.pop();
                    for(int i=0; i<4; i++){
                        int cur_x = cur_point.x + dir[i][0];
                        int cur_y = cur_point.y + dir[i][1];
                        if(cur_x<0 || cur_x>=n || cur_y<0 ||cur_y>=n || visited[cur_x][cur_y]) continue;
                        map[cur_x][cur_y] = map[cur_point.x][cur_point.y] - 1;
                    }
                }
                getmax(temp, visited, map, n);
            }
    
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    cout<<map[i][j]<<" ";
                }
                cout<<endl;
            }
    
        }
    }
    

      

  • 相关阅读:
    模板
    CF1271E Common Number
    模板——长链剖分
    XJOI NOIP501/511训练22 ttt学字符串
    POJ 1151 Atlantis
    BZOJ 1014 [JSOI2008]火星人prefix
    Luogu P1856 [USACO5.5]矩形周长Picture
    CF716D Complete The Graph
    Luogu P2596 [ZJOI2006]书架
    HTML 学习之JavaScript作用域
  • 原文地址:https://www.cnblogs.com/liugl7/p/11664200.html
Copyright © 2011-2022 走看看