首页 > 代码库 > URAL 1197 Lonesome Knight

URAL 1197 Lonesome Knight

BFS中常见的判合法性……

 1 import java.util.Scanner; 2  3 public class P1197 4 { 5     private static int dx[] = new int[] { 1, 2, 2, 1, -1, -2, -2, -1 }; 6     private static int dy[] = new int[] { 2, 1, -1, -2, -2, -1, 1, 2 }; 7  8     private static boolean check(int x, int y) 9     {10         return (x >= 1 && x <= 8 && y >= 1 && y <= 8);11     }12 13     public static void main(String args[])14     {15         try (Scanner cin = new Scanner(System.in))16         {17             while (cin.hasNext())18             {19                 int n = cin.nextInt();20                 for (int i = 0; i < n; i++)21                 {22                     String location = cin.next();23                     int x = location.charAt(0) - ‘a‘ + 1;24                     int y = location.charAt(1) - ‘1‘ + 1;25                     int result = 0;26                     for (int dir = 0; dir < 8; dir++)27                         if (check(x + dx[dir], y + dy[dir]))28                             result++;29                     System.out.println(result);30                 }31             }32         }33     }34 }

 

URAL 1197 Lonesome Knight