#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int T,k; int n,a[25][25],gold=200; pii b[25][25]; int dx[]={0,0,-1,1,0},dy[]={-1,1,0,0,0}; ld gs(pii t) { ld aa=0; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(a[i][j]) aa=max(aa,a[i][j]/pow(abs(i-t.fi)+abs(j-t.se)+1,1.5)); return -aa; } void ez() { //1: regular farmer, 2: backing farmer, 3: suiciding farmer //at most one backing farmer at a time int cr=0; while(1) { ++cr; // cerr<<"gold="<<gold<<"?\n"; // for(int i=0;i<n;++i,cerr<<"\n") // for(int j=0;j<n;++j) // { // char c=0; // if(b[i][j].fi==1) c='F'; // else if(b[i][j].fi==2) c='G'; // else if(b[i][j].fi==3) c='H'; // if(!b[i][j].se&&c) c=c-'A'+'a'; // if(c) cerr<<c; // cerr<<a[i][j]<<" "; // } // cerr<<"+++\n"; // if(cr>400) break; bool board_emp=1,farm_emp=1; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { if(a[i][j]>0) board_emp=0; if(b[i][j].se>0) farm_emp=0; } // cerr<<board_emp<<"w"<<farm_emp<<"??\n"; if(board_emp&&farm_emp) break; queue<pii> Q; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(b[i][j].fi) Q.push(pii(i,j)); while(1) { if(gold>=100&&!b[0][0].fi) { gold-=100; b[0][0]=pii(1,0); Q.push(pii(0,0)); cout<<"R FARMER\n"; continue; } if(Q.empty()) break; pii s=Q.front(); Q.pop(); int t=(rand()&1)?2:0; pair<ld,int> mx(2e9,-1); for(int jj=0;jj<5;++jj) { int j=(jj==4)?4:(jj^t); pii w(s.fi+dx[j],s.se+dy[j]); if(w.fi>=0&&w.fi<n&& w.se>=0&&w.se<n);else continue; if(b[w.fi][w.se].fi&&j!=4) continue; ld scr=0; if(b[s.fi][s.se].fi==1) //norm scr=gs(w); else if(b[s.fi][s.se].fi==2) scr=w.fi+w.se; else scr=-(w.fi+w.se); mx=min(mx,mp(scr,j)); } // cerr<<s.fi<<","<<s.se<<":"<<mx.fi<<","<<mx.se<<"\n"; if(mx.se<0) continue; if(mx.se!=4) { pii w(s.fi+dx[mx.se], s.se+dy[mx.se]); b[w.fi][w.se]=b[s.fi][s.se]; b[s.fi][s.se]=pii(0,0); cout<<"M "<<s.fi<<" "<<s.se<<" "<<w.fi<<" "<<w.se<<endl; } } if(b[0][0].se>0) b[0][0].fi=1,gold+=b[0][0].se,b[0][0].se=0; if(board_emp&&b[0][0].fi) b[0][0].fi=3; int c2=0; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { if(!b[i][j].fi) continue; int u=min(10,a[i][j]); a[i][j]-=u,b[i][j].se+=u; if(b[i][j].fi==2) ++c2; } if(!c2) { pair<int,pii> mg(-1,pii(0,0)); for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(b[i][j].fi) mg=max(mg,mp(b[i][j].se,pii(i,j))); if((gold<100&&mg.fi+gold>=100)||board_emp) b[mg.se.fi][mg.se.se].fi=2; } cout<<"=\n"; } cout<<"===\n"; //cerr<<cr<<"?\n"; } void sol() { gold=200; memset(a,0,sizeof a); memset(b,0,sizeof b); cin>>n; bool nn=1; for(int i=0;i<n;++i) for(int j=0;j<n;++j) cin>>a[i][j],nn&=a[i][j]>=0; if(nn) { ez(); return; } } int main() { cin>>T>>k; while(T--) sol(); }
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 | #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int T,k; int n,a[25][25],gold=200; pii b[25][25]; int dx[]={0,0,-1,1,0},dy[]={-1,1,0,0,0}; ld gs(pii t) { ld aa=0; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(a[i][j]) aa=max(aa,a[i][j]/pow(abs(i-t.fi)+abs(j-t.se)+1,1.5)); return -aa; } void ez() { //1: regular farmer, 2: backing farmer, 3: suiciding farmer //at most one backing farmer at a time int cr=0; while(1) { ++cr; // cerr<<"gold="<<gold<<"?\n"; // for(int i=0;i<n;++i,cerr<<"\n") // for(int j=0;j<n;++j) // { // char c=0; // if(b[i][j].fi==1) c='F'; // else if(b[i][j].fi==2) c='G'; // else if(b[i][j].fi==3) c='H'; // if(!b[i][j].se&&c) c=c-'A'+'a'; // if(c) cerr<<c; // cerr<<a[i][j]<<" "; // } // cerr<<"+++\n"; // if(cr>400) break; bool board_emp=1,farm_emp=1; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { if(a[i][j]>0) board_emp=0; if(b[i][j].se>0) farm_emp=0; } // cerr<<board_emp<<"w"<<farm_emp<<"??\n"; if(board_emp&&farm_emp) break; queue<pii> Q; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(b[i][j].fi) Q.push(pii(i,j)); while(1) { if(gold>=100&&!b[0][0].fi) { gold-=100; b[0][0]=pii(1,0); Q.push(pii(0,0)); cout<<"R FARMER\n"; continue; } if(Q.empty()) break; pii s=Q.front(); Q.pop(); int t=(rand()&1)?2:0; pair<ld,int> mx(2e9,-1); for(int jj=0;jj<5;++jj) { int j=(jj==4)?4:(jj^t); pii w(s.fi+dx[j],s.se+dy[j]); if(w.fi>=0&&w.fi<n&& w.se>=0&&w.se<n);else continue; if(b[w.fi][w.se].fi&&j!=4) continue; ld scr=0; if(b[s.fi][s.se].fi==1) //norm scr=gs(w); else if(b[s.fi][s.se].fi==2) scr=w.fi+w.se; else scr=-(w.fi+w.se); mx=min(mx,mp(scr,j)); } // cerr<<s.fi<<","<<s.se<<":"<<mx.fi<<","<<mx.se<<"\n"; if(mx.se<0) continue; if(mx.se!=4) { pii w(s.fi+dx[mx.se], s.se+dy[mx.se]); b[w.fi][w.se]=b[s.fi][s.se]; b[s.fi][s.se]=pii(0,0); cout<<"M "<<s.fi<<" "<<s.se<<" "<<w.fi<<" "<<w.se<<endl; } } if(b[0][0].se>0) b[0][0].fi=1,gold+=b[0][0].se,b[0][0].se=0; if(board_emp&&b[0][0].fi) b[0][0].fi=3; int c2=0; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { if(!b[i][j].fi) continue; int u=min(10,a[i][j]); a[i][j]-=u,b[i][j].se+=u; if(b[i][j].fi==2) ++c2; } if(!c2) { pair<int,pii> mg(-1,pii(0,0)); for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(b[i][j].fi) mg=max(mg,mp(b[i][j].se,pii(i,j))); if((gold<100&&mg.fi+gold>=100)||board_emp) b[mg.se.fi][mg.se.se].fi=2; } cout<<"=\n"; } cout<<"===\n"; //cerr<<cr<<"?\n"; } void sol() { gold=200; memset(a,0,sizeof a); memset(b,0,sizeof b); cin>>n; bool nn=1; for(int i=0;i<n;++i) for(int j=0;j<n;++j) cin>>a[i][j],nn&=a[i][j]>=0; if(nn) { ez(); return; } } int main() { cin>>T>>k; while(T--) sol(); } |