您的位置:首页 > 其它

暑假小训练 (一道 不能不会的用结构体搜索题)

2016-08-23 20:38 218 查看
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;

4000
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BFS