站外题目求助:病毒扩散

题目总版

Albert_Peng @ 2024-11-24 17:17:54

题目描述

今天在地图上已经有几个地点爆发了疫情,请找到第二天会有哪些地方受到威胁。输入是 5 5 的表格,用来表示地图上的疫情。E 表示现在没有疫情,A 表示已经有疫情。第二天,所有已有疫情地点的上下左右位置都会发展出疫情。请输出 5 5 的新表格,用来表示第二天的疫情。

输入输出格式

输入格式

5 * 5 的字符阵

输出格式

5 * 5 的字符阵

输入输出样例

输入样例#1:

EEEEE
EEEEE
AEEEE
EEEAE
EEEEE

输出样例#1:

EEEEE
AEEEE
AAEAE
AEAAA
EEEAE

正文

我目前的代码是酱紫的:

# include <cstdio>
# include <algorithm>
using namespace std;
char virus [5] [5];
bool new_virus [5] [5];
int main () {
    fill_n(&new_virus [0] [0], 5 * 5, 1);
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {
            scanf(" %c", &virus [i] [j]);
        }
    }
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {  
            if (virus [i] [j] == 65 && new_virus [i] [j]) {
                new_virus [i] [j] = 0;
                if (i - 1 >= 0) {
                    virus [i - 1] [j] = 65;
                    new_virus [i - 1] [j] = 0;
                }
                if (i + 1 < 5) {
                    virus [i + 1] [j] = 65;
                    new_virus [i + 1] [j] = 0;
                }
                if (j - 1 >= 0) {
                    virus [i] [j - 1] = 65;
                    new_virus [i] [j - 1] = 0;
                }
                if (j + 1 < 5) {
                    virus [i] [j + 1] = 65;
                    new_virus [i] [j + 1] = 0;
                }
                if (i - 1 >= 0 && j - 1 >= 0) {
                    virus [i - 1] [j - 1] = 65;
                    new_virus [i - 1] [j - 1] = 0;
                }
                if (i - 1 >= 0 && j + 1 < 5) {
                    virus [i - 1] [j + 1] = 65;
                    new_virus [i - 1] [j + 1] = 0;
                }
                if (i + 1 < 5 && j - 1 >= 0) {
                    virus [i + 1] [j - 1] = 65;
                    new_virus [i + 1] [j - 1] = 0;
                }
                if (i + 1 < 5 && j + 1 < 5) {
                    virus [i + 1] [j + 1] = 65;
                    new_virus [i + 1] [j + 1] = 0;
                }
            }
        }
    }
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {
            printf ("%c", virus [i] [j]);
        }
        printf ("\n");
    }
    return 0;
}

大佬们,救救我吧


by jianping12_AC @ 2024-11-24 17:22:03

# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
char virus [5] [5];
bool new_virus [5] [5];
int main () {
    fill_n(&new_virus [0] [0], 5 * 5, 1);
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {
            scanf(" %c", &virus [i] [j]);
        }
    }
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {  
            if (virus [i] [j] == 65 && new_virus [i] [j]) {
                new_virus [i] [j] = 0;
                if (i - 1 >= 0) {
                    virus [i - 1] [j] = 30;
                   new_virus [i - 1] [j] = 0;
                }
                if (i + 1 < 5) {
                    virus [i + 1] [j] = 30;
                    new_virus [i + 1] [j] = 0;
                }
                if (j - 1 >= 0) {
                    virus [i] [j - 1] = 30;
                   new_virus [i] [j - 1] = 0;
                }
                if (j + 1 < 5) {
                    virus [i] [j + 1] = 30;
                    new_virus [i] [j + 1] = 0;
                }
            }
        }
    }
    for (int i = 0; i < 5; i ++) {
        for (int j = 0; j < 5; j ++) {
            if(new_virus[i][j]==0){
                cout<<"A";
            }
            else
            cout<<"E";
        }
        printf ("\n");
    }
    return 0;
}

调好了,~求关~


by Albert_Peng @ 2024-11-24 18:09:40

@jianping12_AC呃……


by Gavinzhou @ 2024-11-24 18:36:07

事实上,并不需要那么多条件判断,因为如果 'A' 在边(角)上,新增的会到a[0][x]或a[x][0]或a[0][0]上,输出不会输出那些的。 我的代码:

#include <iostream>
#include<algorithm>
#define int long long
using namespace std;
struct Bingdu{
    int x,y;
};
char a[6][6];
Bingdu bd[30];
int dx[]{-1,0,0,1};
int dy[]{0,1,-1,0};
void f(int x,int y){
    for(int i=0;i<4;i++){
        a[x+dx[i]][y+dy[i]]='A';
    }
}
signed main() {
    int c=0;
    for(int i=1;i<=5;i++){
        for(int j=1;j<=5;j++){
            cin>>a[i][j];
            if(a[i][j]=='A'){
                bd[++c].x=i,bd[c].y=j;
            }
        }
    }
    for(int i=1;i<=c;i++){
        f(bd[i].x,bd[i].y);
    }
    for(int i=1;i<=5;i++){
        for(int j=1;j<=5;j++){
            cout<<a[i][j];
        }
        cout<<endl;
    }
    return 0;
}

by cola0827 @ 2024-11-27 21:13:59

signed main() ??


|