http://codeforces.com/problemset/problem/6/B import java.util.*; public class CF6B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); char p = in.next().charAt(0); HashSet<Character> ans = new HashSet<Character>(); int[] di = {0,0,1,-1}; int[] dj = {1,-1,0,0}; char[][] map = new char[n][m]; for(int i = 0; i < n; i ++) map[i] = in.next().toCharArray(); for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { if(map[i][j]!=p) continue; for(int k = 0; k < 4; k ++) { int ni = i + di[k]; int nj = j + dj[k]; if(ni>-1&&ni<n&&nj>-1&&nj<m&&map[ni][nj]!=p&&map[ni][nj]!='.') ans.add(map[ni][nj]); } } } System.out.println(ans.size()); } }
文章来源: https://blog.csdn.net/qq_39370495/article/details/89785809