码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1197 Lonesome Knight

时间:2014-08-26 08:31:25      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   for   ar   div   log   

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

标签:style   blog   color   java   io   for   ar   div   log   

原文地址:http://www.cnblogs.com/gwhahaha/p/3936353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!