superman

聚精会神搞建设 一心一意谋发展
posts - 190, comments - 17, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

POJ 2488 - A Knight's Journey

Posted on 2008-06-15 18:44 superman 阅读(285) 评论(0)  编辑 收藏 引用 所属分类: POJ
 1 /* Accepted 280K 16MS G++ 1675B */
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int n, m, cnt, N;
 7 bool visited[26][26], found;
 8 
 9 
10 struct { int x, y; } dir[8= {
11     {-2-1}, {-2+1}, {-1-2}, {-1+2},
12     {+1-2}, {+1+2}, {+2-1}, {+2+1}
13 };
14 
15 struct {int x, y; } path[26];
16 
17 inline bool inside(const int x, const int y)
18 {
19     if(x >= 0 && x < n && y >= 0 && y < m)
20         return true;
21     return false;
22 }
23 
24 void search(int x, int y)
25 {
26     if(found)
27         return;
28     
29     visited[x][y] = true;
30     path[cnt].x = x, path[cnt].y = y;
31     
32     if(cnt == n * m - 1)
33     {
34         found = true;
35         return;
36     }
37     
38     for(int i = 0; i < 8; i++)
39     {
40         int xx = x + dir[i].x;
41         int yy = y + dir[i].y;
42         if(inside(xx, yy) && visited[xx][yy] == false)
43         {
44             cnt++;
45             search(xx, yy);
46             cnt--;
47         }
48     }
49     
50     visited[x][y] = false;
51 }
52 
53 int main()
54 {
55     scanf("%d"&N);
56     for(int i = 1; i <= N; i++)
57     {
58         found = false;
59         memset(visited, falsesizeof(visited));
60         
61         scanf("%d %d"&m, &n);
62         
63         printf("Scenario #%d:\n", i);
64         
65         search(00);
66         
67         if(found)
68             for(int i = 0; i < n * m; i++)
69                 printf("%c%d", path[i].x + 'A', path[i].y + 1);
70         else
71             printf("impossible");
72         
73         printf("\n");
74         
75         if(i < N)
76             printf("\n");
77     }
78     
79     return 0;
80 }
81 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理