fork(1) download
  1. import java.util.*;
  2.  
  3. class B_Chess {
  4. public static void main(String[] args) throws Exception {
  5. Scanner s = new Scanner(System.in);
  6. String str = s.next();
  7. int rr = str.charAt(0) - 'a';
  8. int rc = str.charAt(1) - '1';
  9. str = s.next();
  10. int nr = str.charAt(0) - 'a';
  11. int nc = str.charAt(1) - '1';
  12. boolean[][] b = new boolean[8][8];
  13. for (int i = 0; i < 8; ++i) {
  14. b[rr][i] = true;
  15. b[i][rc] = true;
  16. }
  17.  
  18. for(int[] pos : rook(rr, rc)){
  19. b[pos[0]][pos[1]]=true;
  20. }
  21. for(int[] pos : rook(nr, nc)){
  22. b[pos[0]][pos[1]]=true;
  23. }
  24. int count = 0;
  25. for (int i = 0; i < 8; ++i) {
  26. for (int j = 0; j < 8; ++j) {
  27. if(!b[i][j]){
  28. ++count;
  29. }
  30. }
  31. }
  32. System.out.println(count);
  33. }
  34.  
  35. static List<int[]> rook(int row, int col) {
  36. List<int[]> list = new ArrayList<int[]>();
  37. list.add(new int[]{row, col});
  38. if (row - 2 >= 0) {
  39. if (col + 1 < 8) {
  40. list.add(new int[] { row - 2, col + 1 });
  41. }
  42. if (col - 1 >= 0) {
  43. list.add(new int[] { row - 2, col - 1 });
  44. }
  45. }
  46. if (row - 1 >= 0) {
  47. if (col + 2 < 8) {
  48. list.add(new int[] { row - 1, col + 2 });
  49. }
  50. if (col - 2 >= 0) {
  51. list.add(new int[] { row - 1, col - 2 });
  52. }
  53. }
  54. if (row + 1 < 8) {
  55. if (col + 2 < 8) {
  56. list.add(new int[] { row + 1, col + 2 });
  57. }
  58. if (col - 2 >= 0) {
  59. list.add(new int[] { row + 1, col - 2 });
  60. }
  61. }
  62. if (row + 2 < 8) {
  63. if (col + 1 < 8) {
  64. list.add(new int[] { row + 2, col + 1 });
  65. }
  66. if (col - 1 >= 0) {
  67. list.add(new int[] { row + 2, col - 1 });
  68. }
  69. }
  70. return list;
  71. }
  72. }
  73.  
Success #stdin #stdout 0.06s 213760KB
stdin
a1
b2
stdout
44