• 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);
        }
    }
    
    
  • 相关阅读:
    switch多分枝语句
    自动扫描判断数组奇偶 直接在控制台输入
    if else语句
    1231作业
    字符集和字符编码
    12.28作业
    置顶广告
    图片库
    js实现隔行换色及其鼠标滑过变色
    【转载】display:inline-block兼容ie6/7的写法
  • 原文地址:https://www.cnblogs.com/fxzemmm/p/14847906.html
Copyright © 2020-2023  润新知