麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ACM 深搜&廣搜 Counting Sheep&Space Exploration

2019-11-14 11:59:14
字體:
來源:轉載
供稿:網友

數羊/牛/石油群類的,就是分堆的搜索。

這里考慮用深搜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 Constraints0 < T <= 1000 < 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 reachtheir 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 somenumber 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);	}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 19禁国产精品福利视频 | 日本成人高清视频 | 中文字幕在线观看精品 | 成人在线视频精品 | 黄色高清免费 | 免费国产视频大全入口 | 精品国产乱码一区二区三区四区 | 在线观看免费视频麻豆 | 国产一区在线免费 | 国人精品视频在线观看 | 在线a毛片 | 一区二区三区国产在线 | 日本在线播放一区二区三区 | 免费看性xxx高清视频自由 | 精品国产高清一区二区三区 | 日韩在线毛片 | 欧美交在线| 欧美日韩亚洲成人 | 青草久久av| 色综合久久久久久久久久久 | 久久久久久亚洲综合影院红桃 | 欧美日韩高清一区二区三区 | 精品成人免费视频 | caoporn国产一区二区 | 91网在线播放 | 九九视频精品在线 | 日韩美香港a一级毛片 | 国产欧美在线一区二区三区 | av电影在线免费 | 91短视频免费 | 成人在线网站 | 免费久久久久久 | 精品国产91久久久久久久妲己 | 色999国产| 狠狠操夜夜爱 | 国产成人在线网站 | 欧美视频国产精品 | 午夜爱爱福利 | 成人毛片在线免费看 | 欧美精品一区二区三区在线播放 | 国产精品午夜性视频 |