Quick actions

cmd+k|ctrl+k

Navigation

Languages

Tax Collection

Snippet info

Language

Cpp

Visibility

public

Author

hungp.gostcode

Created

2020-06-29T09:42:32Z

Updated

2020-06-29T09:42:32Z

#include <bits/stdc++.h>
using namespace std;
// type
#define int long long
typedef long long ll;
typedef long double ld;
// loop
//#define For(i, l, r, x)     for (int i = l; i < r; i+=x)
//#define ForE(i, l, r, x)    for (int i = l; i <= r; i+=x)
//#define Ford(i, r, l)       for (int i = r; i > l; i--)
//#define FordE(i, r, l)      for (int i = r; i >= l; i--)
//#define Fora(i, a)          for (auto i : a)
// I/O 
#define openfile(file)      freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
#define testinput(file)     freopen(file".txt", "r", stdin); 
#define FAST_IO             std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//#define PrintV(a)           Fora(ii, a) cout << ii << ' '; cout << rl;
//#define PrintVl(a)          Fora(ii, a) cout << ii << rl;
//#define PrintA(a, l, r)     for (int ii = l; ii <= r; ii++) cout << a[ii] << ' '; cout << rl;
//#define PrintAl(a, l, r)    for (int ii = l; ii <= r; ii++) cout << a[ii] << rl;
//#define Ptest(x)            return cout << x, 0;
#define setpre(n)           fixed << setprecision(n)
// pair
#define F                   first
#define S                   second
#define pii                 pair<int, int>
#define pll                 pair<ll, ll>
#define pdd                 pair<ld, ld>
// vector & !!?(string)
#define eb                  emplace_back
#define pb                  push_back
#define all(a)              a.begin(), a.end()
#define rall(a)             a.rbegin(), a.rend()
#define sz(a)               a.size()
#define len(a)              a.length()
// geometry calc    
#define pi                  acos(-1.0)
#define g_sin(a)            sin(a*pi/180)
#define g_cos(a)            cos(a*pi/180)
#define g_tan(a)            tan(a*pi/180)
// set val
#define ms0(a)              memset(a,        0, sizeof(a));
#define ms1(a)              memset(a,        1, sizeof(a));
#define msn1(a)             memset(a,       -1, sizeof(a));
#define msinf(a)            memset(a, 0x3f3f3f, sizeof(a));
// constant
const int mod1 = 998244353, mod = 1e9+7;
const int MAXN = 1000005, MAXM = 200010;
// code
int grid[303][303];
int arr[303*303];
pii ans[303*303];
void Solve(){
    int n, m;
    cin >> n >> m;
    if (n % 2 == 1 && m % 2 == 1){
        cout << -1;
        return ;
    }
    
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cin >> grid[i][j];
        }
    }
    int cnt = 0;
    for (int i = 1; i <= m; i++){
        arr[cnt] = grid[1][i];
        ans[cnt++] = make_pair(1, i);
    }
    for (int i = 2; i <= n; i++){
        arr[cnt] = grid[i][m];
        ans[cnt++] = make_pair(i, m);
    }
    if (n % 2 == 0){
        for (int i = n; i > 1; i--){
            if (i % 2 == 0){
                for (int j = m-1; j >= 1; j--){
                    arr[cnt] = grid[i][j];
                    ans[cnt++] = make_pair(i, j);
                }
            }else{
                for (int j = 1; j < m; j++){
                    arr[cnt] = grid[i][j];
                    ans[cnt++] = make_pair(i, j);
                }
            }
        }
    }else{
        for (int i = m-1; i >= 1; i--){
            if (i % 2 == 1){
                for (int j = n; j > 1; j--){
                    arr[cnt] = grid[j][i];
                    ans[cnt++] = make_pair(j, i);
                }
            }else{
                for (int j = 2; j <= n; j++){
                    arr[cnt] = grid[j][i];
                    ans[cnt++] = make_pair(j, i);
                }
            }
        }
    }
    int mx = 0;
    for (int i = 1; i < cnt; i++){
        arr[i] += arr[i-1];
        if (arr[i] < arr[mx])
            mx = i;
    }
    if (arr[cnt-1] < 0){
        cout << -1;
        return;
    }
    mx = (mx+1)%cnt;
    //cout << mx;
    for (int i = 0; i < cnt; i++)
        cout << ans[(i+mx)%cnt].F << ' ' << ans[(i+mx)%cnt].S << endl;
}

signed main(){
    FAST_IO;
    int TC = 1;
	//cin >> TC;
    while(TC--) Solve();
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    return 0;
}
INFO