貪心演算法,每條路徑最短2格,故前k-1步每次走2格,最後一步全走完
由於資料比較小,可以先打表
#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; typedef pair<int,int> Point; int main(){ int n, m, k, flag = -1; cin >> n >> m >>k; vector<Point> table; for(int i = 0 ; i < n ; ++i){ flag*=-1; for(int j = 0; j < m; ++ j){ table.push_back(Point(i,flag>0 ? j : (m-1-j))); } } for(int i = 0 ; i <2*(k -1); i+=2){ cout<<2<<" "<<table[i].first+1<<" "<<table[i].second+1<<" "<<table[i+1].first+1<<" "<<table[i+1].second+1<<endl; } cout<<m*n-2*(k-1); for(int i = 2*(k-1); i < m*n; ++ i ){ cout<<" "<<table[i].first+1<<" "<<table[i].second+1; } cout<<endl; }
關於額外的資訊, |xi - xi + 1| + |yi - yi + 1| = 1,其實就是一個點的四連通區域,即上下左右4個點