Shtirlits - SGU 125 dfs

本文介绍了一种解决特定棋盘矩阵问题的算法。通过输入矩阵B描述相邻状态之间的力量对比,算法输出矩阵A,表示每个状态的力量分布。详细阐述了从左至右、从上至下遍历矩阵的方法,确保每个状态的力量符合其邻居力量的条件。最后通过实例展示了算法的应用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a checkered field of size N x N cells (1 <= N <= 3). Each cell designates the territory of a state (i.e. N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1<=i<=N, 1<=j<=N, 0 <=  A[i, j] <=  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e. 0 <=  B[i, j]  <= 4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.

 

Input

The first line contains a natural number N. Following N lines contain the description of matrix B - N numbers in each line delimited by spaces.

 

Output

If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.

 

Sample Input

3
1 2 1
1 2 1
1 1 0

 

Sample Output

1 2 3
1 4 5
1 6 7

题目意思:求一个矩阵,给出原矩阵某数周围比他大的数的个数矩阵。

解题思路:从头开始从左往右从上往下枚举每一个元素,到第二行及以下时检查上一行对应元素是否满足条件,枚举到最后一个元素时,检查最后一行所有元素是否满足条件。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

#define maxn 5
int num[maxn][maxn];
int ans[maxn][maxn];
int n;
bool ok;
/////////////////////////
bool inside(int x,int y){
    return x>=0 && x<n && y>=0 && y<n;
}

bool check(int x, int y){
    int cnt = 0;
    if(inside(x-1,y) && ans[x-1][y] > ans[x][y]) cnt++; // up
    if(inside(x+1,y) && ans[x+1][y] > ans[x][y]) cnt++;  // down
    if(inside(x,y-1) && ans[x][y-1] > ans[x][y]) cnt++;  // left
    if(inside(x,y+1) && ans[x][y+1] > ans[x][y]) cnt++;  // right
    return cnt==num[x][y];
}

bool dfs(int x,int y){
    if(ok) return true;
    if(x==n && y==0){
        for(int i=0;i<n;i++){
            if(!check(n-1,i)) return false;
        }
        return ok = true;
    }
    int xx,yy;
    if(y==n-1) {
        xx = x+1;
        yy = 0;
    }else{
        xx = x;
        yy = y+1;
    }
    for(int i=0;i<10;i++){
        ans[x][y] = i;
        if(x>0) {
            if(check(x-1,y)) {
                dfs(xx,yy);
                if(ok) return true;
            }
        }
        else {
            dfs(xx,yy);
            if(ok) return true;
        }
    }
    return ok;
}
int main(){
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> num[i][j];
        }
    }
    ok = false;
    memset(ans,-1,sizeof(ans));
    if(!dfs(0,0)){
        cout << "NO SOLUTION" <<endl;
    }else{
        for(int i=0;i<n;i++){
            cout << ans[i][0];
            for(int j=1;j<n;j++){
                cout << " "<<ans[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}


内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值