zoukankan      html  css  js  c++  java
  • hdu1241(bfs)

    搜索题,看上去并查集也可解,不过还是感觉bfs()简单些,不过在处理数组map[x][y]时被伤着了,在处理下标时,把横坐标,纵坐标,x,y,完全懵了,习惯上把x当成纵坐标y当成横坐标,这里建的map[][]中的x其实是纵坐标,y是横坐标,显然还是太嫩了(继续革命)

    #include <iostream>
    #include <queue>
    using namespace std;
    const int Max=101;
    char map[Max][Max];
    int used[Max][Max];
    int dir[8][2]={
    {0,1},{1,1},{1,0},{1,-1},
    {0,-1},{-1,-1},{-1,0},{-1,1}
    };
    queue <int> q;
    void Bfs(int m,int n)
    {
    while(!q.empty())
    q.pop();
    int i,j,k;
    int count=0;
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    {
    if(map[i][j]=='@'&&used[i][j]==0)
    {
    //cout<<"lskjs"<<endl;
    int x,y,s,r;
    q.push(i*n+j);
    while(!q.empty())
    {
    int head;
    head=q.front();///////////////////////////////////////////////////////////////////////
    s=head/n;// 纵zuo biao
    r=head%n;//横 zuob 标
    q.pop();
    for(k=0;k<8;k++)
    {
    y=r+dir[k][0];//横坐标
    x=s+dir[k][1];//纵坐标
    // cout<<"ce:"<<x<<" "<<y<<endl;
    if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]=='@'&&used[x][y]==0)
    {
    //cout<<"shi:"<<x<<" "<<y<<endl;
    used[x][y]=1;
    q.push(x*n+y);
    }
    }

    } count++;
    }
    }
    cout<<count<<endl;
    }
    int main()
    {
    int m,n;
    while(cin>>m&&m!=0)
    {
    int i,j;
    cin>>n;
    memset(used,0,sizeof(used));
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    {
    cin>>map[i][j];
    }
    Bfs(m,n);

    }
    return 0;
    }
  • 相关阅读:
    开博语
    ch8 固定宽度、流式、弹性布局
    ch8 让div居中--使用外边距
    ch8 基于浮动的布局(两列浮动布局、三列浮动布局)
    ch3 盒模型、定位
    事件类型--鼠标与滚轮事件
    事件类型-UI事件、焦点事件
    事件对象
    事件处理程序
    内存和性能--事件委托、移除事件处理程序
  • 原文地址:https://www.cnblogs.com/orangeblog/p/2415699.html
Copyright © 2011-2022 走看看