http://acm.zstu.edu.cn:8080/JudgeOnline/showproblem?problem_id=2585

(1)非常基本的类型。

(2)写时把e, s 弄混了, 而且条件:

if(s.x<=0||s.x>8||s.y<=0||s.y>8||map[s.x][s.y])

   写成了:

if(s.x<0||s.x>8||s.y<0||s.y>8||map[s.x][s.y])

具体代码:

View Code
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int m;
int map[10][10];
int px, py, tx, ty;
int dir[8][2]={1,2,2,1,-1,2,2,-1,1,-2,-2,1,-1,-2,-2,-1};
struct node
{
    int x, y, t;
}e, s;
int bfs()
{
    int i, j, k;
    map[px][py]=1;
    queue<node>q;
    while(!q.empty()) q.pop();
    e.x=px, e.y=py, e.t=0;
    q.push(e);
    while(!q.empty())
    {
        e=q.front();
        q.pop();
        if(e.x==tx&&e.y==ty) return e.t;
        for(i=0;i<8;i++)
        {
            s.x=e.x+dir[i][0];
            s.y=e.y+dir[i][1];
            s.t=e.t+1;
            if(s.x<=0||s.x>8||s.y<=0||s.y>8||map[s.x][s.y])
                continue;
            q.push(s);
            map[s.x][s.y]=1;
        }
    }
    return -1;
}
int main()
{
    int i, j, k, cas=0;
    char ch[10];
    while(scanf("%d", &m)!=EOF)
    {
        if(m==-1) break;
        memset(map, 0, sizeof(map));
        for(i=1;i<=m;i++)
        {
            scanf("%s", ch);
            map[ch[0]-'a'+1][ch[1]-'0']=1;
        }
        scanf("%s", ch);
        px=ch[0]-'a'+1, py=ch[1]-'0';
        scanf("%s", ch);
        tx=ch[0]-'a'+1, ty=ch[1]-'0';
        printf("Board %d: ", ++cas);
        int ans=bfs();
        if(ans==-1) printf("not reachable\n");
        else printf("%d moves\n", ans);
        
    }
    return 0;
}