暑假小训练 (一道 不能不会的用结构体搜索题)
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;
}
题意: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;
}
相关文章推荐
- CSU-ACM2016暑假集训训练1-二分搜索-C
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
- 暑假训练1-搜索专题
- 暑假集训--训练1 二分搜索
- 开始,搜索,不能启动之问题
- 不能搜索
- .“在JAVA中,异常子类必须在它们任何父类之前使用,因为运用父类的catch语句将捕获该类型及其所有子类类型的异常。这样,后面运行子类的catch语句将永远不会到达。而且,Java中不能到达的代码是一个错误。”
- 其实好多人根本不会用百度搜索
- 搜索与回溯 上机训练 4
- 北大暑假训练计划—POJ1013 Counterfeit Dollar(C语言)
- 一道C语言笔试题——理解结构体的内存对齐和位域问题
- 【暑假复习】【搜索】POJ3009:Curling 2.0
- swift UI专项训练37 实时显示搜索结果的搜索框SearchBar
- 2017暑假训练之树状数组
- 暑假训练4-数据结构专题
- c 判断两个结构体内容相等(不能通过内存判断)
- 下沙友好群暑假训练二
- 14年暑假前5日训练总结
- HDU 2037 今年暑假不AC (结构体排序示例题)
- VJ -链表训练--要记住啊!插入是:r->next=pa->next; pa->next=r;不能再错啦!!