Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)

來源:互聯網
上載者:User

/*
 崩潰到極點的priotity_queue + bfs,關鍵是路徑的儲存這點,網上看了說是要有個前驅
 可是後來自己的寫的時候就是不知怎麼儲存好,後來用了類試yt的方法,但是還是好久都沒有做了
 當在cfree在過了測試資料的時候,結果交上去是CE,using namespace std;這個忘記了加
 加上去了是wrong 15ms ,後來發現時count1 沒有初始話
*/
#include<iostream>//2486536 2010-05-24 21:00:33 Accepted 1026 31MS 464K 2482 B C++ 悔惜晟
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
 int x;
 int y;
 int t;
 int flag;
 int pre;
 friend bool operator <(node a, node b)
 {
  return a.t > b.t;
 }
}df[10000], dff[10000];
/*
struct path
{
 int x;
 int y;
 int flag;
 int pre;
}df[10000], dff[10000];
*/
char  map[105][105];
bool hash[105][105];
int dir[4][2] = { {0, 1}, {0, -1}, {-1, 0}, {1, 0} };
int n, m;
int count1;

int bfs()
{
 priority_queue<node> Q;
 node N, P;
 int i;
 count1 = 0;
 
 N.x = 0;
 N.y = 0;
 N.t = 0;
 N.flag = 0;
 N.pre = -1;
 
 Q.push(N);
 while(!Q.empty())
 {
  N = Q.top();
  
  df[count1] = N;
  
  if(N.x == n - 1 && N.y == m - 1)
  {
   return N.t;
  }
  Q.pop();
  for(i = 0 ; i < 4; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.pre = count1;
   if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < m && !hash[P.x][P.y] && map[P.x][P.y] != 'X')
   {
    if(map[P.x][P.y] == '.')
    {
     P.t = N.t + 1;
     P.flag = 0;
     Q.push(P);
    }
    else
    {
     P.t = N.t + 1 + map[P.x][P.y] - '0';
     P.flag =  map[P.x][P.y] - '0';
     Q.push(P);
    }
    hash[P.x][P.y] = true;
   }
  }
  count1++;

  

 }
 return -1;

}

int main()
{
 while(cin>>n>>m)
 {
  int i, j, tt;
  for(i = 0; i < n ; i++)
  for(j = 0; j < m ; j++)
   cin>>map[i][j];
  memset(hash, false, sizeof(hash));
   hash[0][0] = true;
  tt = bfs();
  int c = tt;
  if(tt == -1)
  {
   printf("God please help our poor hero./nFINISH/n");
   continue;
  }
  else
  {
   printf("It takes %d seconds to reach the target position, let me show you the way./n", tt);
   int w = count1;
   while(c >= 0)
   {
    if(df[w].flag == 0)
    {
     dff[c--] = df[w];
     w = df[w].pre;
    }
    else
    {
     dff[c--] = df[w];
     for(i = 0; i < df[w].flag; i++)
     {
         dff[c--] = df[w];
      
     }
       w = df[w].pre;
    }
   }
   for(i = 1; i <= tt
   {
     printf("%ds:(%d,%d)->(%d,%d)/n", i , dff[i - 1].x, dff[ i - 1].y, dff[i].x, dff[i].y);
    if(map[ dff[i].x ][ dff[i].y ] >= '1' && map[ dff[i].x ][ dff[i].y ] <= '9')
    {
     for(j = 1; j <= dff[i].flag; j++)
      printf("%ds:FIGHT AT (%d,%d)/n", i + j, dff[i].x, dff[i].y);
     
     i = i + j;
    }
    else
    i++;
     
   }
   printf("FINISH/n");
   
   
  }  
 }
}

 

 

/*
 崩潰到極點的priotity_queue + bfs,關鍵是路徑的儲存這點,網上看了說是要有個前驅
 可是後來自己的寫的時候就是不知怎麼儲存好,後來用了類試yt的方法,但是還是好久都沒有做了
 當在cfree在過了測試資料的時候,結果交上去是CE,using namespace std;這個忘記了加
 加上去了是wrong 15ms ,後來發現時count1 沒有初始話
 Memory Limit Exceeded
*/
#include<iostream>//2486536 2010-05-24 21:00:33 Accepted 1026 31MS 464K 2482 B C++ 悔惜晟
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
 int x;
 int y;
 int t;
 int flag;
 int pre;
 friend bool operator <(node a, node b)
 {
  return a.t > b.t;
 }
}T;//df[10000], dff[10000];
/*
struct path
{
 int x;
 int y;
 int flag;
 int pre;
}df[10000], dff[10000];
*/
vector<node> df, dff;
char  map[105][105];
bool hash[105][105];
int dir[4][2] = { {0, 1}, {0, -1}, {-1, 0}, {1, 0} };
int n, m;
int count1;

int bfs()
{
 priority_queue<node> Q;
 node N, P;
 int i;
 df.clear();//加了這個就AC了2487014 2010-05-24 22:39:39 Accepted 1026 31MS 552K 3148 B C++ 悔惜晟
 dff.clear();
 count1 = 0;
 
 N.x = 0;
 N.y = 0;
 N.t = 0;
 N.flag = 0;
 N.pre = -1;
 
 Q.push(N);
 while(!Q.empty())
 {
  N = Q.top();
  
  //df[count1] = N;
  df.push_back(N);
  
  if(N.x == n - 1 && N.y == m - 1)
  {
   return N.t;
  }
  Q.pop();
  for(i = 0 ; i < 4; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.pre = count1;
   if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < m && !hash[P.x][P.y] && map[P.x][P.y] != 'X')
   {
    if(map[P.x][P.y] == '.')
    {
     P.t = N.t + 1;
     P.flag = 0;
     Q.push(P);
    }
    else
    {
     P.t = N.t + 1 + map[P.x][P.y] - '0';
     P.flag =  map[P.x][P.y] - '0';
     Q.push(P);
    }
    hash[P.x][P.y] = true;
   }
  }
  count1++;

  

 }
 return -1;

}

int main()
{
 while(cin>>n>>m)
 {
  int i, j, tt, ee;
  for(i = 0; i < n ; i++)
  for(j = 0; j < m ; j++)
   cin>>map[i][j];
  memset(hash, false, sizeof(hash));
   hash[0][0] = true;
  tt = bfs();
  int c = tt;
  if(tt == -1)
  {
   printf("God please help our poor hero./nFINISH/n");
   continue;
  }
  else
  {
   printf("It takes %d seconds to reach the target position, let me show you the way./n", tt);
   int w = df.size() - 1;
   while(c >= 0)
   {
    if(df[w].flag == 0)
    {
     //dff[c--] = df[w];
     T = df[w];
     //df.pop_back();
     dff.push_back(T);
     c--;
     
     w = T.pre;
    }
    else
    {
     T = df[w];
     dff.push_back(T);
     c--;
     for(i = 0; i < df[w].flag; i++)
     {
          dff.push_back(T);
      
     }
     c -= df[w].flag;
     df.pop_back();
       w = T.pre;
    }
   }
   ee = 1;
   for(i = tt - 1; i >= 0
   {
    printf("%ds:(%d,%d)->(%d,%d)/n", ee++ , dff[i + 1].x, dff[ i + 1].y, dff[i].x, dff[i].y);
    if(map[ dff[i].x ][ dff[i].y ] >= '1' && map[ dff[i].x ][ dff[i].y ] <= '9')
    {
     for(j = 1; j <= dff[i].flag; j++)
      printf("%ds:FIGHT AT (%d,%d)/n", ee++,  dff[i].x, dff[i].y);
     
     i = i - j ;
    }
    else
    i--;
     
   }
   printf("FINISH/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.