码迷,mamicode.com
首页 > 其他好文 > 详细

wenbao与bfs

时间:2018-04-14 15:29:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:mil   strong   acm   isp   type   --   ble   net   pop   

学长代码(杭电2645)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n,m;
 5 char s[200][200];
 6 int ma[200][200],ans[200][200];
 7 bool vis[200][200];
 8 int dir[4][2]={-1,0,0,-1,1,0,0,1};
 9 struct Node
10 {
11 int x,y;
12 int step;
13 };
14 
15 int bfs(int x,int y)
16 {
17 memset(vis,0,sizeof(vis));
18 Node a,b,c;
19 a.x=x,a.y=y,a.step=0;
20 queue<Node>q;
21 q.push(a);
22 while(!q.empty())
23 {
24 b=q.front();
25 q.pop();
26 vis[b.x][b.y]=1;
27 if(ma[b.x][b.y]==1)return b.step;
28 for(int i=0;i<4;i++)
29 {
30 c=b;
31 c.x+=dir[i][0];
32 c.y+=dir[i][1];
33 c.step++;
34 if(c.x<0||c.x>=n||c.y<0||c.y>=m||vis[c.x][c.y])continue;
35 q.push(c);
36 }
37 }
38 return -1;
39 }
40 
41 int main()
42 {
43 while(~scanf("%d%d",&n,&m))
44 {
45 for(int i=0;i<n;i++)
46 scanf("%s",s[i]);
47 for(int i=0;i<n;i++)
48 for(int k=0;s[i][k]!=\0;k++)
49 ma[i][k]=s[i][k]-0;
50 for(int i=0;i<n;i++)
51 for(int k=0;k<m;k++)
52 ans[i][k]=bfs(i,k);
53 for(int i=0;i<n;i++)
54 {
55 for(int k=0;k<m-1;k++)
56 printf("%d ",ans[i][k]);
57 printf("%d\n",ans[i][m-1]);
58 }
59 }
60 return 0;
61 }

wenbao(杭电2645)

 1 #include <stdio.h>
 2 #include <queue>
 3 #include <string.h>
 4 using namespace std;
 5 int n,m;
 6 char s[200][200];
 7 int a[200][200];
 8 int b[200][200];
 9 int dir[4][2]= {1,0,0,1,-1,0,0,-1};
10 bool flag[200][200];
11 struct t
12 {
13 int xx,yy;
14 int num;
15 };
16 int bfs(int x,int y)
17 {
18 queue <t> p;
19 t d,b,c;
20 d.xx=x,d.yy=y,d.num=0;
21 p.push(d);
22 while(!p.empty())
23 {
24 b=p.front();
25 p.pop();
26 flag[b.xx][b.yy]=1;
27 if(a[b.xx][b.yy]==1) return b.num;
28 for(int i=0; i<4; i++)
29 {
30 c=b;
31 c.xx+=dir[i][0],c.yy+=dir[i][1];
32 if(c.xx<0||c.xx>=n||c.yy<0||c.yy>=m||flag[c.xx][c.yy])
33 continue;
34 c.num++;
35 p.push(c);
36 }
37 }
38 return -1;
39 }
40 int main()
41 {
42 while(~scanf("%d%d",&n,&m))
43 {
44 getchar();
45 for(int i=0; i<n; i++)
46 {
47 gets(s[i]);
48 for(int j=0; j<m; j++)
49 a[i][j]=s[i][j]-0;
50 }
51 for(int i=0; i<n; i++)
52 for(int j=0; j<m; j++)
53 {
54 memset(flag,0,sizeof(flag));
55 b[i][j]=bfs(i,j);
56 }
57 for(int i=0; i<n; i++)
58 {
59 for(int j=0; j<m-1; j++)
60 {
61 printf("%d ",b[i][j]);
62 }
63 printf("%d\n",b[i][m-1]);
64 }
65 }
66 return 0;
67 }

 

 

http://acm.nyist.net/JudgeOnline/problem.php?pid=92

不想说话!!!!!!!!!!!!

论dfs与bfs的速度、、、

 

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <string.h>
 4 #include <stdio.h>
 5 using namespace std;
 6 typedef pair<int, int> P;
 7 int a[1000][1500];
 8 int w, t, h;
 9 int dir[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0};
10 void dfs(){
11     pair<int, int> p1, p2;
12     p1.first = 0;
13     p1.second = 0;
14     queue<P> q;
15     q.push(p1);
16     while(!q.empty()){
17         p1 = q.front();
18         q.pop();
19         for(int i = 0; i < 4; i++){
20             p2.first = p1.first + dir[i][0];
21             p2.second  = p1.second + dir[i][1];
22             if(p2.first >= 0 && p2.first <= h+1 && p2.second >= 0 && p2.second <= w+1 && a[p2.first][p2.second] != 0){
23                 a[p2.first][p2.second] = 0;
24                 q.push(p2);
25             }
26         }
27     }
28 }
29 // void dfs(int x, int y){
30     // for(int i = 0; i < 4; i++){
31         // int xx = x + dir[i][0];
32         // int yy = y + dir[i][1];
33         // if(xx >= 0 && xx <= h+1 && yy >= 0 && yy <= w+1 && a[xx][yy] != 0){
34             // a[xx][yy] = 0;
35             // dfs(xx, yy);
36         // }
37     // }
38 // }
39 int main(){
40     scanf("%d", &t);
41     while(t--){
42         scanf("%d %d", &w, &h);
43         for(int i = 0; i <= w+1; i++){
44             a[0][i] = 1;
45             a[h+1][i] = 1;
46         }
47         for(int i = 0; i <= h+1; i++){
48             a[i][0] = 1;
49             a[i][w+1] = 1; 
50         }
51         for(int i = 1; i <= h; i++){
52             for(int j = 1; j <= w; j++){
53                 scanf("%d", &a[i][j]);
54             }
55         }
56          dfs();
57         for(int i = 1; i <= h; i++){
58             for(int j = 1; j <= w; j++){
59                 if(j != w)
60                     printf("%d ", a[i][j]);
61                 else
62                     printf("%d\n", a[i][j]);
63             }
64         }
65     }
66     return 0;
67 }
68         

 

 

 

只有不断学习才能进步!

 

wenbao与bfs

标签:mil   strong   acm   isp   type   --   ble   net   pop   

原文地址:https://www.cnblogs.com/wenbao/p/5727797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!