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);
}
}
CodeForces - 6B
最新推荐文章于 2023-07-19 19:12:36 发布
本文介绍了一个Java程序,用于在一个二维字符数组中查找特定字符并计算其出现的路径。通过两个变量跟踪字符的位置,代码遍历了矩阵,计算了不重复路径上的字符数量。适合初学者理解字符串操作和数组遍历技巧。

1850

被折叠的 条评论
为什么被折叠?



