pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Traversal #322

Open morris821028 opened 8 years ago

morris821028 commented 8 years ago

Problem Description

The task is to start from a given cell, traverse every cell of an $n$ by $m$ matrix in a loop without duplication. For example, if $n = 2$ and $m = 2$ and we start from $(1, 0)$, then we can go from $(1, 0)$, to $(1, 1)$, to $(0, 1)$, to $(1, 1)$, then back to $(1, 0)$. For simplicity we assume that $m$ is always an even number.

Subtasks

p10077-sample input 2
p10077 generic solution

There are many cases in one input file, please read it until EOF.

There are four numbers in one case.

The first two numbers means the size of the $n$ by $m$ matrix. The first number is $n$. The second number is $m$.

The last two numbers means the cell that you start from.

Sample Input 1

2 2 0 0

Sample Output 1

0 0
0 1
1 1
1 0
0 0

Sample Input 2

3 4 1 2

Sample Output 2

1 2
2 2
2 3
1 3
0 3
0 2
0 1
0 0
1 0
2 0
2 1
1 1
1 2

Sample Input 3

2 2 0 0
3 4 1 2

Sample Output 3

0 0
0 1
1 1
1 0
0 0
1 2
2 2
2 3
1 3
0 3
0 2
0 1
0 0
1 0
2 0
2 1
1 1
1 2