PKU 1657 Distance on Chessboard
问题:
http://poj.org/problem?id=1657
思路:
原本以为是搜索题,结果发现居然都可以推导出来(*^__^*) 嘻嘻……0MS
睡觉前AC个题,感觉蛮好
代码(写的比较繁琐):
http://poj.org/problem?id=1657
思路:
原本以为是搜索题,结果发现居然都可以推导出来(*^__^*) 嘻嘻……0MS
睡觉前AC个题,感觉蛮好
代码(写的比较繁琐):
1
#include
<
stdio.h
>
2 #include < stdlib.h >
3 #include < string .h >
4 #define Diff(a, b) ((a)>(b) ? ((a)-(b)) : ((b)-(a)))
5 #define Max(a, b) ((a)>(b) ? (a) : (b))
6 #define MAX_LEN 3
7 typedef enum {
8 Black,
9 White
10 }Color;
11
12 int
13 is_linear( char * src, char * dst)
14 {
15 if (src[ 0 ] == dst[ 0 ] || src[ 1 ] == dst[ 1 ])
16 return 1 ;
17 return 0 ;
18 }
19
20 int
21 is_oblique( char * src, char * dst)
22 {
23 int x_diff = Diff(src[ 0 ], dst[ 0 ]);
24 int y_diff = Diff(src[ 1 ], dst[ 1 ]);
25 if (x_diff == y_diff)
26 return 1 ;
27 return 0 ;
28 }
29
30 Color
31 black_or_white( char * src)
32 {
33 int x = src[ 0 ] - ' a ' + 1 ;
34 int y = src[ 1 ] - ' 0 ' ;
35 if (x % 2 == y % 2 )
36 return White;
37 return Black;
38 }
39
40 void
41 solve( char * src, char * dst)
42 {
43 int a, b, c, d, x_diff, y_diff;
44 x_diff = Diff(src[ 0 ], dst[ 0 ]);
45 y_diff = Diff(src[ 1 ], dst[ 1 ]);
46 a = Max(x_diff, y_diff); /* king */
47 if (is_linear(src, dst) || is_oblique(src, dst)) /* queen */
48 b = 1 ;
49 else
50 b = 2 ;
51
52 if (is_linear(src, dst)) /* rook */
53 c = 1 ;
54 else
55 c = 2 ;
56
57 if (is_oblique(src, dst)) /* bishop */
58 d = 1 ;
59 else if (black_or_white(src) != black_or_white(dst))
60 d = - 1 ;
61 else
62 d = 2 ;
63
64 printf( " %d %d %d " , a, b, c);
65 if (d == - 1 )
66 printf( " Inf\n " );
67 else
68 printf( " %d\n " , d);
69 }
70
71 int
72 main( int argc, char ** argv)
73 {
74 int tests;
75 char begin[MAX_LEN], end[MAX_LEN];
76 scanf( " %d " , & tests);
77 while (tests -- ) {
78 scanf( " %s %s " , begin, end);
79 if (begin[ 0 ] == end[ 0 ] && begin[ 1 ] == end[ 1 ])
80 printf( " 0 0 0 0\n " );
81 else
82 solve(begin, end);
83 }
84 }
2 #include < stdlib.h >
3 #include < string .h >
4 #define Diff(a, b) ((a)>(b) ? ((a)-(b)) : ((b)-(a)))
5 #define Max(a, b) ((a)>(b) ? (a) : (b))
6 #define MAX_LEN 3
7 typedef enum {
8 Black,
9 White
10 }Color;
11
12 int
13 is_linear( char * src, char * dst)
14 {
15 if (src[ 0 ] == dst[ 0 ] || src[ 1 ] == dst[ 1 ])
16 return 1 ;
17 return 0 ;
18 }
19
20 int
21 is_oblique( char * src, char * dst)
22 {
23 int x_diff = Diff(src[ 0 ], dst[ 0 ]);
24 int y_diff = Diff(src[ 1 ], dst[ 1 ]);
25 if (x_diff == y_diff)
26 return 1 ;
27 return 0 ;
28 }
29
30 Color
31 black_or_white( char * src)
32 {
33 int x = src[ 0 ] - ' a ' + 1 ;
34 int y = src[ 1 ] - ' 0 ' ;
35 if (x % 2 == y % 2 )
36 return White;
37 return Black;
38 }
39
40 void
41 solve( char * src, char * dst)
42 {
43 int a, b, c, d, x_diff, y_diff;
44 x_diff = Diff(src[ 0 ], dst[ 0 ]);
45 y_diff = Diff(src[ 1 ], dst[ 1 ]);
46 a = Max(x_diff, y_diff); /* king */
47 if (is_linear(src, dst) || is_oblique(src, dst)) /* queen */
48 b = 1 ;
49 else
50 b = 2 ;
51
52 if (is_linear(src, dst)) /* rook */
53 c = 1 ;
54 else
55 c = 2 ;
56
57 if (is_oblique(src, dst)) /* bishop */
58 d = 1 ;
59 else if (black_or_white(src) != black_or_white(dst))
60 d = - 1 ;
61 else
62 d = 2 ;
63
64 printf( " %d %d %d " , a, b, c);
65 if (d == - 1 )
66 printf( " Inf\n " );
67 else
68 printf( " %d\n " , d);
69 }
70
71 int
72 main( int argc, char ** argv)
73 {
74 int tests;
75 char begin[MAX_LEN], end[MAX_LEN];
76 scanf( " %d " , & tests);
77 while (tests -- ) {
78 scanf( " %s %s " , begin, end);
79 if (begin[ 0 ] == end[ 0 ] && begin[ 1 ] == end[ 1 ])
80 printf( " 0 0 0 0\n " );
81 else
82 solve(begin, end);
83 }
84 }