#include<iostream> #include<vector> #include<cmath> using namespace std; int n; int m[100][100]; vector< pair<int,int> > farmers; vector< int > bags; void zero(int t[100][100]) { for (int i=0; i<n; ++i) { for (int j=0; j<n; ++j) { t[i][j] = 0; } } } int get_sumgold() { int sum = 0; for (int r=0; r<n; ++r) { for(int c=0; c<n; ++c) { sum += m[r][c]; } } return sum; } int get_sumbags() { int sumbags = 0; for (int i=0; i<bags.size(); ++i) sumbags += bags[i]; return sumbags; } bool is_free(int r, int c) { if (r<0 or c<0 or r>=n or c>=n) return false; for (int f=0; f<farmers.size(); ++f) { if (farmers[f].first==r and farmers[f].second==c) return false; } return true; } bool move(int f, int deltar, int deltac) { int r = farmers[f].first + deltar; int c = farmers[f].second + deltac; if (is_free(r, c) ){ cout<<"M "<<farmers[f].first<<" "<<farmers[f].second<<" "<<r<<" "<<c<<endl; farmers[f] = pair<int,int>(r,c); return true; } return false; } void random_move(int f){ if (rand()%2==0) { if (!move(f, +1, 0)) move(f, -1, 0); } else { if (!move(f, -1, 0)) move(f, +1, 0); } } void gra() { cin>>n; zero(m); farmers.clear(); bags.clear(); if (farmers.size()>0) cerr<<"[ERR] farmers.size()>0"<<endl; bool rocks=false; for (int r=0; r<n; ++r) { for(int c=0; c<n; ++c) { cin>>m[r][c]; rocks = rocks or (m[r][c]<0); } } if (rocks) { //if rocks skip game cout<<"==="<<endl; return; } ///////////////////////////////////////////////////////// int MAX_FARMERS = max(1, min((n/2),5)); int gold = 200; for(long e=0; e<100000000; ++e) { //jesli nie ma zlota na planszy i w plecakach int sumgold=get_sumgold(), sumbags=get_sumbags(); if (sumgold==0 and sumbags==0) { cout<<"="<<endl; cout<<"==="<<endl; return; } //kup farmera if (gold>=100 and farmers.size()<MAX_FARMERS and is_free(0,0)) { cout<<"R FARMER"<<endl; farmers.push_back(pair<int,int>(0,0)); bags.push_back(0); gold -= 100; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// int cols_per_farmer = ceil(float(n)/farmers.size()); for (int f=0; f<farmers.size(); ++f) { int fr = farmers[f].first; int fc = farmers[f].second; //kolumny farmera [first, end) int cf = (f*cols_per_farmer); int ce = (min((f+1)*cols_per_farmer, n)); //jest w bazie if (fr==0 and fc==0) { gold += bags[f]; //zostaw zloto w bazie bags[f] = 0; if (!move(f, 0, +1)) move(f, +1, 0); //wyjdz z bazy continue; } //ile zostalo zlota w obszarze tego farmera int remaining = 0; for(int c=cf; c<ce; ++c) { for (int r=0; r<n; ++r) { remaining += m[r][c]; } } //idz do bazy if ((bags[f]>0 and remaining==0) or (bags[f]>=100 and sumgold>500 and farmers.size()<MAX_FARMERS) ) { if (rand()%2==0) { if (!move(f, -1, 0)) { if (!move(f, 0, -1)) random_move(f); } } else { if (!move(f, 0, -1)) { if (!move(f, -1, 0)) random_move(f); } } continue; } //hopsaj zbierac else if (fc<cf) {//idz do siebie if (!move(f, 0, +1)) random_move(f); continue; } else if (fc>=ce) {//idz do siebie if (!move(f, 0, -1)) random_move(f); continue; } else { //jestes u siebie -> zbieraj if (m[fr][fc]>0) continue; //jest jakies zloto to sie nie ruszaj else { int gold_above = 0; for (int r=fr; r>=0; r--) gold_above+=m[r][fc]; int gold_below = 0; for (int r=fr; r<n; r++) gold_below+=m[r][fc]; int gold_left = 0; for (int c=fc; c>=cf; c--) for(int r=0; r<n; ++r) gold_left+=m[r][c]; int gold_right = 0; for (int c=fc; c<ce; c++) for(int r=0; r<n; ++r) gold_right+=m[r][c]; if (gold_above>0) { if (!move(f, -1, 0)) random_move(f); } else if (gold_below>0) { if (!move(f, +1, 0)) random_move(f); } else if (gold_left>0) { if (!move(f, 0, -1)) random_move(f); } else if (gold_right>0) { if (!move(f, 0, +1)) random_move(f); } else { random_move(f); } continue; } } }////////////////////////////////////////////////////////////////////////////////////////////////////////////// for (int f=0; f<farmers.size(); ++f) { //zaladuj zloto do plecaka int gain = min(m[farmers[f].first][farmers[f].second], 10); bags[f] += gain; m[farmers[f].first][farmers[f].second] -= gain; } cout<<"="<<endl; } } int main() { int T, k; cin>>T>>k; for (int t=0; t<T; ++t) { gra(); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include<iostream> #include<vector> #include<cmath> using namespace std; int n; int m[100][100]; vector< pair<int,int> > farmers; vector< int > bags; void zero(int t[100][100]) { for (int i=0; i<n; ++i) { for (int j=0; j<n; ++j) { t[i][j] = 0; } } } int get_sumgold() { int sum = 0; for (int r=0; r<n; ++r) { for(int c=0; c<n; ++c) { sum += m[r][c]; } } return sum; } int get_sumbags() { int sumbags = 0; for (int i=0; i<bags.size(); ++i) sumbags += bags[i]; return sumbags; } bool is_free(int r, int c) { if (r<0 or c<0 or r>=n or c>=n) return false; for (int f=0; f<farmers.size(); ++f) { if (farmers[f].first==r and farmers[f].second==c) return false; } return true; } bool move(int f, int deltar, int deltac) { int r = farmers[f].first + deltar; int c = farmers[f].second + deltac; if (is_free(r, c) ){ cout<<"M "<<farmers[f].first<<" "<<farmers[f].second<<" "<<r<<" "<<c<<endl; farmers[f] = pair<int,int>(r,c); return true; } return false; } void random_move(int f){ if (rand()%2==0) { if (!move(f, +1, 0)) move(f, -1, 0); } else { if (!move(f, -1, 0)) move(f, +1, 0); } } void gra() { cin>>n; zero(m); farmers.clear(); bags.clear(); if (farmers.size()>0) cerr<<"[ERR] farmers.size()>0"<<endl; bool rocks=false; for (int r=0; r<n; ++r) { for(int c=0; c<n; ++c) { cin>>m[r][c]; rocks = rocks or (m[r][c]<0); } } if (rocks) { //if rocks skip game cout<<"==="<<endl; return; } ///////////////////////////////////////////////////////// int MAX_FARMERS = max(1, min((n/2),5)); int gold = 200; for(long e=0; e<100000000; ++e) { //jesli nie ma zlota na planszy i w plecakach int sumgold=get_sumgold(), sumbags=get_sumbags(); if (sumgold==0 and sumbags==0) { cout<<"="<<endl; cout<<"==="<<endl; return; } //kup farmera if (gold>=100 and farmers.size()<MAX_FARMERS and is_free(0,0)) { cout<<"R FARMER"<<endl; farmers.push_back(pair<int,int>(0,0)); bags.push_back(0); gold -= 100; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// int cols_per_farmer = ceil(float(n)/farmers.size()); for (int f=0; f<farmers.size(); ++f) { int fr = farmers[f].first; int fc = farmers[f].second; //kolumny farmera [first, end) int cf = (f*cols_per_farmer); int ce = (min((f+1)*cols_per_farmer, n)); //jest w bazie if (fr==0 and fc==0) { gold += bags[f]; //zostaw zloto w bazie bags[f] = 0; if (!move(f, 0, +1)) move(f, +1, 0); //wyjdz z bazy continue; } //ile zostalo zlota w obszarze tego farmera int remaining = 0; for(int c=cf; c<ce; ++c) { for (int r=0; r<n; ++r) { remaining += m[r][c]; } } //idz do bazy if ((bags[f]>0 and remaining==0) or (bags[f]>=100 and sumgold>500 and farmers.size()<MAX_FARMERS) ) { if (rand()%2==0) { if (!move(f, -1, 0)) { if (!move(f, 0, -1)) random_move(f); } } else { if (!move(f, 0, -1)) { if (!move(f, -1, 0)) random_move(f); } } continue; } //hopsaj zbierac else if (fc<cf) {//idz do siebie if (!move(f, 0, +1)) random_move(f); continue; } else if (fc>=ce) {//idz do siebie if (!move(f, 0, -1)) random_move(f); continue; } else { //jestes u siebie -> zbieraj if (m[fr][fc]>0) continue; //jest jakies zloto to sie nie ruszaj else { int gold_above = 0; for (int r=fr; r>=0; r--) gold_above+=m[r][fc]; int gold_below = 0; for (int r=fr; r<n; r++) gold_below+=m[r][fc]; int gold_left = 0; for (int c=fc; c>=cf; c--) for(int r=0; r<n; ++r) gold_left+=m[r][c]; int gold_right = 0; for (int c=fc; c<ce; c++) for(int r=0; r<n; ++r) gold_right+=m[r][c]; if (gold_above>0) { if (!move(f, -1, 0)) random_move(f); } else if (gold_below>0) { if (!move(f, +1, 0)) random_move(f); } else if (gold_left>0) { if (!move(f, 0, -1)) random_move(f); } else if (gold_right>0) { if (!move(f, 0, +1)) random_move(f); } else { random_move(f); } continue; } } }////////////////////////////////////////////////////////////////////////////////////////////////////////////// for (int f=0; f<farmers.size(); ++f) { //zaladuj zloto do plecaka int gain = min(m[farmers[f].first][farmers[f].second], 10); bags[f] += gain; m[farmers[f].first][farmers[f].second] -= gain; } cout<<"="<<endl; } } int main() { int T, k; cin>>T>>k; for (int t=0; t<T; ++t) { gra(); } return 0; } |