ACM 深搜&广搜 Counting Sheep&Space Exploration

来源:互联网 发布:java iterator 实现类 编辑:程序博客网 时间:2024/06/05 02:54

数羊/牛/石油群类的,就是分堆的搜索。

这里考虑用深搜or广搜,凭借题目给的地图范围,范围大的用广搜。


TOJ 2799 Counting Sheep

A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'd gone to bed. As always when my grandmother suggests things, I decided to try it out. The only problem was, there were no sheep around to be counted when I went to bed.



Creative as I am, that wasn't going to stop me. I sat down and wrote a computer program that made a grid of characters, where # represents a sheep, while . is grass (or whatever you like, just not sheep). To make the counting a little more interesting, I also decided I wanted to count flocks of sheep instead of single sheep. Two sheep are in the same flock if they share a common side (up, down, right or left). Also, if sheep A is in the same flock as sheep B, and sheep B is in the same flock as sheep C, then sheeps A and C are in the same flock.


Now, I've got a new problem. Though counting these sheep actually helps me fall asleep, I find that it is extremely boring. To solve this, I've decided I need another computer program that does the counting for me. Then I'll be able to just start both these programs before I go to bed, and I'll sleep tight until the morning without any disturbances. I need you to write this program for me.

输入

The first line of input contains a single number T, the number of test cases to follow.

Each test case begins with a line containing two numbers, H and W, the height and width of the sheep grid. Then follows H lines, each containing W characters (either # or .), describing that part of the grid.

输出

For each test case, output a line containing a single number, the amount of sheep flock son that grid according to the rules stated in the problem description.

Notes and Constraints
0 < T <= 100
0 < H,W <= 100

样例输入

样例输出

题目意思:数羊群,上下左右四个方向,相连的即为一个羊群。

这题用深搜就可以了,比较简单。

#include <stdio.h>int a,b,s;char m[101][101];int dir[4][2]={1,0,0,1,-1,0,0,-1};void dfs(int x,int y){  m[x][y]='.';int i,xx,yy;   for(i=0;i<4;i++)   {   xx=x+dir[i][0];   yy=y+dir[i][1];   if(xx>=0&&xx<a&&yy>=0&&yy<b&&m[xx][yy]=='#')    dfs(xx,yy);   }}int main()//2777{int i,j,u,w,t;scanf("%d",&t);while(t--){   scanf("%d%d",&a,&b);    s=0;if(a==0&&b==0)break;for(i=0;i<a;i++)scanf("%s",m[i]);for(i=0;i<a;i++){for(j=0;j<b;j++){if(m[i][j]=='#'){dfs(i,j);s++;}}} printf("%d\n",s);}}

TOJ 3834 Space 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.

<span style="font-size:14px;"></span>

It's easy to see there are 7 asteroids in this sector.

输入

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains row i of the asteroid field: N characters

输出

* Line 1: A single integer indicating the number of asteroids in the field.

样例输入

样例输出

题目意思:数牛群呗...和上面那题一样的都是上下左右四个方向,但是这个地图范围是1000*1000,所以要用广搜。

用广搜一般都是队列+结构体哦~

#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
原创粉丝点击