題目連接:http://poj.org/problem?id=3984編程
Description數組
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
Inputspa
Outputcode
Sample Inputblog
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
Sample Outputip
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
題解:get
裸的BFS水題。string
AC代碼:io
#include<cstdio> #include<cstring> #include<queue> #include<stack> using namespace std; const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; int mp[5][5]; struct Node{ int x,y; int cnt; Node(){} Node(int _x,int _y,int _cnt) { x=_x, y=_y, cnt=_cnt; } inline bool out() { return x<0 || x>=5 || y<0 || y>=5; } }; queue<Node> Q; bool vis[5][5]; Node pre[5][5]; stack<Node> ans; int main() { memset(mp,1,sizeof(mp)); for(int i=0;i<5;i++) for(int j=0;j<5;j++) scanf("%d",&mp[i][j]); memset(vis,0,sizeof(vis)); Q.push((Node){0,0,0}); vis[0][0]=1; while(!Q.empty()) { Node now=Q.front(); Q.pop(); if(now.x==4 && now.y==4) { ans.push(now); break; } for(int k=0;k<4;k++) { Node nxt=Node(now.x+dx[k],now.y+dy[k],now.cnt+1); if(nxt.out()) continue; if(mp[nxt.x][nxt.y]) continue; if(vis[nxt.x][nxt.y]) continue; Q.push(nxt); vis[nxt.x][nxt.y]=1; pre[nxt.x][nxt.y]=now; } } while(ans.top().cnt) ans.push(pre[ans.top().x][ans.top().y]); while(!ans.empty()) { printf("(%d, %d)\n",ans.top().x,ans.top().y); ans.pop(); } }