CodeForces

在这里插入图片描述
题目链接

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        char u = sc.next().charAt(0);
        char[][] ch = new char[n+5][];
        for(int i=0;i<n;i++) ch[i] = sc.next().toCharArray();
        int x0 = -1;
        int y0 = -1;
        int x1 = -1;
        int y1 = -1;
        for(int i=0;i<n;i++){
            for(int j=0;j<ch[i].length;j++){
                if(x0==-1&&ch[i][j]==u){
                    x0 = i;
                    y0 = j;
                }
                if(ch[i][j]==u){
                    x1 = i;
                    y1 = j;
                }
            }
        }
        int res = 0;
        int x = x0-1;
        int y = y0;
        if(x>=0){
            if(ch[x][y]!='.') res++;
            while(y+1<=y1){
                if(ch[x][y+1]!=ch[x][y]&&ch[x][y+1]!='.') res++;
                y++;
            }
        }
        x = x1+1;
        y = y0;
        if(x<n){
            if(ch[x][y]!='.') res++;
            while(y+1<=y1){
                if(ch[x][y+1]!=ch[x][y]&&ch[x][y+1]!='.') res++;
                y++;
            }
        }
        x = x0;
        y = y0-1;
        if(y>=0){
            if(ch[x][y]!='.') res++;
            while (x+1<=x1){
                if(ch[x+1][y]!=ch[x][y]&&ch[x+1][y]!='.') res++;
                x++;
            }
        }
        x = x0;
        y = y1+1;
        if(y<ch[x].length){
            if(ch[x][y]!='.') {
                res++;
            }
            while (x+1<=x1){
                if(ch[x+1][y]!=ch[x][y]&&ch[x+1][y]!='.') res++;
                x++;
            }
        }
        System.out.println(res);
    }
}

原文地址:https://www.cnblogs.com/fxzemmm/p/14847906.html