Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

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;
}

 

posted on 2010-03-31 17:37  Lvpengms  阅读(548)  评论(0编辑  收藏  举报