A计划 hdu 2102 终于AC,why?? bfs

/*
 这是看到这个题目第一次写的代码,但是 标记为@的地方没有考虑,一直测试数据都过不了,怎么回事?
 为何要犯那么纠结的错误呢??还有要考虑两层都是#的情况的,其实一看题目我就想到的,我以为不要考虑的
 */
#include<iostream> //2399013 2010-04-29 16:50:05 Accepted 2102 0MS 296K 2418 B C++ 悔惜晟
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

char map1[12][12];
char map2[12][12];
bool hash1[12][12];
bool hash2[12][12];
int ei, ej, ek;
int dir[4][2] = { {1, 0}, {-1, 0}, {0 ,-1}, {0 , 1} };
int t;
bool flag;
int n, m;

struct node
{
 int x;
 int y;
 int z;
 int time;
};
void bfs()
{
 node P, N;
 int i;
 N.x = 1;
 N.y = 1;
 N.z = 1;
 N.time = 0;
 hash1[1][1] = true;
 queue<node>q;
 q.push(N);
 while(!q.empty())
 {
  N = q.front();
  if(N.x == ei && N.y == ej && N.z == ek && N.time <= t)
  {
   flag = true;
   break;
  }
 
  /*
  if(N.time > t)
  {
   break;
  }
  */
  q.pop();
  for(i = 0; i < 4; i++)
  {
   P.y = N.y + dir[i][0];
   P.z = N.z + dir[i][1];
   if(P.y > n || P.y < 1 || P.z > m || P.z < 1)
    continue;
   if(N.x == 1 )
   {
    if( (map1[P.y][P.z] == '.'  || map1[P.y][P.z] == 'P')&& !hash1[P.y][P.z] )// @  map1[P.y][P.z] == 'P'这里忘记了考虑,一直处在纠结中
    {
     P.time = N.time + 1;
     P.x = N.x;
     q.push(P);
     hash1[P.y][P.z] = true;

    }
    if( map1[P.y][P.z] == '#' && !hash1[P.y][P.z] && map2[P.y][P.z] != '*' && !hash2[P.y][P.z] && map2[P.y][P.z] != '#')
    {
     P.time = N.time + 1;//还有这里的时间还是要加的, 到#的还是需要时间的,只是时空传输不要时间
     P.x = N.x + 1;
     q.push(P);
     hash1[P.y][P.z] = true;
     hash2[P.y][P.z] = true;

    }
    

   }
   else if(N.x == 2)
   {
    if( (map2[P.y][P.z] == '.' || map2[P.y][P.z] == 'P')&& !hash2[P.y][P.z]) //同上
    {
     P.time = N.time + 1;
     P.x = N.x;
     q.push(P);
     hash2[P.y][P.z] = true;

    }
    if( map2[P.y][P.z] == '#' && !hash2[P.y][P.z] && map1[P.y][P.z] != '*' && !hash1[P.y][P.z] && map1[P.y][P.z] != '#')
    {
     P.time = N.time + 1;
     P.x = N.x - 1;
     q.push(P);
     hash2[P.y][P.z] = true;
     hash1[P.y][P.z] = true;

    }
    

   }

  }
 }

}
int main()
{
 int c;
 int j, k;
 cin>>c;
 while(c--)
 {
  cin>>n>>m>>t;
  for(j = 1; j <= n; j++)
  for(k = 1; k <= m; k++)
  {
   cin>>map1[j][k];
   if(map1[j][k] == 'P')
   {
    ei = 1;
    ej = j;
    ek = k;
   }

  }
  for(j = 1; j <= n; j++)
  for(k = 1; k <= m; k++)
  {
   cin>>map2[j][k];
   if(map2[j][k] == 'P')
   {
    ei = 2;
    ej = j;
    ek = k;
   }

  }

  memset(hash1, false, sizeof(hash1));
  memset(hash2, false, sizeof(hash2));
  flag = false;
  bfs();
  if(flag)
   printf("YES/n");
  else
   printf("NO/n");
  

 }
}
/*
 因为找不错误就baidu了,可是致命的错误还是没有发现,做到晚上00:30,还是没有找出错误,
 因为数据比较大调试起来很困难
 */
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

char map[2][12][12];
bool hash[2][12][12];
int ei, ej, ek;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1} };
bool flag;
int n, m, t;

struct node
{
 int x;
 int y;
 int z;
 int time;
};
int  bfs()
{
 node P, N;
 int i;
 N.x = 0;
 N.y = 0;
 N.z = 0;
 N.time = 0;
 hash[0][0][0] = true;
 queue<node>q;
 q.push(N);
 while(!q.empty())
 {
  N = q.front();
  if(N.x == ei && N.y == ej && N.z == ek && N.time <= t)
  {
   flag = true;
   return 1;
   //break;
  }
  /*
  if(N.time > t)
  {
   break;
  }
  */
  q.pop();
  for(i = 0; i < 4; i++)
  {
   P.x = N.x;
   P.y = N.y + dir[i][0];
   P.z = N.z + dir[i][1];
   /*
   if(P.y >= n || P.y < 0 || P.z >= m || P.z < 0 ||  map[P.x][P.y][P.z] == '*' || N.time + 1 > t || hash[P.x][P.y][P.z])
    continue;
    */
   if(P.y < n && P.y >= 0 && P.z < m && P.z >= 0 &&  map[P.x][P.y][P.z] != '*' && N.time + 1 <= t && !hash[P.x][P.y][P.z])
   {
    if( map[P.x][P.y][P.z] == '.'  || map[P.x][P.y][P.z] == 'P')
    {
     P.time = N.time + 1;
     q.push(P);
     hash[P.x][P.y][P.z] = true;

    }
           if( map[P.x][P.y][P.z] == '#' && map[!(P.x)][P.y][P.z] != '*' && map[!(P.x)][P.y][P.z] != '#' && !hash[!(P.x)][P.y][P.z])
    {
    
     P.x = !(N.x);
     P.time = N.time + 1;
     q.push(P);
     hash[P.x][P.y][P.z] = true;
     hash[!(P.x)][P.y][P.z] = true;

    }
   }
  }
    

   
 }
 return 1;

}
int main()
{
 int c;
 int i, j, k;
 cin>>c;
 while(c--)
 {
  cin>>n>>m>>t;
  for(i = 0; i < 2; i++)
  for(j = 0; j < n; j++)
  for(k = 0; k < m; k++)
  {
   cin>>map[i][j][k];
   if(map[i][j][k] == 'P')
   {
    ei = i;
    ej = j;
    ek = k;
   }

  }
  /*
  for(i = 0; i < 2; i++)
  for(j = 0; j < n; j++)
  {
   scanf("%s", map[i][j]);
   for(k = 0; k < m; k++)
   {
    //cin>>map[i][j][k];
    if(map[i][j][k] == 'P')
    {
     ei = i;
     ej = j;
     ek = k;
    }
   }

  }
  */
  memset(hash, false, sizeof(hash));
  flag = false;
  bfs();
  if(flag)
   printf("YES/n");
  else
   printf("NO/n");
 }
}

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

char map[2][12][12];
bool hash[2][12][12];
int ei, ej, ek;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1} };
bool flag;
int n, m, t;

struct node
{
 int x;
 int y;
 int z;
 int time;
};
int  bfs()
{
 node P, N;
 int i;
 N.x = 0;
 N.y = 0;
 N.z = 0;
 N.time = 0;
 hash[0][0][0] = true;
 queue<node>q;
 q.push(N);
 while(!q.empty())
 {
  N = q.front();
  if(N.x == ei && N.y == ej && N.z == ek && N.time <= t)
  {
   flag = true;
   return 1;
   //break;
  }
  /*
  if(N.time > t)
  {
   break;
  }
  */
  q.pop();
  for(i = 0; i < 4; i++)
  {
   P.x = N.x;
   P.y = N.y + dir[i][0];
   P.z = N.z + dir[i][1];
   if(P.y >= n || P.y < 0 || P.z >= m || P.z < 0 ||  map[P.x][P.y][P.z] == '*' || N.time + 1 > t || hash[P.x][P.y][P.z])
    continue;
   if( map[P.x][P.y][P.z] == '.' || map[P.x][P.y][P.z] == 'P')
   {
    P.time = N.time + 1;
    q.push(P);
    hash[P.x][P.y][P.z] = true;

   }
   if( map[P.x][P.y][P.z] == '#' && !hash[P.x][P.y][P.z] && map[!P.x][P.y][P.z] != '*' && map[!P.x][P.y][P.z] != '#' && !hash[P.x][P.y][P.z])
   {
    
    P.x = !N.x;
    P.time = N.time + 1;
    q.push(P);
    hash[P.x][P.y][P.z] = true;
    //hash[!P.x][P.y][P.z] = true;

   }
  }
    

   
 }
 return 1;

}
int main()
{
 int c;
 int i, j, k;
 cin>>c;
 while(c--)
 {
  cin>>n>>m>>t;
  for(i = 0; i < 2; i++)
  for(j = 0; j < n; j++)
  for(k = 0; k < m; k++)
  {
   cin>>map[i][j][k];
   if(map[i][j][k] == 'P')
   {
    ei = i;
    ej = j;
    ek = k;
   }

  }
  memset(hash, false, sizeof(hash));
  flag = false;

  bfs();
  if(flag)
   printf("YES/n");
  else
   printf("NO/n");
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值