hdu 1026 哎,又做了這麼久 bfs

來源:互聯網
上載者:User

  有點煩的題,思路,怎麼說呢,好似不是難,就是那個記錄路徑煩,以前也做過,不過應該大有不同吧

這代碼很亂,現在我自己也不願去分析了,其實我應該再寫義多一個結構體去,記錄每步情況,把初始結點情況與其分開,以免過程中改變了

初始的情況,而這裡是明顯不應改變的

       有一點值得注意的就是,<的重載,其實自對優先順序隊列不熟,所以一開始重載錯了,如沒const和形參用的引用之類的,以後還是先自己寫比較函吧

等到偶看了stl再搞起哈

#include<iostream>#include<algorithm>#include<queue>#define N 101#define M 101using namespace std;struct node{int m_t;int x,y;int pos_x;int pos_y;int cost;bool operator<( node t) const {return m_t > t.m_t;}};char map[N][M];node m_time[N][M];int n,m;int d[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};bool in(int& x,int& y){return x >= 0 && x < n && y >= 0 && y < m;}bool X(int& x,int& y){return map[x][y] == 'X';}void input(){int i,j;for(i = 0;i < n;i++){for(j = 0;j < m;j++){    cin >>map[i][j];m_time[i][j].m_t = 0;if(map[i][j] == '.')m_time[i][j].cost = 1;else if(map[i][j] == 'X')m_time[i][j].cost = 0;elsem_time[i][j].cost = map[i][j]-'0'+1;}}}bool bfs(){priority_queue<node> q;node now;now.m_t = 0; now.x = 0; now.y = 0; now.pos_x = -1; now.pos_y = -1; now.cost = 1;map[0][0] = 'X';m_time[0][0] = now;q.push(now);while(!q.empty()){node t = q.top();//printf("(%d,%d,(%d))\n",t.x,t.y,t.m_t);//cout <<m_time[0][0].cost <<endl;q.pop();for(int i = 0; i < 4;i++){node next;next.pos_x = t.x;next.pos_y = t.y;next.x = t.x+d[i][0];next.y = t.y+d[i][1];next.cost = m_time[next.x][next.y].cost;if(in(next.x,next.y) && !X(next.x,next.y)){next.m_t = t.m_t + m_time[next.x][next.y].cost;m_time[next.x][next.y] = next;//cout << t.m_t << m_time[next.x][next.y].cost << m_time[next.x][next.y].m_t <<endl;q.push(next);if(next.x == n-1 && next.y == m-1){//m_time[n-1][m-1] = next;//cout << next.m_t <<endl;return true;}map[next.x][next.y] = 'X';}}}return false;}void print1(){cout <<"It takes " <<m_time[n-1][m-1].m_t <<" seconds to reach the target position, let me show you the way." <<endl;int x,y;x = n-1; y = m-1;while(x != 0 || y != 0){int now_x = m_time[x][y].pos_x;int now_y = m_time[x][y].pos_y;int _x = x;int _y = y;m_time[now_x][now_y].x = _x;m_time[now_x][now_y].y = _y;x = now_x; y = now_y;}int step = 0;while(true){//cout <<m_time[n-1][m-1].cost<<endl;if(m_time[x][y].cost == 1){cout <<++step <<"s:" <<"(" <<x <<"," <<y <<")->("     <<m_time[x][y].x <<"," <<m_time[x][y].y <<")" <<endl;int temp = x;x = m_time[x][y].x;y = m_time[temp][y].y;}while(m_time[x][y].cost > 1){cout <<++step <<"s:" <<"FIGHT AT (" <<x <<"," <<y <<")" <<endl;m_time[x][y].cost--;}if(x == n-1 && y == m-1){    cout <<"FINISH" <<endl;return ;}}}void print2(){cout <<"God please help our poor hero." <<endl <<"FINISH" <<endl;}int main(){while(cin >>n >>m){input();if(bfs())print1();else print2();}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.