POJ 2243 bfs

题意:求棋子从起点到终点的最小步数。

分析:bfs。

注意:用while(scanf("%s%s",str1,str2)!=EOF)会tle。这边2次tle后来改成while(cin>>str1>>str2)就过了。

收获:bfs实在是弱,这题几乎是按照自己思路,一点一点敲出来的,不错。继续加油。(这题以前做过,当时是用数组模拟队列,是照别人做的。)

View Code
// I'm the Topcoder
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;

//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

//**************************CONSTANT***********************
#define INF 0x3f3f3f3f
#define eps 1e-8
#define PI acos(-1.)
#define PI2 asin (1.);
typedef long long LL;
//typedef __int64 LL;   //codeforces
typedef unsigned int ui;
typedef unsigned long long ui64;
#define MP make_pair
typedef vector<int> VI;
typedef pair<int, int> PII;
#define pb push_back
#define mp make_pair

//***************************SENTENCE************************
#define CL(a,b) memset (a, b, sizeof (a))
#define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b))
#define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c))

//****************************FUNCTION************************
template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); }
template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; }
template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; }

// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end

const int maxn = 110;
char str1[3];
char str2[3];
//int map[10][10];

struct node{
    int x;
    int y;
    //int time;
};
queue<node> Q;

int sx,sy;
int dir[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,-2},{2,-1},{2,1},{1,2}};
//int dir[8][2]={{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1}};
int dis[maxn][maxn];
int vis[maxn][maxn];
void bfs(node s){
    Q.push(s);
   // dis[s.x][s.y]=0;
    //vis[s.x][s.y]=1;
   // node hd;
    while(!Q.empty()){
        //printf("&&&&\n");
        node temp=Q.front();
        Q.pop();
        int x=temp.x;
        int y=temp.y;
        vis[x][y]=1;
//        if(x==sx&&y==sy)//边界
//        {
//            printf("%d\n",dis[sx][sy]);
//            //printf("sx=%d  sy=%d\n",sx,sy);
//            return;
//        }
        for(int i=0;i<8;i++){
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(xx>=0&&xx<8&&yy>=0&&yy<8&&!vis[xx][yy]){
                dis[xx][yy]=dis[x][y]+1;
                temp.x=xx;
                temp.y=yy;
                Q.push(temp);
                vis[xx][yy]=1;
            }
        }
    }
    //if()

    printf("To get from %s to %s takes %d knight moves.\n",str1,str2,dis[sx][sy]);
       // printf("%d\n",dis[sx][sy]);
}

int main(){
    while(cin>>str1>>str2){
        //node edge;
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        int startx=str1[0]-'a';//开始起点
        int starty=str1[1]-'1';
       // printf("startx=%d starty=%d\n",startx,starty);
        sx=str2[0]-'a';//终点
        sy=str2[1]-'1';
        //printf("sx=%d sy=%d\n",sx,sy);
        dis[startx][starty]=0;
        node s;
        vis[startx][starty]=1;
        s.x=startx;
        s.y=starty;
        //s.time=0;
        if(startx==sx&&starty==sy){
            printf("To get from %s to %s takes 0 knight moves.\n",str1,str2);
            continue;
        }
       else  bfs(s);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值