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

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

ACM 深搜&廣搜 Counting Sheep&Space Exploration

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

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

這里考慮用深搜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);	}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一区二区精品视频在线观看 | 暴力肉体进入hdxxxx0 | av免费在线免费观看 | 被啪羞羞视频在线观看 | 久久人人爽人人爽人人片av免费 | 免费一区在线 | 亚洲第一激情 | 国产一级一级 | 福利四区 | 久久不射电影 | 婷婷中文字幕一区二区三区 | 国产精品视频导航 | 国产精品一品二区三区四区18 | 毛片免费看的 | 欧美人与性禽动交精品 | 一区二区三区无码高清视频 | 色戒在线版| 国产精品久久久久久久久久10秀 | 日本中文字幕高清 | 久久久久一区二区三区四区五区 | 久久蜜桃精品一区二区三区综合网 | 毛片av网 | 深夜免费观看视频 | 一级毛片看| 欧美特黄特色视频 | 亚洲精品 在线播放 | 2023av在线视频 | 九九热精品免费 | 欧美精品久久久久久久久久 | 免费观看一区二区三区视频 | 在线视频观看一区二区 | 97中文字幕第一一一页 | 狠狠干天天操 | 久久久久久久久久久久久国产精品 | 福利一区二区三区视频在线观看 | 99影视电影电视剧在线播放 | 午夜视| 日本中文字幕电影在线观看 | 女人裸体让男人桶全过程 | 欧美一级小视频 | 日韩黄色av网站 |