poj 1856Sea Battle(BFS ,DFS亦可)

                终于AC了,眼泪汪汪啊……昨天从晚上11点弄到2点,开始时TL,然后WR……今天早上再看代码,突然发现,有一个等号没写,悲催啊……

这个题的关键在于先找出‘#’围成的最大矩形,然后判断: 矩形内部是否全为‘#’; 左边界的左边,右边界的右边和下边界的下边是否全为‘.’ ;下方两个顶点的对顶点是否为‘.’;

Sea Battle
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2429 Accepted: 832

Description

During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to guard the banks of the Vltava river. Unfortunately, in the case of any incident, the Czech Admiralty have only a few captains able to control over the large sea battle. Therefore, it was decided to educate new admirals. As an excellent preparation, the game of "Sea Battle" was chosen to help with their study program. 

In this well-known game, a predefined number of ships of predefined shapes are placed on the square board in such a way that they cannot contact one another even with their corners. In this task, we will consider rectangular shaped ships only. The unknown number of rectangular ships of unknown sizes are placed on a rectangular board. All the ships are full rectangles built of hash characters. Write a program that counts the total number of ships present in the field. 

Input

The input consists of more scenarios. The description of each scenario begins with two integer numbers R and C separated with a single space, 1 <= R,C <= 1000. These numbers give the number of rows and columns in the game field. 

After these two numbers, there are R lines, each of them containing C characters. Each character is either hash ("#") or dot ("."). Hashes denote ships, dots water. 

Then, the next scenario description begins. At the end of the input, there will be a line containing two zeros instead of the field size. 

Output

Output a single line for every scenario. If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence "There are S ships." where S is the number of ships. 

Otherwise, print the sentence "Bad placement.". 

Sample Input

6 6
.....#
##...#
##...#
..#..#
.....#
######
6 8
.....#.#
##.....#
##.....#
.......#
#......#
#..#...#
0 0

Sample Output

Bad placement.
There are 5 ships.

代码如下:

#include<stdio.h>
#include<string.h>
int r, c;
char map[1001][1001];
int visited[1001][1001]={0};

int BFS(int x, int y)
{
    int iy=y, jx=x;
    //找出最大矩形(jx, iy)- (x, y)
    while(x+1<c && map[iy][x+1]=='#' )
        x++;
    while(y+1<r && map[y+1][jx]=='#' )
        y++;
    int i ,j;
    //先判断边界是否符合
    for(j=jx; j<=x; j++)//矩形的下方
    {
        if(y<r-1)
        {
            if( map[y+1][j]=='#')
                return 0;
        }
    }
    
    for(i=iy; i<=y; i++)
    {
        if(jx>=1)//矩形左边
        {
            if( map[i][jx-1]=='#')
                return 0;
        }
        if(x<c-1)//矩形右边
        {
            if( map[i][x+1]=='#')
                return 0;
        }
    }
	//判断矩形内部是否符合
	for(i=iy; i<=y; i++)
		for(j=jx; j<=x; j++)
		{
			if(map[i][j]=='#')
				visited[i][j]=1;
			else return 0;
		}

    //再判断下方两个个顶点的对顶点
			
	if(y<r-1 && jx>=1)//左下角
		if( map[y+1][jx-1]=='#')
			return 0;
				
	if(y<r-1 && x<c-1)//右下角
		if( map[y+1][x+1]=='#')
			return 0;
	return 1;
}
int main()
{
    int i, j;
	int num;
	int flag;
    while(scanf("%d %d",&r,&c) && r)
    {
		getchar();
		num=0;
		flag=0;
		for(i=0; i<r; i++)
			for(j=0; j<c; j++)
		        visited[i][j]=0;
        for(i=0; i<r; i++)
        {
		    gets(map[i]);
        }
        for(i=0; i<r; i++)
        {
			if( flag ) break;
			for(j=0; j<c; j++)
			{
				if( map[i][j]=='#' && !visited[i][j])
				{
					if( BFS(j, i) )
						num++;
					else
					{
						flag=1;break;
					}
				}
			}
		}
		if( flag ) printf("Bad placement.\n");
		else  printf("There are %d ships.\n",num);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值