jzxx1846Space Exploration

题目描述
Farmer John’s cows have finally blasted off from earth and are now floating around space in their Moocraft. The cows want to reach their fiery kin on Jupiter’s moon of Io, but to do this they must first navigate through the dangerous asteroid belt. Bessie is piloting the craft through this treacherous N x N (1 <= N <= 1,000) sector of space. Asteroids in this sector comprise some number of 1 x 1 squares of space-rock connected along their edges (two squares sharing only a corner count as two distinct asteroids). Please help Bessie maneuver through the field by counting the number of distinct asteroids in the entire sector. Consider the 10 x 10 space shown below on the left. The ''s represent asteroid chunks, and each ‘.’ represents a .vast void of empty space. The diagram on the right shows an arbitrary numbering applied to the asteroids.
…**… …11…
.
… .2…
… …3…
… …3…3…
…*****… …33333…
… …3…
… …444…
.
… .5…444…
…4…6
… …7…
It’s easy to see there are 7 asteroids in this sector.
/

给出一张N
N的地图(1 <= N <= 1,000),统计其中有多少座岛屿。下图为N=10时的地图,图中“*”表示石块,“.”表示海水。
*/

输入

  • Line 1: A single integer: N * Lines 2…N+1: Line i+1 contains row i of the asteroid field: N characters
    /*
    一行: 一个整数N
    2…N+1行: 每行有 N 个字符
    */

输出

  • Line 1: A single integer indicating the number of asteroids in the field.
    /*
    一行: 一个整数,表示岛屿的个数。
    */

样例输入
10

.





.

…*
…*…
样例输出
7

来源
USACO 2011 January Bronze

传送门

满分代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int s,t;
char m[1001][1001];
struct node {
	int x,y;
};
node pre,cur;
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
void bfs(int a,int b) {
	int i,x,y;
	queue<node>que;
	m[a][b]='.';
	cur.x=a,cur.y=b;
	que.push(cur);
	while(!que.empty()) {
		cur=que.front(),que.pop();
		for(i=0; i<4; i++) {
			x=cur.x+dir[i][0];
			y=cur.y+dir[i][1];
			if(x>=0&&x<t&&y>=0&&y<t&&m[x][y]=='*') {
				m[x][y]='.';
				pre.x=x,pre.y=y;
				que.push(pre);
			}
		}
	}
}
int main() {
	int i,j,k;
	s=0;
	scanf("%d",&t);
	for(i=0; i<t; i++)
		scanf("%s",m[i]);
	for(i=0; i<t; i++) {
		for(j=0; j<t; j++) {
			if(m[i][j]=='*') {
				bfs(i,j);
				s++;
			}
		}
	}
	printf("%d\n",s);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值