第六届CCF软件能力认证B题-消除类游戏 题型:模拟

55 阅读1分钟

3223. 消除类游戏 - AcWing题库

#include<bits/stdc++.h>
using namespace std;
const int N=35;
int a[N][N];
bool st[N][N];
int main() {
    int n,m;cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}

    for(int i=1;i<n;i++)
    {
        for(int j=1;j<m;j++)
        {
            if(a[i][j] == a[i][j-1] && a[i][j] == a[i][j+1]) st[i][j] = st[i][j-1] = st[i][j+1] = true;
            if(a[j][i] == a[j-1][i] && a[j][i] == a[j+1][i]) st[j][i] = st[j-1][i] = st[j+1][i] = true; 
	    }
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!st[i][j]) cout<<a[i][j]<<" ";
            else cout<<0<<" ";
        }
        cout<<endl;
    }

	return 0;
}

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=35;
int a[N][N];
bool st[N][N];
int main() {
    int n,m;cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}


    for(int i=0;i<n;i++)
    {
        for(int j=1;j<m-1;j++)
        {
            if(a[i][j] == a[i][j-1] && a[i][j] == a[i][j+1]) st[i][j] = st[i][j-1] = st[i][j+1] = true;
        }
    }

    for(int i=0;i<m;i++)
    {
        for(int j=1;j<n-1;j++)
        {
            if(a[j][i] == a[j-1][i] && a[j][i] == a[j+1][i]) st[j][i] = st[j-1][i] = st[j+1][i] = true;
        }
    }



    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!st[i][j]) cout<<a[i][j]<<" ";
            else cout<<0<<" ";
        }
        cout<<endl;
    }

	return 0;
}

image.png