poj-1856 Sea Battle *

/*
* bfs
* 又是一道水题。。1A
*
*/
#include
<cstdio>
#include
<cstring>
using namespace std;

const int MAXN = 1000 + 5;
int r, c; //同题意
bool vis[MAXN][MAXN];
char map[MAXN][MAXN];
// sx,sy:矩形左上角。。ex,ey:矩形右下角..tot:矩形数。。。flag:是否正确
int sx, sy, ex, ey, tot, flag;


//获得ex,ey.. 若不是矩形,返回false
bool inline get_cor(int sxx, int syy, int &exx, int &eyy){
int i, j;
for(i=sxx+1; i<r && map[i][syy]=='#'; i++);
exx
= i - 1;
for(i=syy+1; i<c && map[sxx][i]=='#'; i++);
eyy
= i - 1;

for(i=sxx; i<=exx; i++){
for(j=syy; j<=eyy; j++){
if(map[i][j] != '#') return 0;
vis[i][j]
= 1;
}
}

return 1;
}

//检查矩形四周是否有‘#’
bool check(int sxx, int syy, int exx, int eyy){
if(sx > 0){
for(int i = syy-1; i<=eyy+1; i++){
if(i>=0 && i<c && map[sx-1][i]=='#') return 0;
}
}
if(ex < r-1){
for(int i = syy-1; i<=eyy+1; i++){
if(i>=0 && i<c && map[ex+1][i]=='#') return 0;
}
}
if(sy > 0){
for(int i = sxx-1; i<=exx+1; i++){
if(i>=0 && i<r && map[i][sy-1]=='#') return 0;
}
}
if(ey < c-1){
for(int i = sxx-1; i<=exx+1; i++){
if(i>=0 && i<r && map[i][ey+1]=='#') return 0;
}
}
return 1;
}


int main(){
while(scanf("%d %d", &r, &c)){
if(r == 0) return 0;

getchar();
for(int i=0; i<r; i++)
gets(map[i]);

memset(vis,
0, sizeof(vis));
tot
= 0; flag = 1;

//遍历
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(map[i][j] == '.' || vis[i][j]) continue;

sx
= i; sy = j;
if(get_cor(i, j, ex, ey) == 0){
flag
= 0;
i
= r; break;
}
if(check(sx, sy, ex, ey) == 0){
flag
= 0;
i
= r; break;
}
tot
++;
}
}
if(flag)
printf(
"There are %d ships.\n", tot);
else
printf(
"Bad placement.\n");
}

return 0;
}

posted on 2011-07-16 21:06  龙豆  阅读(324)  评论(0编辑  收藏  举报

导航