csdn1780 优先队列BFS
2017-08-28 23:24
218 查看
题目链接:点击打开链接
第一次是求正常的BFS
第二次是每次要转弯的BFS
求第二个只要记住每个点最多是由相邻的个点走过来的就要好了,用vis2标记四个方向,每个方向最多走一次
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn=500+10;
const int xx[]={0,0,1,-1};
const int yy[]={1,-1,0,0};
int m[maxn][maxn],vis1[maxn][maxn],vis2[maxn][maxn][4];
int N,M,c1,c2,r1,r2;
struct node
{
int x,y,dis,dir;//dir表示方向
friend bool operator <(node a,node b)
{
return a.dis>b.dis;
}
};
int check(int x,int y)
{
if(x>0&&x<=N&&y>0&&y<=M&&m[x][y]!=-1)return 1;
return 0;
}
int bfs1()
{
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<node> q;
memset(vis1,0,sizeof(vis1));
node temp,next;
temp.x = r1 ;
temp.y = c1 ;
temp.dis = m[r1][c1];
q.push(temp);
vis1[r1][c1] = 1;
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.x == r2&&temp.y == c2)
return temp.dis;
for(int i = 0 ; i < 4; i ++)
{
int nx = temp.x + xx[i];
int ny = temp.y + yy[i];
next.x=nx,next.y=ny;
if(check(nx,ny)&&!vis1[nx][ny])
{
vis1[nx][ny] = 1;
next.dis = temp.dis +m[nx][ny];
q.push(next);
}
}
}
return -1;
}
int bfs2()
{
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<node> q;
memset(vis2,0,sizeof(vis2));
node temp,next;
temp.x = r1 ;
temp.y = c1 ;
temp.dis = m[r1][c1];
temp.dir=-1;
q.push(temp);
vis1[r1][c1] = 1;
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.x == r2&&temp.y == c2)
return temp.dis;
for(int i = 0 ; i < 4; i ++)
{
int nx = temp.x + xx[i];
int ny = temp.y + yy[i];
next.x=nx,next.y=ny;
if(check(nx,ny)&&!vis2[nx][ny][i]&&temp.dir!=i)
{
vis2[nx][ny][i] = 1;
next.dir=i;
next.dis = temp.dis +m[nx][ny];
q.push(next);
}
}
}
return -1;
}
int main()
{
int ans=0,i,j;
char s[100];
while(~scanf("%d %d %d %d %d %d",&N,&M,&r1,&c1,&r2,&c2))
{
memset(m,0x3f,sizeof(m));
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
{
scanf("%s",s);
if(s[0]=='*')m[i][j]=-1;
else m[i][j]=atoi(s);
}
printf("Case %d: %d %d\n",++ans,bfs1(),bfs2());
}
return 0;
}
第一次是求正常的BFS
第二次是每次要转弯的BFS
求第二个只要记住每个点最多是由相邻的个点走过来的就要好了,用vis2标记四个方向,每个方向最多走一次
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn=500+10;
const int xx[]={0,0,1,-1};
const int yy[]={1,-1,0,0};
int m[maxn][maxn],vis1[maxn][maxn],vis2[maxn][maxn][4];
int N,M,c1,c2,r1,r2;
struct node
{
int x,y,dis,dir;//dir表示方向
friend bool operator <(node a,node b)
{
return a.dis>b.dis;
}
};
int check(int x,int y)
{
if(x>0&&x<=N&&y>0&&y<=M&&m[x][y]!=-1)return 1;
return 0;
}
int bfs1()
{
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<node> q;
memset(vis1,0,sizeof(vis1));
node temp,next;
temp.x = r1 ;
temp.y = c1 ;
temp.dis = m[r1][c1];
q.push(temp);
vis1[r1][c1] = 1;
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.x == r2&&temp.y == c2)
return temp.dis;
for(int i = 0 ; i < 4; i ++)
{
int nx = temp.x + xx[i];
int ny = temp.y + yy[i];
next.x=nx,next.y=ny;
if(check(nx,ny)&&!vis1[nx][ny])
{
vis1[nx][ny] = 1;
next.dis = temp.dis +m[nx][ny];
q.push(next);
}
}
}
return -1;
}
int bfs2()
{
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<node> q;
memset(vis2,0,sizeof(vis2));
node temp,next;
temp.x = r1 ;
temp.y = c1 ;
temp.dis = m[r1][c1];
temp.dir=-1;
q.push(temp);
vis1[r1][c1] = 1;
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.x == r2&&temp.y == c2)
return temp.dis;
for(int i = 0 ; i < 4; i ++)
{
int nx = temp.x + xx[i];
int ny = temp.y + yy[i];
next.x=nx,next.y=ny;
if(check(nx,ny)&&!vis2[nx][ny][i]&&temp.dir!=i)
{
vis2[nx][ny][i] = 1;
next.dir=i;
next.dis = temp.dis +m[nx][ny];
q.push(next);
}
}
}
return -1;
}
int main()
{
int ans=0,i,j;
char s[100];
while(~scanf("%d %d %d %d %d %d",&N,&M,&r1,&c1,&r2,&c2))
{
memset(m,0x3f,sizeof(m));
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
{
scanf("%s",s);
if(s[0]=='*')m[i][j]=-1;
else m[i][j]=atoi(s);
}
printf("Case %d: %d %d\n",++ans,bfs1(),bfs2());
}
return 0;
}
相关文章推荐
- Poj 1724 ROADS (搜索 最短路 BFS优先队列)
- poj 2251 Dungeon Master(优先队列bfs)
- HDU1026 Ignatius and the Princess I(优先队列+BFS)
- HDU2425:Hiking Trip(简单bfs,优先队列实现)
- POJ 2312 Battle City 优先队列+BFS
- BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)
- POJ 3635 基于优先队列的BFS
- hdu 1242 Rescue(BFS优先队列)
- UESTC 482 Charitable Exchange(优先队列+bfs)
- HDU 1026 Ignatius and the Princess I 【BFS(优先队列)+路径存储】
- hdu1180——诡异的楼梯(优先队列+bfs)
- 【UESTC 482】Charitable Exchange(优先队列+bfs)
- POJ2312 Battle City(优先队列+广搜BFS)
- hdu 1242 Rescue (优先队列+bfs)
- CSU 1726: 你经历过绝望吗?两次! <BFS,优先队列求解>
- UESTC 482 Charitable Exchange(优先队列+bfs)
- HDU 1062(优先队列+BFS)
- nyoj 284坦克大战(优先队列+BFS)
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312 NYOJ284 【坦克大战】优先队列BFS