/* * DZI * Autor: Szymon Tur */ #include <iostream> #include "message.h" #include "dzialka.h" using namespace std; int lk(int a) { return a*(a+1)/2; } long long wyn=0; void succ(int start, int end, bool poziomo) { int l=0; if (poziomo) for (int i=start;i<=end;i++) { for (int j=0;j<GetFieldWidth();j++) { if (IsUsableCell(i,j)) ++l; else { wyn+=lk(l); l=0; } } wyn+=lk(l); l=0; } else for (int j=start;j<=end;j++) { for (int i=0;i<GetFieldHeight();i++) { if (IsUsableCell(i,j)) ++l; else { if (l>1) wyn+=lk(l)-l; l=0; } } if (l>1) wyn+=lk(l)-l; l=0; } } void fiveLayersOfIrony(int start, int end, int bok, bool poziomo) { int l=0,ppap=1; if (poziomo) for (int i=start;i<=end-bok+1;i++) { for (int j=0;j<GetFieldWidth();j++) { ppap=1; for (int mn=0;mn<bok;mn++) ppap *= IsUsableCell(i+mn,j); if (ppap==1) ++l; else { if (l>bok) wyn+=lk(l)-lk(bok); l=0; } } if (l>bok) wyn+=lk(l)-lk(bok); l=0; } else for (int j=start;j<=end-bok+1;j++) { for (int i=0;i<GetFieldHeight();i++) { ppap=1; for (int mn=0;mn<bok;mn++) ppap *= IsUsableCell(i,j+mn); if (ppap==1) ++l; else { if (l>1) wyn+=lk(l)-l; l=0; } } if (l>1) wyn+=lk(l)-l; l=0; } } int main() { int w = GetFieldWidth(),h=GetFieldHeight(), start,end,bok; switch (MyNodeId()) { case 0: succ(0,h/2,true); break; case 1: succ(h/2+1,h-1,true); break; case 2: succ(0,w/2,false); break; case 3: succ(w/2+1,w-1,false); break; } if (MyNodeId()>3) { int myi=MyNodeId(),non = NumberOfNodes(); if (myi<(non+4)/2) { for (int i=myi-2;i<=w;i+=(non-4)/2) { start = 0; end = w-1; bok = i; fiveLayersOfIrony(start,end,bok,false); } } else { for (int i = myi-non/2;i<=h;i+=(non-4)/2) { start = 0; end = h-1; bok = i; fiveLayersOfIrony(start,end,bok,true); } } } if (MyNodeId()>0) { PutLL(0,wyn); Send(0); } else { for (int i=1;i<NumberOfNodes();i++) { Receive(i); wyn+=GetLL(i); } cout << wyn; } }
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 | /* * DZI * Autor: Szymon Tur */ #include <iostream> #include "message.h" #include "dzialka.h" using namespace std; int lk(int a) { return a*(a+1)/2; } long long wyn=0; void succ(int start, int end, bool poziomo) { int l=0; if (poziomo) for (int i=start;i<=end;i++) { for (int j=0;j<GetFieldWidth();j++) { if (IsUsableCell(i,j)) ++l; else { wyn+=lk(l); l=0; } } wyn+=lk(l); l=0; } else for (int j=start;j<=end;j++) { for (int i=0;i<GetFieldHeight();i++) { if (IsUsableCell(i,j)) ++l; else { if (l>1) wyn+=lk(l)-l; l=0; } } if (l>1) wyn+=lk(l)-l; l=0; } } void fiveLayersOfIrony(int start, int end, int bok, bool poziomo) { int l=0,ppap=1; if (poziomo) for (int i=start;i<=end-bok+1;i++) { for (int j=0;j<GetFieldWidth();j++) { ppap=1; for (int mn=0;mn<bok;mn++) ppap *= IsUsableCell(i+mn,j); if (ppap==1) ++l; else { if (l>bok) wyn+=lk(l)-lk(bok); l=0; } } if (l>bok) wyn+=lk(l)-lk(bok); l=0; } else for (int j=start;j<=end-bok+1;j++) { for (int i=0;i<GetFieldHeight();i++) { ppap=1; for (int mn=0;mn<bok;mn++) ppap *= IsUsableCell(i,j+mn); if (ppap==1) ++l; else { if (l>1) wyn+=lk(l)-l; l=0; } } if (l>1) wyn+=lk(l)-l; l=0; } } int main() { int w = GetFieldWidth(),h=GetFieldHeight(), start,end,bok; switch (MyNodeId()) { case 0: succ(0,h/2,true); break; case 1: succ(h/2+1,h-1,true); break; case 2: succ(0,w/2,false); break; case 3: succ(w/2+1,w-1,false); break; } if (MyNodeId()>3) { int myi=MyNodeId(),non = NumberOfNodes(); if (myi<(non+4)/2) { for (int i=myi-2;i<=w;i+=(non-4)/2) { start = 0; end = w-1; bok = i; fiveLayersOfIrony(start,end,bok,false); } } else { for (int i = myi-non/2;i<=h;i+=(non-4)/2) { start = 0; end = h-1; bok = i; fiveLayersOfIrony(start,end,bok,true); } } } if (MyNodeId()>0) { PutLL(0,wyn); Send(0); } else { for (int i=1;i<NumberOfNodes();i++) { Receive(i); wyn+=GetLL(i); } cout << wyn; } } |