## [ACM_NYOJ_27]水池数目

### 水池数目

```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_ZOJ_1003]Crashing Balloon

### 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

### 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（深度优先搜索&动态规划）

### Skiing

#### 描述

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

(阅读全文…)