Algorithm


C. Valera and Tubes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).

Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1)(x2, y2)...(xr, yr), that:

  • r ≥ 2;
  • for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds;
  • each table cell, which belongs to the tube, must occur exactly once in the sequence.

Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:

  • no pair of tubes has common cells;
  • each cell of the table belongs to some tube.

Help Valera to arrange k tubes on his rectangle table in a fancy manner.

Input

The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 3002 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.

Output

Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells).

If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.

Examples
input
Copy
3 3 3
output
Copy
3 1 1 1 2 1 3
3 2 1 2 2 2 3
3 3 1 3 2 3 3
input
Copy
2 3 1
output
Copy
6 1 1 1 2 1 3 2 3 2 2 2 1
Note

Picture for the first sample:

Picture for the second sample:



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <stdio.h>

using namespace std;

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int cnt = n * m;

  int i = 1, j = 1;
  bool dir = true;
  while(k > 1) {
    printf("2");
    printf(" %d %d", i, j);
    dir ? j++ : j--;
    if(j > m) { j--; i++; dir = false; }
    if(j < 1) { j++; i++; dir = true; }
    printf(" %d %d", i, j);
    dir ? j++ : j--;
    if(j > m) { j--; i++; dir = false; }
    if(j < 1) { j++; i++; dir = true; }
    putchar('\n');
    k--;
    cnt -= 2;
  }

  printf("%d", cnt);
  while(i != n + 1 && j != m + 1) {
    printf(" %d %d", i, j);
    dir ? j++ : j--;
    if(j > m) { j--; i++; dir = false; }
    if(j < 1) { j++; i++; dir = true; }
  }
  putchar('\n');

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 3 3

Output

x
+
cmd
3 1 1 1 2 1 3
3 2 1 2 2 2 3
3 3 1 3 2 3 3
Advertisements

Demonstration


Codeforces Solution-Valera and Tubes-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+