Lake Counting poj2836(深搜水题)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/DEAR_CXN/article/details/88088363

题目链接

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. 

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M 

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

主要思想:

从任意的W开始,不停地把邻接的部分(八连通)用'.'代替。1次DFS后与初始的这个W连接的所有W就都被替换成了'.',因此直到图中不在存在W为止,总共进行DFS的次数就是答案了。8个方向的共对应了8种状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×M×N)=O(M×N)。

代码:

#include<iostream>
using namespace std;
int N,M;
const int Max_N=101;
const int Max_M=101;
char field[Max_N][Max_M];
void dfs(int x,int y){
	field[x][y]='.';//将现在所在位置替换为'.'
	for(int dx=-1;dx<=1;dx++)//循环遍历连通的8个方向
		for(int dy=-1;dy<=1;dy++){
			int nx=x+dx,ny=y+dy;//向x方向移动dx,向y方向移动dy,移动的结果为(nx,ny)
			if(0<=nx&&nx<N&&0<=ny&&ny<M&&field[nx][ny]=='W')
				dfs(nx,ny);
		}
	return;
}
void solve(){
	int res=0;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++){
			if(field[i][j]=='W'){//从有积水的地方开始dfs
				dfs(i,j);
				res++;
			}
		}
	cout<<res<<endl;
}
int main(){
	cin>>N>>M;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++){
			cin>>field[i][j];
		}
	solve();
	return 0;
}

 

展开阅读全文

Counting Out

06-23

Problem DescriptionnConsider this simple form of shuffling a deck of cards. Hold the deck face down and count to a certain number (say k), on each count taking one card from the top of the deck and placing it at the bottom. When you have finished counting, turn the top card (the k+1'th card) over and place it face up on the table. Thus cards are continually moved from the top of the deck to the bottom, even when you have fewer cards than your chosen number. Continue in this way until you have no more cards left. Thus if k was 4, you would move 4 cards, one by one, from the top of the deck to the bottom and turn over the fifth. You would then move the sixth, seventh, eighth and ninth cards to the bottom and expose the tenth. Note that the number of the exposed card is always one more than k; if k was nine you would expose every tenth card, and so on.nnWrite a program that will read in the chosen number (k) and details of a deck of cards and simulate this process. Remember that a standard deck (or pack) of cards contains 52 cards. These are divided into 4 suits - Spades, Hearts, Diamonds and Clubs. Within each suit there are 13 cards - Ace (A), 2-9, Ten (T), Jack (J), Queen (Q) and King (K).n nnInputnInput will consist of a series of scenarios. Each scenario will start with a number k (4 <= k <= 26) on a line by itself. This will be followed by the description of a deck. A deck will be specified on four lines with 13 cards on each line and with cards separated by exactly one space (see the example below). The cards are shown as they would be seen if they were face up, thus the last card in the sequence would be the top of the deck as far as you are concerned. The file will be terminated by a line consisting of a zero (0).nn nnOutputnOutput will consist of a series of lines, one for each scenario in the input. Each line will consist of the last card played, in the format shown below.nn nnSample Inputn4nH2 H7 SA HJ D4 S4 HT DJ C9 HQ CT H8 SQnDQ S6 D2 H5 CJ CK HA D5 D9 H9 S8 D3 C4nS2 C5 CQ D8 DT C6 DK H4 CA C8 C2 SK C7nS5 H6 H3 S9 S7 C3 SJ S3 ST HK DA D6 D7n0 n nnSample OutputnS8 问答

Counting Sheep

11-26

Problem DescriptionnA 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.nnnnCreative 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.nnnNow, 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.n nnInputnThe first line of input contains a single number T, the number of test cases to follow.nnEach 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.n nnOutputnFor 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.nnNotes and Constraintsn0 < T <= 100n0 < H,W <= 100n nnSample Inputn2n4 4n#.#.n.#.#n#.##n.#.#n3 5n###.#n..#..n#.###n nnSample Outputn6n3n 问答

Bean Counting

09-06

The CSC has decided to hold a contest to guess the number of beans in a given jar. To make it a wee bit more interesting, they decided to have the contestants guess the number of a particular kind of bean, the jar having many types of beans in it. Each participant would par $2.00 per guess.nIf the guess is one off (either one greater or less) than the actual count, they get a single share of the total pot. If the guess is right on, they get two shares of the total pot.nnYour task is to tally these guesses and print out how much each person wins.nnnInputnnThe first line will contain hundreds of letters, from a - z, each representing one bean of that type of bean. There will only be 26 different types of beans. The next line contains an integer n, the number of guesses. Each of the n following line will be the guess from a contestant. A contestant can guess as many times as they like. The line will begin with a single letter, in the range A - Z, leaving only 26 possible contests. The first letter will be followed by a single space, and then up to five pairs of letter/number combinations, the letter representing the type of bean for this guess, and the number the number of that bean the contestant expects to find. The letter and the value of a guess will be separated by a ':' and each guess on lines with more than one guess will be separated by a ','.nnProcess to the end of file.nnTallying:nnThe amount a contestant will receive is based on shares of a pot. The pot will consist of the total of all the guesses, at $2.00 per guess. The fraction each person gets will be based on:nnTotNum: total number of guesses by all contestantsnTotShares: total number of shares from all contestantsnOffOne: number of off-by-one guesses by that contestantnRightOn: number if right-on guesses by that contestantnn(TotNum * $2.00) / TotShares * (OffOne + (RightOn * 2))nnnOutputnnPrint out all the winners, in alphabetical order, and their winnings, separated by a single space. Each winner should be on a line by itself.nnSeparate output for different cases by a single blank line.nnnSample Inputnnaadddddddddddddddddddddddffffffffffffffffwwwwwwwwwwwwccccalalalan4nA a:6,c:6nB w:12nC d:25nD d:22,c:4,l:3,f:15nnnSample OutputnnA 3.20nB 3.20nD 9.60 问答

没有更多推荐了,返回首页