标签:std space rri stream arp bfs include csharp name
#include <iostream> #include <string.h> #include <stack> #include <queue> #include <algorithm> using namespace std; const int maxn=10; struct P { int x,y; }point[maxn]; struct PP { int fx,fy; }path[maxn][maxn]; int num[maxn][maxn]; int vis[maxn][maxn]; int dx[4]={0,-1,1,0}; int dy[4]={-1,0,0,1}; int total=0; int sx=1,sy=1; int ex=4,ey=4; char map[6][6]= { {‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘}, {‘#‘,‘.‘,‘.‘,‘.‘,‘#‘,‘#‘}, {‘#‘,‘.‘,‘#‘,‘.‘,‘.‘,‘#‘}, {‘#‘,‘.‘,‘.‘,‘.‘,‘#‘,‘#‘}, {‘#‘,‘#‘,‘.‘,‘.‘,‘.‘,‘#‘}, {‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘} }; int ok(int x,int y) { if(x<0||x>5||y<0||y>5) return 0; if(map[x][y]==‘#‘) return 0; if(vis[x][y]==1) return 0; return 1; } void dfs(int x,int y,int step) { if(x==ex&&y==ey) { total++; for(int i=0;i<step;i++) { cout<<"("<<point[i].x<<","<<point[i].y<<")"; } cout<<endl; return; } for(int i=0;i<4;i++) { int curx=x+dx[i]; int cury=y+dy[i]; if(ok(curx,cury)) { vis[curx][cury]=1; point[step].x=curx; point[step].y=cury; dfs(curx,cury,step+1); vis[curx][cury]=0; } } } void bfs(int x,int y) { num[x][y]=0; queue<P>q; P a,b; a.x=x; a.y=y; path[a.x][a.y].fx=0; path[a.x][a.y].fy=0; q.push(a); while(!q.empty()) { b=q.front(); q.pop(); for(int i=0;i<4;i++) { a.x=b.x+dx[i]; a.y=b.y+dy[i]; while(ok(a.x,a.y)) { vis[a.x][a.y]=1; q.push(a); path[a.x][a.y].fx=dx[i]; path[a.x][a.y].fy=dy[i]; num[a.x][a.y]=num[b.x][b.y]+1; } } } } void print(int x,int y) { if(x==sx&&y==sy) { cout<<"("<<x<<","<<y<<")"; return; } print(x-path[x][y].fx,y-path[x][y].fy); cout<<"("<<x<<","<<y<<")"; } int main() { memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); point[0].x=sx; point[0].y=sy; vis[sx][sy]=1; dfs(sx,sy,1); cout<<"There are "<<total<<" paths to arrive."<<endl; memset(vis,0,sizeof(vis)); vis[sx][sy]=1; bfs(sx,sy); for(int i=0;i<6;i++) { for(int j=0;j<6;j++) cout<<num[i][j]<<" "; cout<<endl; } cout<<endl; cout<<num[ex][ey]<<endl<<endl;; print(ex,ey); cout<<endl; return 0; }
标签:std space rri stream arp bfs include csharp name
原文地址:https://www.cnblogs.com/KasenBob/p/10624186.html