http://acm.hust.edu.cn/vjudge/contest/129524#problem/I
题意:8x8的象棋,给出开始和结束的点,和一个不能走的点,求出最短路。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int ax,ay,bx,by,cx,cy;
int gragh[10][10];
int path[10][10];
class Map //用结构体可以方便的判断x,y
{
public:
int x,y;
}now;
void bfs()
{
now.x = ax;
now.y = ay;
queue<Map>q;
q.push(now);
while(!q.empty()){
Map a = q.front();
q.pop();
if(a.x == bx && a.y == by)
return ;
for(int i = -1;i <= 1; i++)
for(int j = -1;j <= 1; j++)
if(i != 0 || j != 0){
if(a.x+i <= 8 && a.x+i >= 1 && a.y+j <= 8 && a.y+j >= 1 && gragh[a.x+i][a.y+j] != -1 && path[a.x+i][a.y+j] == 0)
{ //分别判断了:界,不是c点,没走过
Map temp;
temp.x = a.x+i;
temp.y = a.y+j;
path[temp.x][temp.y] = path[a.x][a.y]+1;
q.push(temp);
}
}
}
}
int main()
{
int ncase = 1;
// freopen("in.txt","r",stdin);
while(scanf("%d%d%d%d%d%d",&ax,&ay,&bx,&by,&cx,&cy) != EOF){
memset(path,0,sizeof(path));
memset(gragh,0,sizeof(gragh)); //记得初始化
gragh[cx][cy] = -1;
bfs();
printf("Case %d: %d\n",ncase++,path[bx][by]);
}
return 0;
}