• POJ 1856 Sea Battle (bfs,dfs)


    题意:寻找相连的区域,八个方向都要找,判断这个区域是否为矩形。

    这题写得太悲剧了,写哭了,bfs写完超时,dfs写完还超时...........最后才发现是在输入处理上出了问题....哎,啥都不说了....都在代码里了........

    bfs:

    #include <iostream>
    #include
    <cstdio>
    #include
    <algorithm>
    #include
    <memory.h>
    #include
    <cmath>
    #include
    <vector>
    using namespace std;

    const int BORDER = (1<<26)-1;
    #define MAXN 1010
    #define INF 0x7ffffff
    #define _clr(x,y) memset(x,y,sizeof(x))
    #define _add(x) ((++x)&BORDER)
    #define _in(x) scanf("%d",&x)
    #define _out(x) printf("%d\n",x)
    #define _min(m,v) m<v?m:v
    #define _max(m,v) m>v?m:v

    #define _set_que(q,i,a,b) {q[i].x=a;q[i].y=b;}
    #define _set_node(n,a,b) {n.x=a;n.y=b;}

    struct NODE{
    int x,y;
    }que[BORDER
    +5],node[MAXN*MAXN];
    int index;
    int direct[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},
    {
    1,1},{1,0},{1,-1},{0,-1}};
    int direct1[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    int visit[MAXN][MAXN];
    char arr[MAXN][MAXN];
    int n,m,en,st,ans,minx,miny,maxx,maxy;
    inline
    bool _is(const int& x,const int& y)
    {
    if(x<0 || x>=n)
    return false;
    if(y<0 || y>=m)
    return false;
    return true;
    }
    inline
    void _max_min(const int& a,const int& b)
    {
    minx
    = _min(minx,a);
    miny
    = _min(miny,b);
    maxx
    = _max(maxx,a);
    maxy
    = _max(maxy,b);
    }
    int init()
    {
    en
    = 1;
    st
    = 0;
    ans
    = 0;
    index
    = 0;
    _clr(visit,
    0);
    return 0;
    }
    int bfs(const int& px,const int& py)
    {
    int x,y,i,j,tmp,a,b,sum;
    /* init */
    en
    = 1;
    st
    = sum = 0;
    visit[px][py]
    = 1;
    minx
    = INF;
    miny
    = INF;
    maxx
    = -1;
    maxy
    = -1;
    _set_que(que,
    0,px,py);
    while(st < en)
    {
    a
    = que[st].x;
    b
    = que[st].y;
    _max_min(a,b);
    ++sum;
    _add(st);
    for(i = 0; i < 8; ++i)
    {
    x
    = a + direct[i][0];
    y
    = b + direct[i][1];
    if(_is(x,y) && arr[x][y]=='#' && !visit[x][y])
    {
    visit[x][y]
    = 1;
    _set_que(que,en,x,y);
    _add(en);
    }
    }
    }
    tmp
    = (maxx-minx+1)*(maxy-miny+1);
    if(tmp == sum)
    return 1;
    else
    return 0;
    }
    int main()
    {
    int i,j,tmp;
    char str[MAXN];
    while(scanf("%d%d",&n,&m))
    {
    if(!n && !m)
    break;
    init();
    gets(str);
    for(i = 0; i < n; ++i)
    scanf(
    "%s",arr[i]);
    for(i = 0; i < n; ++i)
    {
    for(j = 0; j < m; ++j)
    if(arr[i][j] == '#' && !visit[i][j])
    {
    tmp
    = bfs(i,j);
    if(tmp)
    ++ans;
    else
    {
    ans
    = -1;
    break;
    }
    }
    if(ans == -1)
    break;
    }
    if(ans == -1)
    printf(
    "Bad placement.\n");
    else
    printf(
    "There are %d ships.\n",ans);
    }
    return 0;
    }

    dfs:

    #include <iostream>
    #include
    <cstdio>
    #include
    <algorithm>
    #include
    <memory.h>
    #include
    <cmath>
    #include
    <vector>
    using namespace std;

    const int BORDER = (1<<26)-1;
    #define MAXN 1010
    #define INF 0x7ffffff
    #define _clr(x,y) memset(x,y,sizeof(x))
    #define _add(x) ((++x)&BORDER)
    #define _in(x) scanf("%d",&x)
    #define _out(x) printf("%d\n",x)
    #define _min(m,v) m<v?m:v
    #define _max(m,v) m>v?m:v

    #define _set_que(q,i,a,b) {q[i].x=a;q[i].y=b;}
    #define _set_node(n,a,b) {n.x=a;n.y=b;}

    struct NODE{
    int x,y;
    }que[BORDER
    +5],node[MAXN*MAXN];
    int index;
    int direct[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},
    {
    1,1},{1,0},{1,-1},{0,-1}};
    int direct1[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    int visit[MAXN][MAXN];
    char arr[MAXN][MAXN];
    int n,m,en,st,ans,minx,miny,maxx,maxy,sum;
    inline
    bool _is(const int& x,const int& y)
    {
    if(x<0 || x>=n)
    return false;
    if(y<0 || y>=m)
    return false;
    return true;
    }
    inline
    void _max_min(const int& a,const int& b)
    {
    minx
    = _min(minx,a);
    miny
    = _min(miny,b);
    maxx
    = _max(maxx,a);
    maxy
    = _max(maxy,b);
    }
    int init()
    {
    en
    = 1;
    st
    = 0;
    ans
    = 0;
    index
    = 0;
    _clr(visit,
    0);
    return 0;
    }
    int dfs(const int& x,const int& y)
    {
    int a,b;
    visit[x][y]
    = 1;
    ++sum;
    _max_min(x,y);
    for(int i = 0; i < 8; ++i)
    {
    a
    = x + direct[i][0];
    b
    = y + direct[i][1];
    if(_is(a,b) && arr[a][b]=='#' && !visit[a][b])
    dfs(a,b);
    }
    return 0;
    }
    int main()
    {
    int i,j,tmp;
    char str[MAXN];
    while(scanf("%d%d",&n,&m))
    {
    if(!n || !m)
    return 0;
    init();
    for(i = 0; i < n; ++i)
    {
    scanf(
    "%s",arr[i]);
    }
    for(i = 0; i < n; ++i)
    for(j = 0; j < m; ++j)
    {
    if(arr[i][j] == '#' && !visit[i][j])
    {
    maxx
    = maxy = -1;
    minx
    = miny = INF;
    sum
    = 0;
    dfs(i,j);
    if( sum == (maxx-minx+1)*(maxy-miny+1) )
    ++ans;
    else
    {
    ans
    = -1;
    break;
    }
    }
    if(ans == -1)
    break;
    }
    if(ans == -1)
    printf(
    "Bad placement.\n");
    else
    printf(
    "There are %d ships.\n",ans);
    }
    return 0;
    }

  • 相关阅读:
    Request Payload 和 Form Data 的区别
    es6 字符串模板拼接和传统字符串拼接
    TypeScript
    Jquery的$(document).click() 在iphone手机上失效的问题
    Vuex 是什么?
    什么是JSONP?
    git 放弃本地修改操作
    CSS3+HTML5+JS 实现一个块的收缩&展开动画
    Promise学习笔记(一)
    React@16.13.1配合antd UI使用,自定义主题
  • 原文地址:https://www.cnblogs.com/lvpengms/p/1701600.html
Copyright © 2020-2023  润新知