HDU 1072 Nightmare BFS

來源:互聯網
上載者:User

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

其實就是多加了一個引爆時間的限制條件,反正n,m給的很小,直接記錄3維狀態,之後就很隨意了。

#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 = 15;const int WALL = 0;const int EMPTY = 1;const int STR = 2;const int TAR = 3;const int RESET = 4;const int dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};int mp[maxn][maxn], st[maxn][maxn][7];int n,m,sx,sy,ex,ey;void bfs() {    queue<int> qx,qy,qt;    qx.push(sx); qy.push(sy); qt.push(6);    st[sx][sy][6] = 0;    while(!qx.empty()) {        int x = qx.front(), y = qy.front(), t = qt.front();        int nowt = st[x][y][t];        qx.pop(); qy.pop(); qt.pop();        for(int i = 0;i < 4;i++) {            int nx = x + dx[i], ny = y + dy[i];            int nt = mp[nx][ny] == RESET ? 6 : t - 1;            if(t - 1 > 0 && mp[nx][ny] != WALL) {                if(nowt + 1 < st[nx][ny][nt]) {                    st[nx][ny][nt] = nowt + 1;                    qx.push(nx); qy.push(ny); qt.push(nt);                }            }        }    }}int main() {    int T; scanf("%d",&T);    while(T--) {        scanf("%d%d",&n,&m);        memset(mp,0,sizeof(mp));        memset(st,0x3f,sizeof(st));        int inf = st[0][0][0],ans = inf;        for(int i = 1;i <= n;i++) {            for(int j = 1;j <= m;j++) {                scanf("%d",&mp[i][j]);                if(mp[i][j] == STR) {                    sx = i; sy = j;                }                if(mp[i][j] == TAR) {                    ex = i; ey = j;                }            }        }        bfs();        for(int i = 1;i < 6;i++) ans = min(ans,st[ex][ey][i]);        if(ans >= inf) puts("-1");        else printf("%d\n",ans);    }    return 0;}

  

HDU 1072 Nightmare 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.