HDU 1728 逃離迷宮 BFS

來源:互聯網
上載者:User

標籤:blog   os   io   for   ar   div   cti   log   amp   

其實就是讓你找最少的拐彎次數,dk數組記錄到一個點的最少拐彎次數,每次讓一個方向上的所有點進隊就好了。

注意如果拐彎次數相等還是可以進隊的,因為過來的方向可能不一樣。

#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <set>#include <vector>#include <string>#include <queue>#include <deque>#include <bitset>#include <list>#include <cstdlib>#include <climits>#include <cmath>#include <ctime>#include <algorithm>#include <stack>#include <sstream>#include <numeric>#include <fstream>#include <functional>using namespace std;#define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector<int> VI;typedef pair<int,int> pii;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 110;const int dx[] = {0,0,1,-1};const int dy[] = {1,-1,0,0};int dk[maxn][maxn];char mp[maxn][maxn];int sx,sy,ex,ey,n,m,k;void bfs() {    queue<int> qx,qy,qk;    qx.push(sx); qy.push(sy);    dk[sx][sy] = 0;    while(!qx.empty()) {        int x = qx.front(), y = qy.front(), nk = dk[x][y];        qx.pop(); qy.pop();        for(int i = 0;i < 4;i++) {            int nx = x + dx[i], ny = y + dy[i];            while(mp[nx][ny] != ‘*‘ && dk[x][y] + 1 <= dk[nx][ny]) {                qx.push(nx); qy.push(ny);                dk[nx][ny] = dk[x][y] + 1;                nx += dx[i]; ny += dy[i];            }        }    }}int main() {    int T; scanf("%d",&T);    while(T--) {        memset(dk,0x3f,sizeof(dk));        memset(mp,‘*‘,sizeof(mp));        scanf("%d%d",&n,&m);        for(int i = 1;i <= n;i++) {            for(int j = 1;j <= m;j++) {                scanf(" %c",&mp[i][j]);            }        }        scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);        bfs();        if(dk[ex][ey] > k + 1) puts("no");        else puts("yes");    }    return 0;}

  

HDU 1728 逃離迷宮 BFS

聯繫我們

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