hdu - 2645 find the nearest station (bfs水)

http://acm.hdu.edu.cn/showproblem.php?pid=2645

找出每个点到距离最近的车站的距离。

直接bfs就好。

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 using namespace std;
 5 int n,m;
 6 int maze[200][200],dis[200][200],vis[200][200];
 7 int dir[4][2]={-1,0,1,0,0,1,0,-1};
 8 struct point
 9 {
10     int x,y,step;
11 };
12
13 int bfs(int a,int b)
14 {
15    // printf("%d %d\n",a,b);
16     memset(vis,0,sizeof(vis));
17     queue<point>que;
18     point s;
19     s.x=a;s.y=b;s.step=0;
20     que.push(s);
21     vis[s.x][s.y]=1;
22     while(!que.empty())
23     {
24         point e=que.front();que.pop();
25        // printf("%d %d %d\n",e.x,e.y,e.step);
26         if(maze[e.x][e.y]) return e.step;
27         for(int i=0;i<4;i++)
28         {
29             s.x=e.x+dir[i][0];
30             s.y=e.y+dir[i][1];
31             if(!vis[s.x][s.y]&&s.x>=0&&s.x<n&&s.y>=0&&s.y<m)
32             {
33                 vis[s.x][s.y]=1;
34                 s.step=e.step+1;
35                 que.push(s);
36             }
37         }
38     }
39 }
40
41 int main()
42 {
43    // freopen("a.txt","r",stdin);
44     char s[200];
45     while(~scanf("%d%d",&n,&m))
46     {
47         for(int i=0;i<n;i++)
48         {
49             scanf("%s",s);
50             for(int j=0;j<m;j++)
51             {
52                 maze[i][j]=s[j]-‘0‘;
53                 //printf("%d\n",maze[i][j]);
54             }
55         }
56         memset(dis,0,sizeof(dis));
57         for(int i=0;i<n;i++)
58             for(int j=0;j<m;j++)
59             if(!maze[i][j])
60                dis[i][j]=bfs(i,j);
61         for(int i=0;i<n;i++)
62         {
63             for(int j=0;j<m-1;j++)
64                 printf("%d ",dis[i][j]);
65             printf("%d\n",dis[i][m-1]);
66         }
67     }
68     return 0;
69 }
时间: 2024-12-18 12:25:10

hdu - 2645 find the nearest station (bfs水)的相关文章

hdu 2645 find the nearest station

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dandelion has left the hometown so long,she finds it's difficult to find the station in the city.So she needs you ,a clear programmer, to help her.Now you

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

【HDOJ】2645 find the nearest station

裸BFS. 1 /* 2645 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 using namespace std; 8 9 #define MAXN 200 10 11 typedef struct node_t { 12 int x, y; 13 node_t() {} 1

hdu 1195 Open the Lock (bfs+优先队列)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4253    Accepted Submission(s): 1858 Problem Description Now an emergent task for you is to open a password lock. The password is c

hdu 5170 GTY&#39;s math problem(水,,数学,,)

题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){ double x1 = b*log((double)a); double x2 = d*log((double)c); if(fabs(x1-x2)<eps){ puts("=")

HDU - 4813 Hard Code (长春赛区水题)

Description Some strange code is sent to Da Shan High School. It's said to be the prophet's note. The note is extremely hard to understand. However, Professor Meng is so smart that he successfully found the pattern of the code. That is, the length of

HDU 5280 Senior&#39;s Array (暴力,水)

题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7f7f 5 using namespace std; 6 const int N=2000; 7 int a[N]

HDU 1875 畅通工程再续 (最小生成树 水)

Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米.当然,为了节省资金,只要求实现任意2个小岛之间有路通即可.其中桥的价格为 100元/米.

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is