A計劃 hdu 2102 終於AC,why?? bfs

來源:互聯網
上載者:User

/*
 這是看到這個題目第一次寫的代碼,但是 標記為@的地方沒有考慮,一直測試資料都過不了,怎麼回事?
 為何要犯那麼糾結的錯誤呢??還有要考慮兩層都是#的情況的,其實一看題目我就想到的,我以為不要考慮的
 */
#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");
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.