标签:搜索

[ACM_NYOJ_27]水池数目

蓝飞 蓝飞 | 时间:2013-05-06, Mon | 13,567 views
编程算法 

水池数目

时间限制:3000 ms | 内存限制:65535 KB

描述

南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池。

输入

第一行输入一个整数N,表示共有N组测试数据
每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0<n<100),然后,输入接下来的m行每行输入n个数,表示此处有水还是没水(1表示此处是水池,0表示此处是地面)

输出

输出该地图中水池的个数。
要注意,每个水池的旁边(上下左右四个位置)如果还是水池的话的话,它们可以看做是同一个水池。

样例输入

2
3 4
1 0 0 0 
0 0 1 1
1 1 1 0
5 5
1 1 1 1 0
0 0 1 0 1
0 0 0 0 0
1 1 1 0 0
0 0 1 1 1

样例输出

2
3

来源

NYOJ27

代码如下:

#include<stdio.h>
#include<queue>

using namespace std;

struct S{
	int row, col;
	S(int row, int col):row(row),col(col){}
};

int a[100][100];
int o[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		int m, n, num = 0;
		queue<S> q;
		scanf("%d %d", &m, &n);
		for(int i = 0; i < m; ++i){
			for(int j = 0; j < n; ++j){
				scanf("%d", &a[i][j]);
			}
		}
		for(int i = 0; i < m; ++i){
			for(int j = 0; j < n; ++j){
				if(a[i][j] == 1){
					++num;
					a[i][j] = 2;
					q.push(S(i, j));
					while(!q.empty()){
						S s = q.front();
						q.pop();
						for(int k = 0; k < 4; ++k){
							int row = s.row + o[k][0];
							int col = s.col + o[k][1];
							if(row >= 0 && col >= 0 && row < m && col < n && a[row][col] == 1){
								a[row][col] = 2;
								q.push(S(row, col));
							}
						}
					}
				}
			}
		}
		printf("%d\n", num);
	}
	return 0;
}

[ACM]简单回溯——猜牌游戏

蓝飞 蓝飞 | 时间:2012-06-02, Sat | 5,450 views
编程算法 

猜牌游戏

【问题描述】

猜牌游戏:桌上有分别写着1-100的100张牌,游戏者从100张牌子中抽出K张,把K(1<K<100)张牌对应的数字相乘得到一个结果S,然后把结果S告诉挑战者,让挑战者猜游戏者K张牌的可能组合。游戏者也可能报一个假的结果S给挑战者,也就是不存在K张牌相乘得到该结果,这时挑战者要辨别游戏者是否说谎。挑战者猜中则为赢,猜错就为输。

【输入形式】

从标准输入自然数S和自然数K。

【输出形式】

输出K张牌的所有方式(用空格隔开),每一种方式为一行,在每一行末均输出一个回车符。如果不存在K张牌相乘得到S的情况,则输出LIE。

(阅读全文…)

[ACM_ZOJ_1003]Crashing Balloon

蓝飞 蓝飞 | 时间:2012-05-22, Tue | 14,127 views
编程算法 

Crashing Balloon

Time Limit: 2 Seconds Memory Limit: 65536 KB

Description

On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV. The rule is very simple. On the ground there are 100 labeled balloons, with the numbers 1 to 100. After the referee shouts "Let's go!" the two players, who each starts with a score of "1", race to crash the balloons by their feet and, at the same time, multiply their scores by the numbers written on the balloons they crash. After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant reports his\her score, the product of the numbers on the balloons he\she's crashed. The unofficial winner is the player who announced the highest score.

Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved. The player who claims the lower score is entitled to challenge his\her opponent's score. The player with the lower score is presumed to have told the truth, because if he\she were to lie about his\her score, he\she would surely come up with a bigger better lie. The challenge is upheld if the player with the higher score has a score that cannot be achieved with balloons not crashed by the challenging player. So, if the challenge is successful, the player claiming the lower score wins.

So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score 343 is by crashing balloons labeled 7 and 49, and the only way to score 49 is by crashing a balloon labeled 49. Since each of two scores requires crashing the balloon labeled 49, the one claiming 343 points is presumed to be lying.

On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one crashes balloons 2, 3 and 27, while the other crashes balloon 81), so the challenge would not be upheld.

By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if one player claims 10001 points and the other claims 10003, then clearly none of them are telling the truth. In this case, the challenge would not be upheld.

Unfortunately, anyone who is willing to referee a game of crashing balloon is likely to get over-excited in the hot atmosphere that he\she could not reasonably be expected to perform the intricate calculations that refereeing requires. Hence the need for you, sober programmer, to provide a software solution.

Input

Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of crashing balloon.

Output

Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.

(阅读全文…)

[ACM_HDU_1515]Anagrams by Stack

蓝飞 蓝飞 | 时间:2012-05-09, Wed | 6,580 views
编程算法 

Anagrams by Stack

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 614 Accepted Submission(s): 311

Description

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:

[
i i i i o o o o
i o i i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o o is valid, but
i i o is not (it's too short), neither is
i i o o o i (there's an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

(阅读全文…)

[ACM_NYOJ_10]Skiing(深度优先搜索&动态规划)

蓝飞 蓝飞 | 时间:2012-04-17, Tue | 8,108 views
编程算法 

Skiing

时间限制:3000 ms | 内存限制:65535 KB
难度:5

描述

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子:

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

(阅读全文…)