zoukankan      html  css  js  c++  java
  • Funny Feature

    In the process of preparation to Halloween it was decided to plant some pumpkins on the n × m meters rectangular platform. The platform is divided to n × midentical square cells with 1 meter length sides.

    The arrangement of pumpkins was carefully prepared, and you are given the resulting plan. For each cell of this platform it is given how many pumpkins should be planted in it. All these quantities appear to be from 1 to 5, inclusive. 

    Special pumpkin-landing machine was bought to plant Halloween symbols. It can perform a simple operation: plant pumpkin to the cell, specified by its coordinates (the first coordinate ranges 1 to n, and the second one ranges 1 to m). 

    At the last moment an unpleasant feature of the machine was discovered. Every time this machine plants a pumpkin to the specified cell, one more pumpkin is also planted to all cells, which are adjacent to the specified one and already have at least one pumpkin in it. One cell is called adjacent to another one if they share a side.

    Besides for technical reasons you cannot specify the same cell twice.

    Now Halloween celebration is under the threat of failure. You are asked to write the program which finds the sequence of n × m operations leading to demanded landing of pumpkins, or informs that it is impossible.

    Input

    The first line of input contains two integers n and m  — sizes of the field (1 ≤ n,m ≤ 200). Next n lines contain m integers from 1 to 5  — how many pumpkins should be planted in each cell of the platform. Numbers in lines are separated by single spaces.

    Output

    If the solution exists, print n × m lines with two numbers in each: a line and a column numbers, where the next pumpkin should be landed. If there are multiple solutions, print any of them.

    If the solution does not exist, print a single line "No solution" (quotes for clarity).

    Example(s)
    sample input
    sample output
    1 2
    1 2
    
    1 2
    1 1
    
    sample input
    sample output
    1 3
    1 3 1
    
    1 2
    1 3
    1 1
    
    sample input
    sample output
    3 3
    2 4 2
    2 1 2
    3 2 3
    
    1 2
    3 1
    3 3
    1 1
    1 3
    3 2
    2 1
    2 3
    2 2
    
    sample input
    sample output
    2 1
    1
    1
    
    No solution
    
    #include<queue>
    #include<stack>
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    const int maxn=200+10;
    const int di[]={-1,0,1,0},dj[]={0,1,0,-1};
    int map[maxn][maxn],n,m;
    bool mark[maxn][maxn]={0};
    struct node
    {
    int x,y;
    node(int x,int y):x(x),y(y){}
    void show()
    {
    cout<<x<<" "<<y<<endl;
    }
    };
    void Cant()
    {
    cout<<"No solution"<<endl;
    exit(0);
    }
    int main()
    {
    cin>>n>>m;    
    queue<node> Q;
    stack<node> S;
    for(int i = 0; i <= n+1;i++) for(int j = 0; j<=m+1;j++) map[i][j]=10;
    for(int i = 1; i<=n;i++)
    REP(int j =1; j<=m;j++)
    {
    cin>>map[i][j];    
    if(map[i][j]==1)
    Q.push(node(i,j));
    }
    while(Q.size())
    {
    node cur=Q.front();Q.pop();
    S.push(cur);
    mark[cur.x][cur.y]=true;        
    for(int k =0; k <=3;k++)
    {
    int t=di[k]+cur.x,u=dj[k]+cur.y;
    if(!mark[t][u]&&map[t][u]!=10)
    {
    map[t][u]–;
    if(!map[t][u])
    Cant();
    if(map[t][u]==1)
    Q.push(node(t,u));
    }
    }
    }
    if(S.size()!=n*m)
    Cant();
    while(S.size())
    {
    S.top().show();S.pop();
    }
    }
    

      

  • 相关阅读:
    ACdream 1224 Robbers (贪心)
    HDU 4320 Arcane Numbers 1 (质因子分解)
    在脚本中重定向输入
    呈现数据
    shell中的for、while、until(二)
    shell中的for、while、until
    C 指针疑虑
    结构化命令
    fdisk -c 0 350 1000 300命令
    PC机上的COM1口和COM2口
  • 原文地址:https://www.cnblogs.com/LyningCoder/p/3662955.html
Copyright © 2011-2022 走看看