1 条题解

  • 0
    @ 2023-6-11 12:19:05

    C :

    #include<stdio.h>
    int main()
    {
    	char a[100][100];
    	int n,m,i,j,s=0;
    	scanf("%d %d",&n,&m);
    	for(i=0;i<n;i++){
    			scanf("%s",&a[i]);
    	}
    	for(i=0;i<n;i++){
    		for(j=0;j<m;j++){ 
    			if(a[i][j]=='*') printf("*");
    			else {
    				if(i==0){
    					if(j==0){
    						if(a[i][j+1]=='*')s++;
    						if(a[i+1][j+1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    					}else if(j==m-1){
    						if(a[i][j-1]=='*')s++;
    						if(a[i+1][j-1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    					}else {
    						if(a[i][j+1]=='*')s++;
    						if(a[i+1][j+1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    						if(a[i][j-1]=='*')s++;
    						if(a[i+1][j-1]=='*')s++;
    					}
    				}
    				else if(i==n-1){
    					if(j==0){
    						if(a[i][j+1]=='*')s++;
    						if(a[i-1][j+1]=='*')s++;
    						if(a[i-1][j]=='*')s++;
    					}else if(j==m-1){
    						if(a[i][j-1]=='*')s++;
    						if(a[i-1][j-1]=='*')s++;
    						if(a[i-1][j]=='*')s++;
    					}else {
    						if(a[i][j+1]=='*')s++;
    						if(a[i-1][j+1]=='*')s++;
    						if(a[i-1][j]=='*')s++;
    						if(a[i][j-1]=='*')s++;
    						if(a[i-1][j-1]=='*')s++;
    					}
    				}else if(i!=0&&j==0&&i!=n-1){
    						if(a[i-1][j]=='*')s++;
    						if(a[i-1][j+1]=='*')s++;
    						if(a[i][j+1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    						if(a[i+1][j+1]=='*')s++;
    				}else if(i!=0&&j==m-1&&i!=n-1){
    						if(a[i-1][j]=='*')s++;
    						if(a[i-1][j-1]=='*')s++;
    						if(a[i][j-1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    						if(a[i+1][j-1]=='*')s++;
    				}else {
    						if(a[i-1][j]=='*')s++;
    						if(a[i-1][j-1]=='*')s++;
    						if(a[i-1][j+1]=='*')s++;
    						if(a[i][j-1]=='*')s++;
    						if(a[i][j+1]=='*')s++;
    						if(a[i+1][j]=='*')s++;
    						if(a[i+1][j-1]=='*')s++;
    						if(a[i+1][j+1]=='*')s++;
    				}
    				printf("%d",s);
    			}
    			s=0;
    		}
    		printf("\n"); 
    	}
    	return 0;
     } 
    

    C++ :

    #include <iostream>
    #include <cstdio>
    #define N 103
    void search(int i,int j);
    char a[N][N];
    int n,m;
    int main()  {
        scanf("%d %d",&n,&m);
        getchar();
        for (int i=0;i<n;i++)   {
            for (int j=0;j<m;j++)
                scanf("%c",&a[i][j]);
            getchar();
        }
        for (int i=0;i<n;i++)   {
            for (int j=0;j<m;j++)   {
                if (a[i][j]=='*')
                    printf("*"); 
                else
                    search(i,j);
                if (j+1==m) printf("\n");
            }
        }
        return 0;
    }
    void search(int i,int j)    {
        int flag=0;
        for (int di=-1;di<2;di++)
            for (int dj=-1;dj<2;dj++)
                {
                    int x=i+di;
                    int y=j+dj;
                    if ((di!=0||dj!=0)&&x>=0&&x<n&&y>=0&&y<m)
                        if (a[x][y]=='*')
                            flag++;
                }
        printf("%d",flag);
    }
    

    Java :

    
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            int m, n;
            Scanner scanner = new Scanner(System.in);
            m = scanner.nextInt();
            n = scanner.nextInt();
            String s = scanner.nextLine();
            String[] s1 =new String[m];
            char[][] c = new char[m][n];
            for(int i = 0 ; i < m ; i++)
            {
            	s1[i] = scanner.nextLine();
            	for(int j = 0 ; j < n ; j++)
            	{
            		 c[i][j] = s1[i].charAt(j);
            	}
            }
           
            int[][] a = new int[m][n];
            for(int i = 0 ; i < m ; i++)
                for(int j = 0 ; j < n ; j++)
                    a[i][j] = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (c[i][j] == '*') {
                        a[i][j] = -1;
                        continue;
                    }
                    if (i - 1 >= 0 && c[i - 1][j] == '*')
                        a[i][j] += 1;
                    if (i - 1 >= 0 && j - 1 >= 0 && c[i - 1][j - 1] == '*')
                        a[i][j] += 1;
                    if (i - 1 >= 0 && j + 1 < n && c[i - 1][j + 1] == '*')
                        a[i][j] += 1;
                    if (j - 1 >= 0 && c[i][j - 1] == '*')
                        a[i][j] += 1;
                    if (j + 1 < n && c[i][j + 1] == '*')
                        a[i][j] += 1;
                    if (j - 1 >= 0 && i + 1 < m && c[i + 1][j - 1] == '*')
                        a[i][j] += 1;
                    if (i + 1 < m && c[i + 1][j] == '*')
                        a[i][j] += 1;
                    if (i + 1 < m && j + 1 < n && c[i + 1][j + 1] == '*')
                        a[i][j] += 1;
     
                }
            }
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (a[i][j] != -1)
                        System.out.print(a[i][j]);
                    else {
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
     
        }
    }
    

    Python :

    sr=input().split()
    n=int(sr[0])
    m=int(sr[1])
    a=[["?" for i in range(m+2)] for j in range(n+2)]
    b=[[0 for i in range(m)] for j in range(n)]
    c=[[-1,-1,-1,0,0,1,1,1],[-1,0,1,-1,1,-1,0,1]]
    for i in range(n):
        sr=input()
        for j in range(m):
            a[i+1][j+1]=sr[j]
    for i in range(1,n+1):
        for j in range(1,m+1):
            if(a[i][j]=="*"):
                b[i-1][j-1]=a[i][j]
            else:
                js=0
                for k in range(8):
                    a1=i+c[0][k]
                    a2=j+c[1][k]
                    if(a[a1][a2]=="*"):
                        js=js+1
                b[i-1][j-1]=js
    for i in range(n):
        for j in range(m):
            print(b[i][j],end="")
        print()
    
    • 1

    信息

    ID
    2493
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者