码迷,mamicode.com
首页 > 其他好文 > 详细

秋招编程真题:方阵中的数的影响过程

时间:2019-10-12 22:47:31      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:ace   div   输出   nbsp   main   clu   编程   turn   while   

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

影响规则如下:

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;
        }

    }
}

  

 

秋招编程真题:方阵中的数的影响过程

标签:ace   div   输出   nbsp   main   clu   编程   turn   while   

原文地址:https://www.cnblogs.com/liugl7/p/11664200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!