DFS & BFS
2017-04-06 21:15
429 查看
1.数独 DFS
View Code
1 #include<cstdio> 2 #include<queue> 3 #include<iostream> 4 using namespace std; 5 int s[5][5],r[5][5]; 6 int dx[4]= {1,0,-1,0},dy[4]= {0,1,0,-1}; 7 struct Point 8 { 9 int x,y,d; 10 Point *f; 11 Point() {} 12 Point(int x1,int y1,int d1) 13 { 14 x=x1; 15 y=y1; 16 d=d1; 17 f=NULL; 18 } 19 }; 20 void print(Point* p) 21 { 22 int t[25][2]; 23 int k=0; 24 while(p!=NULL) 25 { 26 t[k][0]=p->x; 27 t[k][1]=p->y; 28 k+=1; 29 p=p->f; 30 } 31 for(int i=k-1; i>=0; i--) 32 cout<<'('<<t[i][0]<<", "<<t[i][1]<<')'<<endl; 33 } 34 void bfs(int x,int y,int d) 35 { 36 queue<Point> Q; 37 Q.push(Point(x,y,d)); 38 r[x][y]=1; 39 while(!Q.empty()) 40 { 41 Point* p=new Point(); 42 p=&Q.front(); 43 Q.pop(); 44 if(p->x==4&&p->y==4) 45 { 46 print(p); 47 break; 48 } 49 for(int i=0; i<4; i++) 50 { 51 int nx=p->x+dx[i],ny=p->y+dy[i]; 52 if(nx>=0&&nx<5&&ny>=0&&ny<5&&r[nx][ny]==0&&s[nx][ny]==0) 53 { 54 r[nx][ny]=1; 55 Point t; 56 t.x=nx,t.y=ny,t.d=p->d+1; 57 t.f=p; 58 Q.push(t); 59 } 60 } 61 62 } 63 } 64 int main() 65 { 66 for(int i=0; i<5; i++) 67 for(int j=0; j<5; j++) 68 scanf("%d",&s[i][j]); 69 bfs(0,0,0); 70 return 0; 71 }
View Code
相关文章推荐
- UVA 1600 - Patrol Robot( BFS & DFS)
- HDU 1241 Oil Deposits【dfs&bfs】
- 【DFS&BFS】HDU1241-Oil Deposits
- HDU 1254:推箱子【DFS && BFS】
- DFS&&BFS模板
- SDUT:找朋友 BFS&&DFS
- DFS&BFS板子
- uva1600(最短路 bfs & dfs)
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
- hdu1026 Ignatius and the Princess I --BFS & 记录路径 & 与DFS的比较
- 回家_洛谷U3427_dfs&bfs
- poj 1130 Alien Security (dfs+bfs<反向最短路>)
- CH Round #72树洞[二分答案 DFS&&BFS]
- DFS & BFS
- Gym - 100971J- Robots at Warehouse-bfs&&dfs
- hdu1241 Oil Deposits(dfs&&bfs)
- HDU 1983 BFS&&DFS
- HDU 4771 Stealing Harry Potter's Precious【BFS+DFS】
- 【LeetCode】 Surrounded Regions (BFS && DFS)
- 数据结构 - 图的存储结构表示及其遍历 (DFS && BFS)