#include <iostream> #include <cmath> using namespace std; int main() { ios_base::sync_with_stdio(0); int t,k; int tab[20][20]; int farmerx = 0; int farmery = 0; int celx = 0; int cely = 0; cin>>t>>k; for(int i=0;i<t;i++){ farmerx = 0; farmery = 0; celx=0; cely=0; int n; cin>>n; for(int i3=0;i3<n;i3++){ for(int i4=0;i4<n;i4++){ cin>>tab[i3][i4]; } } cout<<"R FARMER\n"; while(true){ celx=0; cely=0; for(int i3=0;i3<n;i3++){ for(int i4=0;i4<n;i4++){ if(tab[i3][i4] > 0){ if(celx == 0 && cely == 0) { celx = i3; cely = i4; } else{ if(abs(celx - farmerx) + abs(cely - farmery) > abs(i3 - farmerx) + abs(i4 - farmery)){ celx = i3; cely = i4; } } } } } if(celx == 0 && cely == 0){ while(farmerx != 0){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx-1<<" "<<farmery<<"\n=\n"; farmerx--; } while(farmery != 0){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery-1<<"\n=\n"; farmery--; } cout<<"=\n"; cout<<"==="; break; } while(celx < farmerx){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx-1<<" "<<farmery<<"\n=\n"; farmerx--; } while(celx > farmerx){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx+1<<" "<<farmery<<"\n=\n"; farmerx++; } while(cely < farmery){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery-1<<"\n=\n"; farmery--; } while(cely > farmery){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery+1<<"\n=\n"; farmery++; } while(tab[farmerx][farmery] > 0){ cout<<"=\n"; tab[farmerx][farmery] -= 10; } } } 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 | #include <iostream> #include <cmath> using namespace std; int main() { ios_base::sync_with_stdio(0); int t,k; int tab[20][20]; int farmerx = 0; int farmery = 0; int celx = 0; int cely = 0; cin>>t>>k; for(int i=0;i<t;i++){ farmerx = 0; farmery = 0; celx=0; cely=0; int n; cin>>n; for(int i3=0;i3<n;i3++){ for(int i4=0;i4<n;i4++){ cin>>tab[i3][i4]; } } cout<<"R FARMER\n"; while(true){ celx=0; cely=0; for(int i3=0;i3<n;i3++){ for(int i4=0;i4<n;i4++){ if(tab[i3][i4] > 0){ if(celx == 0 && cely == 0) { celx = i3; cely = i4; } else{ if(abs(celx - farmerx) + abs(cely - farmery) > abs(i3 - farmerx) + abs(i4 - farmery)){ celx = i3; cely = i4; } } } } } if(celx == 0 && cely == 0){ while(farmerx != 0){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx-1<<" "<<farmery<<"\n=\n"; farmerx--; } while(farmery != 0){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery-1<<"\n=\n"; farmery--; } cout<<"=\n"; cout<<"==="; break; } while(celx < farmerx){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx-1<<" "<<farmery<<"\n=\n"; farmerx--; } while(celx > farmerx){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx+1<<" "<<farmery<<"\n=\n"; farmerx++; } while(cely < farmery){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery-1<<"\n=\n"; farmery--; } while(cely > farmery){ cout<<"M "<<farmerx<<" "<<farmery<<" "<<farmerx<<" "<<farmery+1<<"\n=\n"; farmery++; } while(tab[farmerx][farmery] > 0){ cout<<"=\n"; tab[farmerx][farmery] -= 10; } } } return 0; } |