hdu 1372

#include <stdio.h>
#include <iostream>
#include <queue>

int move[8][2] ={{1,2},{2,1},{-1,-2},{-2,-1},{-1,2},{-2,1},{2,-1},{1,-2} };
bool used[10][10];
using namespace std;

struct point {
 int x;int y;
 int step;
};

int  BFS(point src ,point res) {

queue<point> my;
 my.push(src);
 while( !my.empty() ) {
  point temp = my.front();
  point t ;
  if(temp.x == res.x && temp.y == res.y) return temp.step;
  for(int i = 0 ;i < 8; i++ ) {
   t.x = temp.x + move[i][0];
   t.y = temp.y + move[i][1];
   if(t.x>0&&t.x<9&&t.y>0&&t.y<9 && 0 == used[t.x][t.y]) {
    t.step = temp.step+1;
    used[t.x][t.y] = 1;
    my.push(t);
   }
  }
  my.pop();

}
 return -1;
}
int main() {

char a,c; int b,d;
 while(cin>>a>>b>>c>>d) {
  memset(used,0,sizeof(used));
  a-=96;  c-=96;
  point src,res;
  src.x = a; src.y = b;
  used[a][b] = 1;
  src.step = 0;
  res.x = c; res.y = d;
  printf("To get from %c%d to %c%d takes %d knight moves.\n",a+96,b,c+96,d,BFS(src ,res));
 }
 return 0;
}

#include <stdio.h>
#include <iostream>
#include <queue>

int move[8][2] ={{1,2},{2,1},{-1,-2},{-2,-1},{-1,2},{-2,1},{2,-1},{1,-2} };
bool used[10][10];
using namespace std;

struct point {
 int x;int y;
 int step;
};

int  BFS(point src ,point res) {

queue<point> my;
 my.push(src);  //
 while( !my.empty() ) {
  point cur = my.front();
  point t ;
  if(cur.x == res.x && cur.y == res.y) return cur.step;
  for(int i = 0 ;i < 8; i++ ) {
   t.x = cur.x + move[i][0];
   t.y = cur.y + move[i][1];
   if(t.x>0&&t.x<9&&t.y>0&&t.y<9 && 0 == used[t.x][t.y]) {
    t.step = cur.step+1;
    used[t.x][t.y] = 1;
    my.push(t);
   }
  }
  my.pop();

}
 return -1;
}
int main() {

char a,c; int b,d;
 while(cin>>a>>b>>c>>d) {
  memset(used,0,sizeof(used));
  a-=96;  c-=96;
  point src,res;
  src.x = a; src.y = b;
  used[a][b] = 1;
  src.step = 0;
  res.x = c; res.y = d;
  printf("To get from %c%d to %c%d takes %d knight moves.\n",a+96,b,c+96,d,BFS(src ,res));
 }
 return 0;
}

#include <stdio.h>
#include <iostream>
#include <queue>

int move[8][2] ={{1,2},{2,1},{-1,-2},{-2,-1},{-1,2},{-2,1},{2,-1},{1,-2} };
bool used[10][10];
using namespace std;

struct point {
    int x;int y;
    int step;
};

int  BFS(point src ,point res) {

    queue<point> my;
    my.push(src);
    while( !my.empty() ) {
        point temp = my.front();
        point t ;
        if(temp.x == res.x && temp.y == res.y) return temp.step;
        for(int i = 0 ;i < 8; i++ ) {
            t.x = temp.x + move[i][0];
            t.y = temp.y + move[i][1];
            if(t.x>0&&t.x<9&&t.y>0&&t.y<9 && 0 == used[t.x][t.y]) {
                t.step = temp.step+1;
                used[t.x][t.y] = 1;
                my.push(t);
            }
        }
        my.pop();

    }
    return -1;
}
int main() {

    char a,c; int b,d;
    while(cin>>a>>b>>c>>d) {
        memset(used,0,sizeof(used));
        a-=96;  c-=96;
        point src,res;
        src.x = a; src.y = b;
        used[a][b] = 1;
        src.step = 0;
        res.x = c; res.y = d;
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a+96,b,c+96,d,BFS(src ,res));
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int c[9][9];
int dir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
typedef struct
{
    int x,y,count;
}node;
node start,finish;
int bfs()
{
    memset(c,0,sizeof(c));
    node pre,cur;
    start.count = 0;
    queue<node> q;
    q.push(start);
    c[start.x][start.y] = 1;

    while(!q.empty())
    {
        pre = q.front();
        q.pop();
        if(pre.x == finish.x&&pre.y == finish.y)
        return pre.count;
        for(int i = 0; i < 8; i++)
        {
            cur.x = pre.x + dir[i][0];
            cur.y = pre.y + dir[i][1];
            if(cur.x<1||cur.x>8||cur.y<1||cur.y>8)continue;
            if(c[cur.x][cur.y]==1)continue;
            c[cur.x][cur.y] = 1;
            cur.count = pre.count + 1;
            q.push(cur);
        }
    }
    return -1;
}
int main()
{
    char row,end;
    int col,ed;
    int min;
    while(scanf("%c",&row)!=EOF)
    {
        scanf("%d",&col);
        getchar();
        scanf("%c%d",&end,&ed);
        getchar();
        start.x = row-‘a‘+1;
        start.y = col;
        finish.x = end-‘a‘+1;
        finish.y = ed;
        if(start.x==finish.x&&start.y==finish.y)
        min = 0;
        else  min = bfs();
        printf("To get from %c%d to %c%d takes %d knight moves.\n",row,col,end,ed,min);
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int c[9][9];
int dir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
typedef struct
{
    int x,y,count;
}node;
node start,finish;
int bfs()
{
    memset(c,0,sizeof(c));
    node pre,cur;
    start.count = 0;
    queue<node> q;
    q.push(start);
    c[start.x][start.y] = 1;
   
    while(!q.empty())
    {
        pre = q.front();
        q.pop();
        if(pre.x == finish.x&&pre.y == finish.y)
        return pre.count;
        for(int i = 0; i < 8; i++)
        {
            cur.x = pre.x + dir[i][0];
            cur.y = pre.y + dir[i][1];
            if(cur.x<1||cur.x>8||cur.y<1||cur.y>8)continue;
            if(c[cur.x][cur.y]==1)continue;
            c[cur.x][cur.y] = 1;
            cur.count = pre.count + 1;
            q.push(cur);
        }
    }
    return -1;
}
int main()
{
    char row,end;
    int col,ed;
    int min;
    while(scanf("%c",&row)!=EOF)
    {
        scanf("%d",&col);
        getchar();
        scanf("%c%d",&end,&ed);
        getchar();
        start.x = row-‘a‘+1;
        start.y = col;
        finish.x = end-‘a‘+1;
        finish.y = ed;
        if(start.x==finish.x&&start.y==finish.y)
        min = 0;
        else  min = bfs();
        printf("To get from %c%d to %c%d takes %d knight moves.\n",row,col,end,ed,min);
    }
    return 0;
}

hdu 1372

时间: 2024-11-07 18:35:38

hdu 1372的相关文章

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visit

HDU 1372 (搜索方向稍有改变) Knight Moves

其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标记为已走过. 因为测试数据里面有一组 f6 f6,此时样例输出的是0 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法. 输入: 输入包含一个或多个输入样例.每个测试样例将会有两个坐标,表示现在的位置和将要到达的地

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin

HDU 1372 Knight Moves(bfs)

嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向,然后从起点到终点跑一遍最典型的bfs即可...注意HDU的坑爹输入和输出... AC代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<cstring> 5 6 usi

HDU 1372.Knight Moves

Knight Moves Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1372 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour

hdu 1372 骑士从起点走到终点的步数

给出起点和终点 求骑士从起点走到终点所需要的步数 Sample Inpute2 e4 //起点 终点a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6 Sample OutputTo get from e2 to e4 takes 2 knight moves.To get from a1 to b2 takes 4 knight moves.To get from b2 to c3 takes 2 knight moves.To get from a1 to h8 take

hdu 1372 AND poj 2243 bfs

骑士巡游找最短路. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 8; 7 const int M = N * N; 8 int step[N][N]; 9 int dir[N][2] = { 1, 2, 1, -2, -1, 2, -1, -2, 2, 1, 2, -1, -2, 1, -2, -1 }; 1

HDU 1372 Knight Moves【BFS】

题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次[email protected][email protected]) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue&