zoukankan      html  css  js  c++  java
  • HOJ 位图[BFS]

    位图
    Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
    Total submit users: 230, Accepted users: 201
    Problem 10109 : No special judgement
    Problem description
    现在我们给出一个n×m的单色位图,且该为图中至少含有一个白色的像素。我们用(i, j)来代表第i行第j列的像素,并且定义两点p1=(i1, j1)和p2=(i2, j2)之间的距离为: d(p1, p2)=|i1 - i2| + |j1 - j2|
    请写一个程序:
    读入该位图;对于每个像素,计算出离该像素最近的白色像素与它的距离。

    Input
    第一行包括两个用空格分开的整数n和m,1<=n<=182,1<=m<=182。以下的n行每行包括一个长度为m的用0和1组成的字符串,在第i+1行的第j个字符如果为”1”,那么表示像素(i, j)为白的,否则为黑的。

    Output
    输出一个n×m的数表,其中的第i行的第j个数字为f(i, j)表示像素(i, j)到最近的白色像素的距离。

    Sample Input
    3 4
    0001
    0011
    0110
    Sample Output
    3 2 1 0
    2 1 0 0
    1 0 0 1
    Judge Tips
    注意输出的f(i,j)数据,每两个之间为一个空格。
    你可以在网站http://www.mimuw.edu.pl/~msawicki/stereo.html上找到一个叫做Stereogram的小程序,这就是该问题的原型。


    Problem Source
    SDOI

    Submit   Discuss   Judge Status  Problems  Ranklist 

    刚开始的时候一直想着用1来作为起点进行搜索,后来才改过来,BFS搜的就是最短距离。

    code:

      1 /*
      2 3 2
      3 01
      4 00
      5 10
      6 1 0
      7 1 1
      8 0 1
      9 4 4
     10 1000
     11 0000
     12 0000
     13 0001
     14 0 1 2 3
     15 1 2 3 2
     16 2 3 2 1
     17 3 2 1 0
     18 5 5
     19 00000
     20 00000
     21 00100
     22 00000
     23 00000
     24 4 3 2 3 4
     25 3 2 1 2 3
     26 2 1 0 1 2
     27 3 2 1 2 3
     28 4 3 2 3 4
     29 5 5
     30 10001
     31 00000
     32 00100
     33 00000
     34 10001
     35 0 1 2 1 0
     36 1 2 1 2 1
     37 2 1 0 1 2
     38 1 2 1 2 1
     39 0 1 2 1 0
     40 */ 
     41 #include <iostream>   
     42 #include <iomanip>   
     43 #include <fstream>   
     44 #include <sstream>   
     45 #include <algorithm>   
     46 #include <string>   
     47 #include <set>   
     48 #include <utility>   
     49 #include <queue>   
     50 #include <stack>   
     51 #include <list>   
     52 #include <vector>   
     53 #include <cstdio>   
     54 #include <cstdlib>   
     55 #include <cstring>   
     56 #include <cmath>   
     57 #include <ctime>   
     58 #include <ctype.h> 
     59 using namespace std;
     60 
     61 int n,m; 
     62 int map[205][205];
     63 int res[205][205];
     64 int record[100205][2];
     65 int vst[205][205]; 
     66 int cnt;
     67 char str[1100];
     68 int dir[4][2]={
     69     0,1,
     70     0,-1,
     71     1,0,
     72     -1,0 
     73 };
     74 
     75 typedef struct node 
     76 {
     77     int x,y;
     78     int step;
     79 }Node;
     80 
     81 bool check(int x,int y)
     82 {
     83     if(x>=0&&x<n&&y>=0&&y<m&&!vst[x][y])
     84         return true;
     85     else
     86         return false;
     87 }
     88 
     89 void BFS(int sx,int sy)
     90 {
     91     int i;
     92     memset(vst,0,sizeof(vst));
     93     vst[sx][sy]=1;
     94     queue<Node>Que;
     95     Node pre,last;
     96     pre.x=sx;
     97     pre.y=sy;
     98     pre.step=0;
     99     Que.push(pre);
    100     while(!Que.empty())
    101     {
    102         pre=Que.front();
    103         Que.pop();
    104         if(map[pre.x][pre.y]==1)
    105         {
    106             res[sx][sy]=pre.step;
    107              return;
    108         } 
    109         for(i=0;i<4;i++)
    110         {
    111             last.x=pre.x+dir[i][0];
    112             last.y=pre.y+dir[i][1];
    113             last.step=pre.step+1;
    114             if(check(last.x,last.y))
    115             {
    116                 Que.push(last);
    117                 vst[last.x][last.y]=1;
    118             }
    119         }
    120     }
    121 }
    122     
    123 
    124 int main()
    125 {
    126     int i,j;
    127     while(~scanf("%d%d",&n,&m))
    128     { 
    129         cnt=0;
    130         memset(map,0,sizeof(map));
    131         memset(res,0,sizeof(res)); 
    132         getchar();
    133         for(i=0;i<n;i++)
    134         {
    135              for(j=0;j<m;j++)
    136             {
    137                  if(getchar()=='0')
    138                  {
    139                     map[i][j]=0;
    140                      record[cnt][0]=i;
    141                      record[cnt++][1]=j;
    142                 } 
    143                  else
    144                     map[i][j]=1;
    145               }
    146             getchar();
    147         }
    148         for(i=0;i<cnt;i++)
    149             BFS(record[i][0],record[i][1]);
    150         for(i=0;i<n;i++)
    151         {
    152             for(j=0;j<m;j++)
    153             {
    154                 if(j==0)
    155                    printf("%d",res[i][j]);
    156                 else
    157                    printf(" %d",res[i][j]);
    158             }
    159             printf("\n");
    160         }  
    161     } 
    162     return 0;
    163 }






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    2015年5月1日 转载--各种变量在内存中的分布
    2015年4月30日 计算两个日期天数,写的代码
    2015年4月29日 dayofweek
    2015年4月28日
    2015年4月28日----高大上的数组,进制准换,最多是35进制
    2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
    2015年4月27日
    《C++ Primer》学习笔记:迭代器介绍
    《C++ Primer》学习笔记:3.3.3其他vector操作
    《C++ Primer》学习笔记:向vector对象添加元素蕴含的编程假定
  • 原文地址:https://www.cnblogs.com/XBWer/p/2602418.html
Copyright © 2011-2022 走看看