Nightmare HDU1072

非常标准的BFS

第一次写错了很多

1、到达4时设置为墙就好了  避免了死循环

2、不用开d数组   在结构体里面就行了

3、结构体初始化函数的写法: Node(int x=0,int y=0,int oil=0):x(x),y(y),oil(oil){}

4、bfs的FOR里面的判断条件可以写的很清晰!就判断可以的  不可以的直接不处理!

#include<bits/stdc++.h>
using namespace std;
int sx,sy,ex,ey;
int d[][],a[][],d2[][];
bool f[][];
int n,m;
struct aa
{
int x;
int y;
int oil;
aa(int x=,int y=,int oil=):x(x),y(y),oil(oil){}
}; void bfs()
{ const int dr[]={-,,,};
const int dc[]={,,,-}; queue<aa>q;
memset(d,-,sizeof(d)); memset(f,true,sizeof(f));
aa u(sx,sy,);d[sx][sy]=;
q.push(u);
// printf("u:%d %d %d\n",u.x,u.y,u.oil);
while(!q.empty())
{ aa u=q.front();q.pop(); if(u.x==ex&&u.y==ey) {printf("%d\n",d[ex][ey]);return;}
for(int i=;i<=;i++)
{
aa v(u.x+dr[i],u.y+dc[i],u.oil-);
d[v.x][v.y]=d[u.x][u.y]+; if(v.x<||v.x>n||v.y<||v.y>m) continue ;
if(v.oil==)continue ;
if(a[v.x][v.y]==){ v.oil=; a[v.x][v.y]=; q.push(v); }
if(a[v.x][v.y]!=)
{
//printf("v:%d %d %d\n",v.x,v.y,v.oil);
q.push(v);} } } printf("-1\n");
} int main()
{
int cas;cin>>cas;
while(cas--)
{ cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==){sx=i;sy=j;}
if(a[i][j]==){ex=i;ey=j;}
}
bfs(); } return ;
}

第二次:简洁了许多

#include<bits/stdc++.h>
using namespace std; int world[][];int sx,sy,ex,ey;int n,m; struct node
{
int x,y,d,oil; node(int x=,int y=,int d=,int oil=):x(x),y(y),d(d),oil(oil){}
}; void bfs()
{
int dx[]={,,,-};
int dy[]={,,-,}; node u(sx,sy,,);
queue<node>q;
q.push(u); while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey){printf("%d\n",u.d);return;} for(int i=;i<;i++)
{
node v(u.x+dx[i],u.y+dy[i],u.d+,u.oil-);
if(v.x>=&&v.x<=n&&v.y>=&&v.y<=m&&v.oil)
{
if(world[v.x][v.y]==){v.oil=;world[v.x][v.y]=;q.push(v);} else if(world[v.x][v.y]>) q.push(v);
}
}
}
printf("-1\n");
} int main()
{
int cas;cin>>cas;
while(cas--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{scanf("%d",&world[i][j]);
if(world[i][j]==){sx=i;sy=j;}
if(world[i][j]==){ex=i;ey=j;} } bfs(); } return ;
}

大神的简洁代码:

#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
#include <stdio.h>
using namespace std;
#define Size 8
/*
这题目可以重复道路.只需要把控制炸弹的地方使用后,变成墙壁即可.
*/
struct Node
{
int x;
int y;
int time;//time代表已用的时间.
int rest;//rest代表剩余的时间.
//按照时间从高到低排列.
bool operator < (Node a) const
{
return this->time > a.time;
}
}; int world[Size][Size];
int n, m;
int temp;
int dir[][] = { { , }, { , }, { -, }, { , - } };
int sx, sy;
int rx, ry;
/*
0代表墙壁.1代表正常路.2代表起点.3代表终点.4代表炸弹控制器.
*/
int bfs()
{
priority_queue<Node> temp;
Node now, next, s;
s.x = sx;
s.y = sy;
s.time = ;
s.rest = ;
temp.push(s); while (!temp.empty())
{
now = temp.top();
temp.pop(); if (now.x == rx && now.y == ry && now.rest > )
{
return now.time;
}
//减枝.当剩余时间为1时.还没找到出口,说明到不了了.
if (now.rest == )
continue; for (int i = ; i < ; ++i)
{
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
next.time = now.time + ;
next.rest = now.rest - ;
//判断位置是否合理.
if (next.x >= && next.y >= && next.x < n && next.y < m && world[next.x][next.y] != && next.rest >= )
{
//如果他到了炸弹这里.
if (world[next.x][next.y] == )
{
next.rest = ;
//改为墙壁即可.
world[next.x][next.y] = ;
}
temp.push(next);
}
}
}
return -;
} int main()
{
int t;
scanf("%d", &t);
for (int i = ; i < t; ++i)
{
scanf("%d%d", &n,&m);
memset(world, , sizeof(world));
for (int j = ; j < n; ++j)
{
for (int k = ; k < m; ++k)
{
scanf("%d", &temp);
//初始位置.
if (temp == )
{
sx = j;
sy = k;
}
//目标位置.
else if (temp == )
{
rx = j;
ry = k;
}
world[j][k] = temp;
}
}
printf("%d\n", bfs());
} return ;
}
上一篇:sql复制数据表和表结构


下一篇:Entity Framework问题:ReferentialConstraint 中的依赖属性映射由存储生成的列