Asteroids! hdu 1240 终于AC

/*
很水的bfs,一开始bfs不会,用dfs错了好多次,应该是算法有问题吧,我觉得这个题目用dfs
应该可以的
*/
#include<iostream>//2389356 2010-04-27 11:23:31 Accepted 1240 0MS 308K 1495 B C++ 悔惜晟
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;

char map[12][12][12];
bool hash[12][12][12];
bool flag;
int dir[6][3] = {{0, 0, 1}, {0, 1, 0}, {0, 0, -1}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0} };
int a, b, c;
int d, e, f;
int t;
int n;
struct node
{
 int x;
 int y;
 int z;
 int num;
};
queue<node> q;

int bfs(int si, int sj, int sk)
{
 node N, P;
 N.x = si;
 N.y = sj;
 N.z = sk;
 N.num = 0;
 int i;
 q.push(N);
 hash[si][sj][sk] = false;
 while(!q.empty())
 {
  N = q.front();
  if(N.x == f && N.y == e && N.z == d && N.num == t)
  {
   flag = true;
   cout<<n<<" "<<N.num<<endl;
   break;
  }
  if(N.num > t)
  {
   break;
  }
  q.pop();
  for(i = 0; i < 6; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.z = N.z + dir[i][2];
   
   if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < n && P.z >= 0 && P.z < n && hash[P.x][P.y][P.z] && map[P.x][P.y][P.z] == 'O')
   {
    P.num = N.num + 1;
    q.push(P);
    hash[P.x][P.y][P.z] = false;
   }
  }
   
 }
 return 1;

}
int main()
{
 char str[12];
 int i, j ,k;
 while(cin>>str>>n)
 {
  for(i = 0; i < n; i++)
  for(j = 0; j < n; j++)
  for(k = 0; k < n; k++)
   cin>>map[i][j][k];
  cin>>a>>b>>c;
  cin>>d>>e>>f;
  cin>>str;
  t = (int)(fabs(1.0*a - d) + fabs(1.0*b - e) + fabs(1.0*c - f)); //省赛的时候这样的CE过
  flag = false;
  memset(hash, true, sizeof(hash));//一开始这里标记错了,以后应该规定标记的,不然写写就会错了
  bfs(c, b ,a);
  if(!flag)
   cout<<"NO ROUTE"<<endl;
 }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值