#include <iostream> #include <cstring> #include "message.h" #include "dzialka.h" using namespace std; struct field { unsigned long long hori; unsigned long long vert; unsigned long long sqrt; }map[75002][1002]; int usable_count = 0; int analyze_row_count = 1000; int analyze_columns_count = 1000; int width = 0; int height = 0; int myNodeId = 0; int nodeCount = 0; unsigned long long sum = 0; long sendData(int node, int col, int col_count, int row, int row_count) { int i, c; PutChar(node, 'r'); PutInt(node, col); PutInt(node, col_count); PutInt(node, row); PutInt(node, row_count); //cout<<"col="<<col<<" col_count="<<col_count<<endl; for(i = 0; i<row_count; i++) { //cout<<"hori="<<map[row+i][col_count].hori<<" vert="<<map[row+i][col_count].vert<<" sqrt="<<map[row+i][col_count].sqrt<<endl; PutLL(node, map[row+i][col_count].hori); PutLL(node, map[row+i][col_count].vert); PutLL(node, map[row+i][col_count].sqrt); } Send(node); return row+row_count; } void doCalc(int col, int col_count, int row, int row_count) { int sendNode = (myNodeId+1==nodeCount?1:myNodeId+1); int send_start = row, c, r, r_index, c_index; for(c=0; c<col_count && col+c<width; c++) { //cout<<"Col:"<<col+c<<endl; for(r=0; r<row_count; r++) { r_index = row+r; if(IsUsableCell(r_index, col+c)) { //cout<<"1"<<endl; map[r_index][c+1].hori = map[r_index][c].hori+1; if(0 == r_index) map[r_index][c+1].vert = 1; else map[r_index][c+1].vert = map[r_index-1][c+1].vert+1; if((r_index>0 && map[r_index-1][c].vert && map[r_index-1][c].vert) && map[r_index][c].hori) map[r_index][c+1].sqrt = map[r_index-1][c+1].sqrt+map[r_index][c].sqrt-map[r_index-1][c].sqrt+1; else map[r_index][c+1].sqrt = 0; //cout<<"doCalc: hori="<<map[r_index][c+1].hori<<" vert="<<map[r_index][c+1].vert<<" sqrt="<<map[r_index][c+1].sqrt<<endl; usable_count++; } else { //cout<<"0"<<endl; map[r_index][c+1].hori = map[r_index][c+1].vert = map[r_index][c+1].sqrt=0; } sum += map[r_index][c+1].hori+map[r_index][c+1].vert+map[r_index][c+1].sqrt; } } if(row+row_count == height) { PutChar(0, 'u'); PutInt(0, c); PutInt(0, usable_count); PutLL(0, sum); //cout<<"sum="<<sum<<endl; Send(0); sum = 0; usable_count = 0; } if(col+col_count < width) sendData(sendNode, col+col_count-1, col_count, send_start, row_count); } int main() { myNodeId = MyNodeId(); nodeCount = NumberOfNodes(); char ch; height = GetFieldHeight(); width = GetFieldWidth(); int i, rNode, col, col_count, row, row_count; //memset(map, 0, sizeof map); if(myNodeId>0) { for(i=0; i<height;i++) map[i][0].hori = map[i][0].vert = map[i][0].sqrt=0; if(analyze_row_count>height) analyze_row_count = height; analyze_columns_count = width/(nodeCount-1); if(0 == analyze_columns_count) analyze_columns_count = width; if(analyze_columns_count>1000) analyze_columns_count = 1000; if(1 == myNodeId) { for(i=0; i<height-analyze_row_count; i+=analyze_row_count) doCalc(0, analyze_columns_count, i, analyze_row_count); if(height-i>0) doCalc(0, analyze_columns_count, i, height-i); } while(true) { //cout<<"Wait"<<endl; rNode = Receive(-1); ch = GetChar(rNode); if('c' == ch) { //cout<<"Exit"<<endl; return 0; } col = GetInt(rNode); col_count = GetInt(rNode); row = GetInt(rNode); row_count = GetInt(rNode); for(int i = 0; i<row_count; i++) { map[row+i][0].hori = GetLL(rNode); map[row+i][0].vert = GetLL(rNode); map[row+i][0].sqrt = GetLL(rNode); } /*if(col+1+col_count>width) { col_count = width-(col+1); cout<<"col_count="<<col_count<<endl; }*/ doCalc(col+1, col_count, row, row_count); } } else /*Main instance*/ { long u, c; long usable_count = 0, cols = 0, chunk_sizes = 0; unsigned long long result = 0; while(true) { //cout<<"Wait"<<endl; rNode = Receive(-1); ch = GetChar(rNode); if(ch == 'u') { cols += GetInt(rNode); usable_count+=GetInt(rNode); result+=GetLL(rNode); //cout<<"U:"<<cols<<" "<<usable_count<<endl; }else { col_count = GetInt(rNode); row_count = GetInt(rNode); //cout<<"R:"<<col_count<<" "<<row_count<<endl; for(i=0; i<row_count; i++) { result += GetLL(rNode); result += GetLL(rNode); result += GetLL(rNode); } chunk_sizes+=row_count; } if(cols == width) { //cout<<"result="<<result<<" usable_count="<<usable_count<<endl; cout<<result-usable_count<<endl; for(i=0; i<nodeCount-1; i++) { PutChar(i+1, 'c'); Send(i+1); } return 0; } } } 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 212 213 214 215 216 217 218 219 220 221 222 | #include <iostream> #include <cstring> #include "message.h" #include "dzialka.h" using namespace std; struct field { unsigned long long hori; unsigned long long vert; unsigned long long sqrt; }map[75002][1002]; int usable_count = 0; int analyze_row_count = 1000; int analyze_columns_count = 1000; int width = 0; int height = 0; int myNodeId = 0; int nodeCount = 0; unsigned long long sum = 0; long sendData(int node, int col, int col_count, int row, int row_count) { int i, c; PutChar(node, 'r'); PutInt(node, col); PutInt(node, col_count); PutInt(node, row); PutInt(node, row_count); //cout<<"col="<<col<<" col_count="<<col_count<<endl; for(i = 0; i<row_count; i++) { //cout<<"hori="<<map[row+i][col_count].hori<<" vert="<<map[row+i][col_count].vert<<" sqrt="<<map[row+i][col_count].sqrt<<endl; PutLL(node, map[row+i][col_count].hori); PutLL(node, map[row+i][col_count].vert); PutLL(node, map[row+i][col_count].sqrt); } Send(node); return row+row_count; } void doCalc(int col, int col_count, int row, int row_count) { int sendNode = (myNodeId+1==nodeCount?1:myNodeId+1); int send_start = row, c, r, r_index, c_index; for(c=0; c<col_count && col+c<width; c++) { //cout<<"Col:"<<col+c<<endl; for(r=0; r<row_count; r++) { r_index = row+r; if(IsUsableCell(r_index, col+c)) { //cout<<"1"<<endl; map[r_index][c+1].hori = map[r_index][c].hori+1; if(0 == r_index) map[r_index][c+1].vert = 1; else map[r_index][c+1].vert = map[r_index-1][c+1].vert+1; if((r_index>0 && map[r_index-1][c].vert && map[r_index-1][c].vert) && map[r_index][c].hori) map[r_index][c+1].sqrt = map[r_index-1][c+1].sqrt+map[r_index][c].sqrt-map[r_index-1][c].sqrt+1; else map[r_index][c+1].sqrt = 0; //cout<<"doCalc: hori="<<map[r_index][c+1].hori<<" vert="<<map[r_index][c+1].vert<<" sqrt="<<map[r_index][c+1].sqrt<<endl; usable_count++; } else { //cout<<"0"<<endl; map[r_index][c+1].hori = map[r_index][c+1].vert = map[r_index][c+1].sqrt=0; } sum += map[r_index][c+1].hori+map[r_index][c+1].vert+map[r_index][c+1].sqrt; } } if(row+row_count == height) { PutChar(0, 'u'); PutInt(0, c); PutInt(0, usable_count); PutLL(0, sum); //cout<<"sum="<<sum<<endl; Send(0); sum = 0; usable_count = 0; } if(col+col_count < width) sendData(sendNode, col+col_count-1, col_count, send_start, row_count); } int main() { myNodeId = MyNodeId(); nodeCount = NumberOfNodes(); char ch; height = GetFieldHeight(); width = GetFieldWidth(); int i, rNode, col, col_count, row, row_count; //memset(map, 0, sizeof map); if(myNodeId>0) { for(i=0; i<height;i++) map[i][0].hori = map[i][0].vert = map[i][0].sqrt=0; if(analyze_row_count>height) analyze_row_count = height; analyze_columns_count = width/(nodeCount-1); if(0 == analyze_columns_count) analyze_columns_count = width; if(analyze_columns_count>1000) analyze_columns_count = 1000; if(1 == myNodeId) { for(i=0; i<height-analyze_row_count; i+=analyze_row_count) doCalc(0, analyze_columns_count, i, analyze_row_count); if(height-i>0) doCalc(0, analyze_columns_count, i, height-i); } while(true) { //cout<<"Wait"<<endl; rNode = Receive(-1); ch = GetChar(rNode); if('c' == ch) { //cout<<"Exit"<<endl; return 0; } col = GetInt(rNode); col_count = GetInt(rNode); row = GetInt(rNode); row_count = GetInt(rNode); for(int i = 0; i<row_count; i++) { map[row+i][0].hori = GetLL(rNode); map[row+i][0].vert = GetLL(rNode); map[row+i][0].sqrt = GetLL(rNode); } /*if(col+1+col_count>width) { col_count = width-(col+1); cout<<"col_count="<<col_count<<endl; }*/ doCalc(col+1, col_count, row, row_count); } } else /*Main instance*/ { long u, c; long usable_count = 0, cols = 0, chunk_sizes = 0; unsigned long long result = 0; while(true) { //cout<<"Wait"<<endl; rNode = Receive(-1); ch = GetChar(rNode); if(ch == 'u') { cols += GetInt(rNode); usable_count+=GetInt(rNode); result+=GetLL(rNode); //cout<<"U:"<<cols<<" "<<usable_count<<endl; }else { col_count = GetInt(rNode); row_count = GetInt(rNode); //cout<<"R:"<<col_count<<" "<<row_count<<endl; for(i=0; i<row_count; i++) { result += GetLL(rNode); result += GetLL(rNode); result += GetLL(rNode); } chunk_sizes+=row_count; } if(cols == width) { //cout<<"result="<<result<<" usable_count="<<usable_count<<endl; cout<<result-usable_count<<endl; for(i=0; i<nodeCount-1; i++) { PutChar(i+1, 'c'); Send(i+1); } return 0; } } } return 0; } |