zoukankan      html  css  js  c++  java
  • poj 2243 Knight Moves

    这题和poj 1915一样,用bfs做走马步。现在再看当时的代码,真是好幼稚啊。

     1 #include <stdio.h>
     2 #include <string.h>
     3 int X[] = {-2,-2,-1,-1,1,1,2,2},
     4     Y[] = {-1,1,-2,2,-2,2,-1,1};
     5 struct Point
     6 {
     7     int x,y;
     8     void init(int a,int b)
     9     {x = a; y = b;}
    10 }q[70],s,t;
    11 int head,tail,map[9][9];
    13 void bfs() 14 { 15 Point u,v; 16 int x1,y1,i; 17 head = tail = 0; 18 q[0] = s; 19 map[s.x][s.y] = 0; 20 if(s.x == t.x && s.y == t.y) 21 { 22 printf("To get from %c%d to %c%d takes 0 knight moves.\n" 23 ,s.x-1+'a',s.y,t.x-1+'a',t.y); 24 return ; 25 } 26 while(head <= tail) 27 { 28 u = q[head++]; 29 for(i = 0; i < 8; i++) 30 { 31 x1 = u.x + X[i]; 32 y1 = u.y + Y[i]; 33 if(x1 > 0 && x1 < 9 && y1 > 0 && y1 < 9 && map[x1][y1] == -1) 34 { 35 if(x1 == t.x && y1 == t.y) 36 { printf("To get from %c%d to %c%d takes %d knight moves.\n" 37 ,s.x-1+'a',s.y,t.x-1+'a',t.y,map[u.x][u.y]+1);return ;} 38 v.init(x1,y1); 39 q[++tail] = v; 40 map[x1][y1] = map[u.x][u.y] + 1; 41 } 42 } 43 } 44 } 45 int main() 46 { 47 char t1,t2; 48 while(~scanf("%c%d %c%d",&t1,&s.y,&t2,&t.y)) 49 { 50 getchar(); 51 s.x = t1 -'a'+1; 52 t.x = t2 -'a'+1; 53 memset(map,-1,sizeof map); 54 bfs(); 55 } 56 return 0; 57 }
  • 相关阅读:
    HDU
    稀疏表(ST / Sparse Table)
    HDU
    HDU
    树状数组
    用 BitArray 来编写埃拉托斯特尼筛法
    BitArray 内置是逆序存储, 因此要自行实现正序输出
    位运算,位移,窗体
    按位运算,窗体程序,And,Or,Xor
    基数排序
  • 原文地址:https://www.cnblogs.com/lzxskjo/p/2661108.html
Copyright © 2011-2022 走看看