fork(3) download
  1. /* BDFHJK
  2.  * Version : trunk
  3.  * Wed, 15 Jun 2011 19:58:49 +0200 */
  4.  
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<string>
  8. #include<list>
  9. #include<deque>
  10. #include<map>
  11. #include<set>
  12. #include<queue>
  13. #include<stack>
  14. #include<utility>
  15. #include<sstream>
  16. #include<cstring>
  17. #include<cmath>
  18. #include<cstdlib>
  19. #include<algorithm>
  20. #include<vector>
  21. #include<sys/time.h>
  22.  
  23. ///Train mode
  24. #include<fstream>
  25.  
  26. using namespace std;
  27.  
  28. //#define DEBUG 1
  29. #define NORMAL 1
  30. //#define TRAIN 1
  31. //#define VISUAL 1
  32.  
  33. #define DB(X) printf("Debug: %d\n", X);
  34.  
  35. #define REP(I,N) for(int I=0;I<(N);I++)
  36. #define FOR(I,A,B) for(int I=(A);I<=(B);I++)
  37. #define FORD(I,A,B) for(int I=(A);I>=(B);I--)
  38. #define ALL(X) (X).begin(),(X).end()
  39. #define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)
  40.  
  41. #define MOD ((int)1e9+7)
  42. #define INF ((int)1e9+7)
  43. #define mINF ((int)1e6+7)
  44. #define EPS 1e-1
  45. #define PI ((LD)3.141592654)
  46.  
  47. #define VAR(V,init) __typeof(init) V=(init)
  48. #define SIZE(X) X.size()
  49.  
  50. #define PB push_back
  51. #define MP make_pair
  52. #define FI first
  53. #define SE second
  54.  
  55. typedef vector<int> VI;
  56. typedef pair<int,int> PII;
  57. typedef long long LL;
  58. typedef vector<string> VS;
  59. typedef long double LD;
  60.  
  61. LL nwd(LL a, LL b) { return !b?a:nwd(b,a%b); }
  62.  
  63. double czas;
  64.  
  65. static inline double gT2(){
  66. timeval tv;
  67. gettimeofday(&tv, 0);
  68. return tv.tv_sec + tv.tv_usec * 1e-6;
  69. }
  70.  
  71. static double startTime;
  72.  
  73. static inline void initTime(){
  74. startTime = gT2();
  75. }
  76.  
  77. static inline bool isTimeup() {
  78. return czas <= gT2() - startTime;
  79. }
  80.  
  81.  
  82. VS R;
  83. unsigned char P[2000][2000];
  84. int W, H;
  85. string name;
  86. bool used[1500][1500];
  87. bool crat[1500][1500];
  88. int kwadrat_size;
  89. int kwadrat_sum;
  90. int light;
  91. int frag_light;
  92. int lights[20][20];
  93. int TM;
  94. int GxM[3][3];
  95. int GyM[3][3];
  96. int gM[5][5];
  97. unsigned char PE[20][1500][1500]; ///20*2*2*MB=80MB
  98. int eD[1500][1500];
  99. double grd[1500][1500];
  100. int uTh;
  101. int lTh;
  102. int UT;
  103.  
  104. int ZA;
  105.  
  106. typedef struct
  107. {
  108. string name;
  109. int minx;
  110. int maxx;
  111. int miny;
  112. int maxy;
  113. int pole;
  114. int probe;
  115. int algo;
  116. int cx;
  117. int cy;
  118. LD ratio2; ///Circle to center frag from algo 2
  119. LD frag; ///Black pixels frag
  120. LD crater; ///Num of black pixels
  121. LD black; ///Num of more than black pixels
  122. LD ultrablack;
  123. LD differ; ///Differ of black pixels
  124. LD sldp; ///Left to right sum of pixels
  125. LD score;
  126. LD ratio; ///Center to circle diff from algo 1
  127. LD fraci; ///Frag on circle from calc
  128. LD cendiff;
  129. LD corndiff;
  130. LD cenfra;
  131. LD cornfra;
  132. LD frd;
  133. LD rzp; ///Rozproszenie
  134. LD cratincircle;
  135. LD bestcirc;
  136. vector<int> fragmenty;
  137. bool md;
  138. bool uniq;
  139. } rectangle;
  140.  
  141. typedef struct
  142. {
  143. string name;
  144. bool used;
  145. vector<rectangle> rectangles;
  146. } answer;
  147. vector<answer> answers;
  148. vector<rectangle> AA;
  149. vector<rectangle> BB;
  150. vector<rectangle> CC;
  151.  
  152. struct crcmp
  153. {
  154. bool operator () (const rectangle &l, const rectangle &p) const
  155. {
  156. if (l.score < p.score) return true;
  157. else return false;
  158. }
  159. } crc;
  160.  
  161. int ZI;
  162.  
  163. class CraterDetection
  164. {
  165. public:
  166.  
  167. bool overlap(rectangle a, rectangle b)
  168. {
  169. int xl, xr, yt, yb, arA, arB, iA, uA;
  170. arA = (a.maxx - a.minx + 1) * (a.maxy - a.miny + 1);
  171. arB = (b.maxx - b.minx + 1) * (b.maxy - b.miny + 1);
  172. xl = max(a.minx, b.minx);
  173. xr = min(a.maxx, b.maxx);
  174. yt = max(a.miny, b.miny);
  175. yb = min(a.maxy, b.maxy);
  176. iA = (xl <= xr && yt <= yb ? (xr - xl + 1) * (yb - yt + 1) : 0);
  177. uA = arA + arB - iA;
  178.  
  179. return 10 * iA > 3 * uA;
  180. }
  181. void calc_answers()
  182. {
  183. int anscnt = 0;
  184. int goodans = 0;
  185. int badans = 0;
  186. int unique = 0;
  187. int uniqueTM0 = 0;
  188. int uniqueTM1 = 0;
  189. int uniqueTM2 = 0;
  190. int uniqueTM3 = 0;
  191. int uniqueTM4 = 0;
  192. int uniqueTM5 = 0;
  193. int uniqueTM6 = 0;
  194. int uniqueTM7 = 0;
  195. int anTM[11];
  196. int negTM[11];
  197. int good[900];
  198. int bad[900];
  199.  
  200. memset(anTM, 0, sizeof(anTM));
  201. memset(negTM, 0, sizeof(negTM));
  202. memset(good, 0, sizeof(good));
  203. memset(bad, 0, sizeof(bad));
  204.  
  205.  
  206.  
  207. REP(i, AA.size())
  208. {
  209. bool pass = 0;
  210. REP(j, answers.size()) REP(k, answers[j].rectangles.size())
  211. {
  212.  
  213. if (AA[i].name == answers[j].name)
  214. {
  215. if (overlap(AA[i], answers[j].rectangles[k]))
  216. {
  217. if (pass == 0)
  218. {
  219. anTM[AA[i].probe]++;
  220. goodans++;
  221. pass = 1;
  222. AA[i].md = 1;
  223. }
  224. if (answers[j].rectangles[k].md == 0)
  225. {
  226. good[i/100]++;
  227. AA[i].uniq = 1;
  228. answers[j].rectangles[k].md = 1;
  229. unique++;
  230. if (AA[i].probe == 0) uniqueTM0++;
  231. if (AA[i].probe == 1) uniqueTM1++;
  232. if (AA[i].probe == 2) uniqueTM2++;
  233. if (AA[i].probe == 3) uniqueTM3++;
  234. if (AA[i].probe == 4) uniqueTM4++;
  235. if (AA[i].probe == 5) uniqueTM5++;
  236. if (AA[i].probe == 6) uniqueTM6++;
  237. if (AA[i].probe == 7) uniqueTM7++;
  238. }
  239.  
  240. }else
  241. {
  242.  
  243. }
  244. }
  245. }
  246.  
  247. if (pass == 0)
  248. {
  249. negTM[AA[i].probe]++;
  250. badans++;
  251. bad[i/100]++;
  252. }
  253. }
  254.  
  255. cerr << "----------------CRATERS FOUND------------" << endl;
  256.  
  257. int zak = 0;
  258. FOR(i, 0, AA.size()-1)
  259. {
  260. if (zak < 100)
  261. {
  262. if (AA[i].md == 0)
  263. {
  264. cerr << "*FAUL*" << endl;
  265. }else if (AA[i].uniq == 0) cerr << "*REPEAT*" << endl;
  266. cerr << AA[i].name << " ==> SCORE = " << AA[i].score << " NX = " << AA[i].minx << " MX = " << AA[i].maxx << " NY = " << AA[i].miny << " MY " << AA[i].maxy << endl;
  267. cerr << "FRAG = " << AA[i].frag << " DIFF = " << AA[i].differ << " CRATER = " << AA[i].crater << " SLDP = " << AA[i].sldp << " RATIO = " << AA[i].ratio << " FRACI = " << AA[i].fraci << endl;
  268. cerr << "CORNDIFF = " << AA[i].corndiff << " CENTEDIFF = " << AA[i].cendiff << " CENFRA = " << AA[i].cenfra << " CORNFRA = " << AA[i].cornfra << " FRD = " << AA[i].frd << endl;
  269. cerr << "BLACK = " << AA[i].black << " ULTRABLACK = " << AA[i].ultrablack << " RZP = " << AA[i].rzp << " CX = " << AA[i].cx << " CY = " << AA[i].cy << " CICR = " << AA[i].cratincircle << " MXCIR = " << AA[i].bestcirc;
  270. if (AA[i].fragmenty.size() > 0)
  271. {
  272. REP(f, AA[i].fragmenty.size()) cerr << " F" << f << "= " << AA[i].fragmenty[f];
  273. }
  274. cerr << endl;
  275. cerr << endl;
  276. zak++;
  277. }
  278. }
  279.  
  280. cerr << "-----------------CRATERS NOT FOUND--------------" << endl;
  281.  
  282. zak = 0;
  283. REP(j, answers.size()) REP(k, answers[j].rectangles.size())
  284. {
  285. if (zak > 100 || answers[j].used==0) continue;
  286. if (answers[j].rectangles[k].md == 0)
  287. {
  288. rectangle RC = answers[j].rectangles[k];
  289. cerr << "NAME = " << answers[j].name << " NX = " << RC.minx << " MX = " << RC.maxx << " NY = " << RC.miny << " MY " << RC.maxy << endl;
  290. cerr << endl;
  291. zak++;
  292. }
  293. }
  294.  
  295. cerr << endl;
  296.  
  297.  
  298. cerr << "--------------------STAT------------------------" << endl;
  299. REP(i, AA.size()/100)
  300. {
  301. double gb = (bad[i]==0) ? 0 : ((LD)good[i]/(LD)bad[i]);
  302. cerr << "ZAKR = " << (i*100) << " - " << ((i+1)*100) << " SCORES " << AA[i*100].score << " RATING = " << gb << endl;
  303. }
  304.  
  305.  
  306. cerr << "anscnt = " << anscnt << " goodans = " << goodans << " badans = " << badans << " unique = " << unique << endl;
  307. REP(i, 10) cerr << "ANS[" << i << "]= " << anTM[i] << "/" << negTM[i] << endl;
  308. cerr << "UTM0 = " << uniqueTM0 << " UTM1 = " << uniqueTM1 << " UTM2 = " << uniqueTM2 <<" UTM3 = " << uniqueTM3 <<" UTM4 = " << uniqueTM4 << " UTM5 = " << uniqueTM5 << " UTM6 = " << uniqueTM6 << " UTM7 = " << uniqueTM7 << endl;
  309.  
  310. }
  311.  
  312. ///----------------------------------------------------------------------------------------
  313.  
  314. int init()
  315. {
  316.  
  317. light = 0;
  318. frag_light = 0;
  319. R.clear();
  320. AA.clear();
  321. memset(P, 0, sizeof(P));
  322.  
  323. gauss_init();
  324.  
  325. #ifdef TRAIN
  326. train_init();
  327. #endif
  328.  
  329. #ifdef DEBUG
  330. train_init();
  331. #endif
  332.  
  333.  
  334. return 0;
  335. }
  336.  
  337.  
  338. vector <string> getCraters()
  339. {
  340. sort_answers();
  341. REP(i, BB.size())
  342. {
  343. if (AA.size() < 95000) AA.PB(BB[i]); else break;
  344. }
  345. REP(i, CC.size())
  346. {
  347. if (AA.size() < 95000) AA.PB(CC[i]); else break;
  348. }
  349.  
  350. make_answers();
  351.  
  352. #ifdef DEBUG
  353. calc_answers();
  354. sleep(1);
  355. #endif
  356.  
  357. return R;
  358. }
  359.  
  360.  
  361. int processImage(string _name, int _W, int _H, vector <int> data)
  362. {
  363. W = _W;
  364. H = _H;
  365. name = _name;
  366. make_array(data);
  367. make_edges();
  368.  
  369. if (name[name.size()-3] == 't') czas = 170.0;
  370. else czas = 25.0;
  371.  
  372. initTime();
  373.  
  374. #ifdef DEBUG
  375. REP(i, answers.size())
  376. {
  377. if (answers[i].name == name) answers[i].used = 1;
  378. }
  379. #endif
  380.  
  381. #ifdef TRAIN
  382. train(name);
  383. #endif
  384.  
  385. #ifdef VISUAL
  386. present();
  387. sleep(1);
  388. #endif
  389.  
  390. #ifdef NORMAL
  391. ZA = 0;
  392. bool start=0;
  393.  
  394. REP(i, 8)
  395. {
  396. TM = i;
  397. algo1(name);
  398. if (i==4 && ZA < 500) start=1;
  399. }
  400.  
  401. if (start && name[name.size()-3] == 't')
  402. {
  403. algo2(name, 90, 2.55, 20000);
  404. } else
  405. if (ZA > 25000 && name[name.size()-3] == 't')
  406. {
  407. REP(i, AA.size())
  408. {
  409. if (AA[i].name == name)
  410. {
  411. AA[i].score += (int)3e7;
  412. }
  413. }
  414. algo2(name, 150, 6.0, 20000);
  415. } else
  416. if (ZA > 5000 && name[name.size()-3] == 't')
  417. {
  418. algo2(name, 170, 5.0, 150);
  419. }
  420.  
  421. for(int y = 0; y <= H-21; y+= 20) for(int x = 0; x <= W-21; x+=20)
  422. {
  423. rectangle RC;
  424. RC.name = name;
  425. RC.miny = y;
  426. RC.minx = x;
  427. RC.maxy = y+20;
  428. RC.maxx = x+20;
  429. RC.score = (int)1e9;
  430. RC.probe = 9;
  431. RC.algo = 1;
  432. BB.PB(RC);
  433. }
  434. for(int y = 10; y <= H-21; y+= 20) for(int x = 10; x <= W-21; x+=20)
  435. {
  436. rectangle RC;
  437. RC.name = name;
  438. RC.miny = y;
  439. RC.minx = x;
  440. RC.maxy = y+20;
  441. RC.maxx = x+20;
  442. RC.score = (int)1e9;
  443. RC.probe = 9;
  444. RC.algo = 1;
  445. BB.PB(RC);
  446. }
  447. if (name[name.size()-3] == 't') for(int y = 0; y <= H-11; y+= 10) for(int x = 0; x <= W-11; x+=10)
  448. {
  449. rectangle RC;
  450. RC.name = name;
  451. RC.miny = y;
  452. RC.minx = x;
  453. RC.maxy = y+10;
  454. RC.maxx = x+10;
  455. RC.score = (int)1e9;
  456. RC.probe = 9;
  457. RC.algo = 1;
  458. CC.PB(RC);
  459. }
  460. if (name[name.size()-3] == 't') for(int y = 5; y <= H-11; y+= 10) for(int x = 5; x <= W-11; x+=10)
  461. {
  462. rectangle RC;
  463. RC.name = name;
  464. RC.miny = y;
  465. RC.minx = x;
  466. RC.maxy = y+10;
  467. RC.maxx = x+10;
  468. RC.score = (int)1e9;
  469. RC.probe = 9;
  470. RC.algo = 1;
  471. CC.PB(RC);
  472. }
  473. for(int y = 0; y <= H-31; y+= 30) for(int x = 0; x <= W-31; x+=30)
  474. {
  475. rectangle RC;
  476. RC.name = name;
  477. RC.miny = y;
  478. RC.minx = x;
  479. RC.maxy = y+30;
  480. RC.maxx = x+30;
  481. RC.score = (int)1e9;
  482. RC.probe = 9;
  483. RC.algo = 1;
  484. CC.PB(RC);
  485. }
  486. for(int y = 0; y <= H-61; y+= 60) for(int x = 0; x <= W-61; x+=60)
  487. {
  488. rectangle RC;
  489. RC.name = name;
  490. RC.miny = y;
  491. RC.minx = x;
  492. RC.maxy = y+60;
  493. RC.maxx = x+60;
  494. RC.score = (int)1e9;
  495. RC.probe = 9;
  496. RC.algo = 1;
  497. CC.PB(RC);
  498. }
  499. for(int y = 30; y <= H-61; y+= 60) for(int x = 30; x <= W-61; x+=60)
  500. {
  501. rectangle RC;
  502. RC.name = name;
  503. RC.miny = y;
  504. RC.minx = x;
  505. RC.maxy = y+60;
  506. RC.maxx = x+60;
  507. RC.score = (int)1e9;
  508. RC.probe = 9;
  509. RC.algo = 1;
  510. CC.PB(RC);
  511. }
  512. for(int y = 10; y <= H-41; y+= 40) for(int x = 10; x <= W-41; x+=40)
  513. {
  514. rectangle RC;
  515. RC.name = name;
  516. RC.miny = y;
  517. RC.minx = x;
  518. RC.maxy = y+40;
  519. RC.maxx = x+40;
  520. RC.score = (int)1e9;
  521. RC.probe = 9;
  522. RC.algo = 1;
  523. BB.PB(RC);
  524. }
  525. for(int y = 10; y <= H-81; y+= 80) for(int x = 10; x <= W-81; x+=80)
  526. {
  527. rectangle RC;
  528. RC.name = name;
  529. RC.miny = y;
  530. RC.minx = x;
  531. RC.maxy = y+80;
  532. RC.maxx = x+80;
  533. RC.score = (int)1e9;
  534. RC.probe = 9;
  535. RC.algo = 1;
  536. BB.PB(RC);
  537. }
  538. for(int y = 40; y <= H-81; y+= 80) for(int x = 40; x <= W-81; x+=80)
  539. {
  540. rectangle RC;
  541. RC.name = name;
  542. RC.miny = y;
  543. RC.minx = x;
  544. RC.maxy = y+80;
  545. RC.maxx = x+80;
  546. RC.score = (int)1e9;
  547. RC.probe = 9;
  548. RC.algo = 1;
  549. BB.PB(RC);
  550. }
  551.  
  552. #endif
  553.  
  554. return 0;
  555. }
  556.  
  557.  
  558. ///----------------------------------------------------------------------------------------------
  559.  
  560. void gauss_init()
  561. {
  562. GxM[0][0] = -1; GxM[0][1] = 0; GxM[0][2] = 1;
  563. GxM[1][0] = -2; GxM[1][1] = 0; GxM[1][2] = 2;
  564. GxM[2][0] = -1; GxM[2][1] = 0; GxM[2][2] = 1;
  565.  
  566. GyM[0][0] = 1; GyM[0][1] = 2; GyM[0][2] = 1;
  567. GyM[1][0] = 0; GyM[1][1] = 0; GyM[1][2] = 0;
  568. GyM[2][0] = -1; GyM[2][1] = -2; GyM[2][2] = -1;
  569.  
  570. gM[0][0] = 2; gM[0][1] = 4; gM[0][2] = 5; gM[0][3] = 4; gM[0][4] = 2;
  571. gM[1][0] = 4; gM[1][1] = 9; gM[1][2] = 12; gM[1][3] = 9; gM[1][4] = 4;
  572. gM[2][0] = 5; gM[2][1] = 12; gM[2][2] = 15; gM[2][3] = 12; gM[2][4] = 2;
  573. gM[3][0] = 4; gM[3][1] = 9; gM[3][2] = 12; gM[3][3] = 9; gM[3][4] = 4;
  574. gM[4][0] = 2; gM[4][1] = 4; gM[4][2] = 5; gM[4][3] = 4; gM[4][4] = 2;
  575. }
  576.  
  577.  
  578. void gaussian_blur()
  579. {
  580. memset(PE[UT], 0, sizeof(PE[UT]));
  581. FOR(y, 2, H-3) FOR(x, 2, W-3)
  582. {
  583. int np = 0;
  584. FOR (yoff, -2, 2) FOR(xoff, -2, 2)
  585. {
  586. np += P[y+yoff][x+xoff] * gM[2 + yoff][2 + xoff];
  587. }
  588. PE[UT][y][x] = np/159;
  589. }
  590. }
  591.  
  592.  
  593. void sobel_mask()
  594. {
  595. FOR(y, 1, H-2) FOR(x, 1, W-2)
  596. {
  597. double Gx = 0;
  598. double Gy = 0;
  599. FOR(yoff, -1, 1) FOR(xoff, -1, 1)
  600. {
  601. Gx += PE[UT][y+yoff][x+xoff] * GxM[xoff + 1][yoff + 1];
  602. Gy += PE[UT][y+yoff][x+xoff] * GyM[xoff + 1][yoff + 1];
  603. }
  604. double ta = (atan2(Gx,Gy)/3.14159) * 180.0;
  605. if (((ta > 112.5) && (ta < 157.5)) || ((ta < -22.5) && (ta > -67.5))) eD[y][x] = 135;
  606. if (((ta > 67.5) && (ta < 112.5)) || ((ta < -67.5) && (ta > -112.5))) eD[y][x] = 90;
  607. if (((ta > 22.5) && (ta < 67.5)) || ((ta < -112.5) && (ta > -157.5)))eD[y][x] = 45;
  608. if (((ta < 22.5) && (ta > -22.5)) || (ta > 157.5) || (ta < -157.5)) eD[y][x] = 0;
  609. grd[y][x] = sqrt(pow(Gx,2.0) + pow(Gy,2.0));
  610. }
  611. }
  612.  
  613.  
  614. void trace_edges()
  615. {
  616. FOR(y, 1, H-2) FOR(x, 1, W-2)
  617. {
  618. if (grd[y][x] > uTh)
  619. {
  620. if (eD[y][x] == 0) find_e(0, 1, y, x, 0); else
  621. if (eD[y][x] == 45) find_e(1, 1, y, x, 45); else
  622. if (eD[y][x] == 90) find_e(1, 0, y, x, 90); else
  623. if (eD[y][x] == 135) find_e(1, -1, y, x, 135); else
  624. PE[UT][y][x] = 0;
  625. }
  626. else PE[UT][y][x] = 0;
  627. }
  628. FOR(y, 0, H-1) FOR(x, 0, W-1) if (PE[UT][y][x] != 255) PE[UT][y][x] = 0;
  629. }
  630.  
  631.  
  632.  
  633. void find_e(int rs, int cs, int y, int x, int angle)
  634. {
  635. int _y;
  636. int _x;
  637. bool _end = 0;
  638.  
  639. if (cs < 0)
  640. {
  641. if (x > 0) _x = x + cs;
  642. else _end = 1;
  643. } else if (x < W - 1) _x = x + cs;
  644. else _end = 1;
  645.  
  646. if (rs < 0)
  647. {
  648. if (y > 0) _y = y + rs;
  649. else _end = 1;
  650. } else if (y < H - 1) _y = y + rs;
  651. else _end = 1;
  652.  
  653. while ((grd[_y][_x] > lTh) && (eD[_y][_x]==angle) && !_end )
  654. {
  655. PE[UT][_y][_x] = 255;
  656. if (cs < 0)
  657. {
  658. if (_x > 0) _x = _x + cs;
  659. else _end = 1;
  660. } else if (_x < W - 1) _x = _x + cs;
  661. else _end = 1;
  662.  
  663. if (rs < 0)
  664. {
  665. if (_y > 0) _y = _y + rs;
  666. else _end = 1;
  667. } else if (_y < H - 1) _y = _y + rs;
  668. else _end = 1;
  669. }
  670. }
  671.  
  672. void make_edges()
  673. {
  674. FOR(i, 1, 20)
  675. {
  676. if (i!=3 && i!=5 && i!=7 && i!=10) continue;
  677. UT = i;
  678. uTh = 10*i;
  679. lTh = 5*i;
  680.  
  681. gaussian_blur();
  682. sobel_mask();
  683. trace_edges();
  684. }
  685. }
  686.  
  687. ///----------------------------------------------------------------------------------------
  688.  
  689. void make_array(vector<int> data)
  690. {
  691. REP(i, H) REP(j, W) P[i][j] = data[H*j+i];
  692. }
  693.  
  694. void make_answers()
  695. {
  696. REP(i, AA.size())
  697. {
  698. stringstream ss;
  699. ss << AA[i].name << " " << AA[i].minx << " " << AA[i].miny << " " << AA[i].maxx << " " << AA[i].maxy;
  700. //cerr << ss.str() << endl;
  701. R.PB(ss.str());
  702. }
  703. }
  704.  
  705. int diff_calc(int y, int x)
  706. {
  707. return abs(P[y][x] - lights[y/100][x/100]);
  708. }
  709.  
  710. int diff_calc_e7(int y, int x) { return PE[7][y][x];}
  711.  
  712. int frag_calc(int y, int x)
  713. {
  714. int fr = 0;
  715. if (x < W-1) fr = max(fr, abs(P[y][x+1] - P[y][x]));
  716. if (x > 0) fr = max(fr, abs(P[y][x-1] - P[y][x]));
  717. if (y < H-1) fr = max(fr, abs(P[y-1][x] - P[y][x]));
  718. if (y > 0) fr = max(fr, abs(P[y+1][x] - P[y][x]));
  719. return fr;
  720. }
  721.  
  722. LD calc_best_circle(rectangle &RC)
  723. {
  724.  
  725. int minr = min(RC.maxx-RC.minx, RC.maxy-RC.miny) * 0.5;
  726. int maxr = max(RC.maxx-RC.minx, RC.maxy-RC.miny);
  727.  
  728. if (minr < 10) return -1.;
  729. if (maxr > 80) return -1.;
  730. if (RC.probe >= 1) return -1.;
  731. if (isTimeup()) return -1.;
  732. if (name[name.size()-3] != 't') minr = max(15, minr); ///EDIT 1.8.5
  733. //cerr << "MR = " << minr << " XR = " << maxr << endl;
  734.  
  735. LD ans = 0;
  736.  
  737. FOR(km, minr, maxr)
  738. {
  739. double delta = 3;
  740. FOR(sy, RC.miny, RC.maxy-km-1)
  741. {
  742. //cerr << "HERE1" << endl;
  743. double cd, ci, ced, cei, fra, fri;
  744. cd = 0;
  745. ci = 0;
  746. ced = 0;
  747. cei = 0;
  748. fra = 0;
  749. fri = 0;
  750.  
  751. int cx = RC.minx + km/2;
  752. int cy = sy + km/2;
  753.  
  754. int NYO[1500];
  755. int MYO[1500];
  756. int NYO2[1500];
  757. int MYO2[1500];
  758.  
  759. /*
  760. REP(i, 1500) NYO[i] = -1;
  761. REP(i, 1500) MYO[i] = -1;
  762. REP(i, 1500) NYO2[i] = -1;
  763. REP(i, 1500) MYO2[i] = -1;
  764. */
  765. memset(NYO, -1, sizeof(NYO));
  766. memset(MYO, -1, sizeof(NYO));
  767. memset(NYO2, -1, sizeof(NYO));
  768. memset(MYO2, -1, sizeof(NYO));
  769.  
  770. FOR(y, sy, sy+km) FOR(x, RC.minx, RC.minx + km)
  771. {
  772. //cerr << "HERE2" << endl;
  773. int xdist = abs(x-cx);
  774. int ydist = abs(y-cy);
  775. double dist = sqrt ( pow((double)xdist, 2) + pow((double)ydist, 2) );
  776.  
  777. if (dist > ((LD)km * (0.5)))
  778. {
  779. cd += diff_calc_e7(y, x);
  780. ci += 1;
  781. } else
  782. if (dist < ((LD)km*0.5 -delta))
  783. {
  784. ced += diff_calc_e7(y, x);
  785. cei += 1;
  786. if (NYO[y] == -1) NYO[y] = x;
  787. if (MYO[y] == -1) MYO[y] = x;
  788. NYO[y] = min(NYO[y], x);
  789. MYO[y] = max(MYO[y], x);
  790. } else
  791. {
  792.  
  793. fra+= diff_calc_e7(y, x);
  794. fri++;
  795.  
  796. if (NYO2[y] == -1) NYO2[y] = x;
  797. if (MYO2[y] == -1) MYO2[y] = x;
  798. NYO2[y] = min(NYO2[y], x);
  799. MYO2[y] = max(MYO2[y], x);
  800.  
  801. }
  802.  
  803. }
  804.  
  805. //cerr << ci << " " << cei << " " << fri << endl;
  806. //cerr << cd << " " << ced << " " << fra << endl;
  807. //FOR(i, 0, 50) cerr << NYO[i] << endl;
  808. //FOR(i, 0, 50) cerr << MYO[i] << endl;
  809. //FOR(i, 0, 50) cerr << NYO2[i] << endl;
  810. //FOR(i, 0, 50) cerr << MYO2[i] << endl;
  811.  
  812. FOR(j, RC.minx, RC.maxx-km)
  813. {
  814. double ratio;
  815. if (fri > 0 && cei > 0 && fra > 0 && fri > 0) ratio = (fra/fri) / (ced/cei) ;
  816. else ratio = (fra==0 && ced > 0) ? 100 : 0;
  817. double frag = (fri==0)?0:fra/fri;
  818. //cerr << cd << " " << ci << " " << ced << " " << cei << endl;
  819. //if (cei > 0 && ci > 0 && fri > 0) cerr << "Y = " << sy << " X = " << j << " RATIO = " << ratio << " CENTERDIF = " << (ced/cei) << " CORNERDIFF = " << (cd/ci) << " FRAG = " << frag << endl;
  820. double cfrag = (cei==0)?0:ced/cei;
  821.  
  822. ans = max (ans, (LD)(frag/255.));
  823. FOR(y, sy, sy+km)
  824. {
  825. cd += diff_calc_e7(y,j+km+1);
  826. if (MYO2[y] != -1)
  827. {
  828. cd -= diff_calc_e7(y, MYO2[y]);
  829. fra += diff_calc_e7(y, MYO2[y]);
  830. }
  831.  
  832. if (MYO[y] != -1)
  833. {
  834. fra -= diff_calc_e7(y, MYO[y]);
  835. ced += diff_calc_e7(y, MYO[y]);
  836. }
  837.  
  838. if (NYO[y] != -1)
  839. {
  840. ced -= diff_calc_e7(y, NYO[y]);
  841. fra += diff_calc_e7(y, NYO[y]);
  842. }
  843.  
  844. if (NYO2[y] != -1)
  845. {
  846. fra -= diff_calc_e7(y, NYO2[y]);
  847. cd += diff_calc_e7(y, NYO2[y]);
  848. }
  849. cd -= diff_calc_e7(y,j);
  850.  
  851. if (NYO[y] != -1) NYO[y]++;
  852. if (MYO[y] != -1) MYO[y]++;
  853. if (NYO2[y] != -1) NYO2[y]++;
  854. if (MYO2[y] != -1) MYO2[y]++;
  855. }
  856. }
  857. }
  858. }
  859. return ans;
  860. }
  861.  
  862. void calc_rectangle(rectangle &RC)
  863. {
  864. if (RC.algo==2) {calc_score(RC); return;}
  865. double cd, ci, ced, cei, fra, fri, dil, dip, cf, cef, frd;
  866. cd = 0;
  867. cf = 0;
  868. ci = 0;
  869. ced = 0;
  870. cef = 0;
  871. cei = 0;
  872. fra = 0;
  873. fri = 0;
  874. frd = 0;
  875. dil = 0;
  876. dip = 0;
  877.  
  878. int cx = RC.minx + (RC.maxx-RC.minx)/2;
  879. int cy = RC.miny + (RC.maxy-RC.miny)/2;
  880.  
  881. //kwadrat_size = RC.maxy - RC.miny;
  882.  
  883. //RC.bestcirc = calc_best_circle(RC);
  884. LD crincir = 0;
  885. LD nocrincir = 0;
  886.  
  887. FOR(y, RC.miny, RC.maxy) FOR(x, RC.minx, RC.maxx)
  888. {
  889. int xdist = abs(x-cx);
  890. int ydist = abs(y-cy);
  891. double dist = sqrt ( pow((double)xdist, 2) + pow((double)ydist, 2) );
  892.  
  893. if (dist > kwadrat_size*0.50) continue;
  894.  
  895. if (P[y][x] > lights[y/100][x/100]-35) nocrincir++;
  896. else crincir++;
  897. }
  898.  
  899. RC.cratincircle = (crincir==0)?0:(crincir / (nocrincir+crincir));
  900.  
  901. //cx-=2;
  902. RC.cx = cx;
  903. RC.cy = cy;
  904.  
  905. FOR(y, RC.miny, RC.maxy) FOR(x, RC.minx, RC.maxx)
  906. {
  907. int xdist = abs(x-cx);
  908. int ydist = abs(y-cy);
  909. double dist = sqrt ( pow((double)xdist, 2) + pow((double)ydist, 2) );
  910.  
  911. if (dist > ((LD)kwadrat_size * 0.5))
  912. {
  913. cd += abs(P[y][x] - lights[y/100][x/100]);
  914. cf += frag_calc(y,x);
  915. ci += 1;
  916. } else
  917. if (dist < ((LD)kwadrat_size * 0.3))
  918. {
  919. ced += abs(P[y][x] - lights[y/100][x/100]);
  920. cef += frag_calc(y,x);
  921. cei += 1;
  922. } else
  923. {
  924.  
  925. fra+=frag_calc(y,x);
  926. frd+=abs(P[y][x] - lights[y/100][x/100]);
  927. fri++;
  928. }
  929.  
  930. if (x <= cx) dil += P[y][x]; else dip += P[y][x];
  931. }
  932.  
  933. RC.sldp = (dip>0) ? dil/dip : 0;
  934.  
  935. double ratio;
  936. double fragm = (fri>0) ? fra/fri : 0;
  937.  
  938. if (cd > 0 && ci > 0 && cd/ci > 0 )
  939. {
  940. ratio = ((ced/cei)/(cd/ci));
  941. }
  942.  
  943. RC.corndiff = (ci==0)? 0:(cd/ci);
  944. RC.cornfra = (ci==0) ? 0 : (cf/ci);
  945. RC.cenfra = (ci==0) ? 0 : (cef/cei);
  946. RC.cendiff = (cei==0)?0:(ced/cei);
  947. RC.frd = (fri==0)?0:(frd/fri);
  948. RC.ratio = ratio;
  949. RC.fraci = fragm;
  950. calc_score(RC);
  951. }
  952.  
  953.  
  954. /**
  955. * if (l.fraci > 10) scorel = 100;
  956.   if (l.crater < 0.3) scorel += 100;
  957.   if (l.ratio < 1.5) scorel *= 0.5;
  958.   if (l.pole > 6000) scorel += 50; ///EDIT
  959.   if (l.maxx-l.minx < 27 || l.maxy-l.miny < 27) scorel += 50;
  960.   */
  961.  
  962. void calc_score(rectangle &l)
  963. {
  964. if (l.algo == 2) {l.score = 200+l.ratio2; return;}
  965. LD scorel = 100;
  966. if (l.frag < 50 && l.frag > 0) scorel = abs(5 - l.frag);
  967. else scorel = 1000000;
  968.  
  969.  
  970. // if (l.differ > 30) scorel *= 0.5;
  971. if (l.maxx-l.minx < 25 || l.maxy-l.miny < 25) scorel+=2;
  972.  
  973. if (l.sldp > 0.9) scorel += 1;
  974. if (l.sldp > 1.2) scorel += 50;
  975. if (l.ultrablack < 0.15 && (l.ultrablack < (l.black/2))) scorel += 200;
  976. if (l.rzp > 0.6) scorel+=1;
  977. if (l.minx < 10 || l.miny < 10 || l.maxx > 590 || l.maxy > 390) scorel += 100;
  978. if (l.name[l.name.size()-3] == 't')
  979. {
  980. scorel = abs(l.frag - 30);
  981. if (l.differ > 40) scorel *= 0.5;
  982. scorel += 100;
  983. }
  984.  
  985. if (l.cratincircle < 0.9) scorel+=1;//100;
  986. if (l.cratincircle < 0.7) scorel+=100;//100;
  987. //if (l.bestcirc < 0.1 && l.bestcirc != -1) scorel+=10;
  988. if (l.cratincircle < 0.1) scorel+=10000000;//100;
  989.  
  990. double ldx = l.maxx-l.minx;
  991. double ldy = l.maxy-l.miny;
  992.  
  993. if (ldx > 1 && ldy > 1)
  994. {
  995. if ((max(ldx, ldy) / min(ldx, ldy)) > 1.2) scorel += 0.7;
  996. if ((max(ldx, ldy) / min(ldx, ldy)) > 1.4) scorel += 100;
  997. }
  998. if (l.name[l.name.size()-3] == 't' && l.black < 0.1) scorel+=10000;
  999. if ((l.frag < 0.1 || l.differ < 6) ) scorel += 10000;
  1000. if (l.name[l.name.size()-3] != 't' && (l.maxx-l.minx < 16 || l.maxy-l.miny < 16)) scorel += 100000;
  1001. if (max(l.maxx-l.minx, l.maxy-l.miny) > 260) scorel += 100000;
  1002. scorel += l.probe * 1000000;
  1003. if (l.maxx-l.minx < 10 || l.maxy-l.miny < 10) scorel += 10000000;
  1004. if (max(l.maxx-l.minx, l.maxy-l.miny) > 350) scorel += 10000000;
  1005. l.score = scorel;
  1006. ZA++;
  1007. }
  1008.  
  1009. void sort_answers()
  1010. {
  1011. //int num = 0;
  1012. //REP(i, AA.size()) if (AA[i].probe == 0 && AA[i].name[AA[i].name.size()-3] == 't') num++;
  1013. //cerr << num << endl;
  1014. //REP(i, AA.size()) calc_score(AA[i]);
  1015. sort(ALL(AA), crc);
  1016. }
  1017.  
  1018.  
  1019. ///----------------------------------------------------------------------------------------------
  1020.  
  1021.  
  1022. int minx, miny, maxx, maxy;
  1023. int xx, xy, nx, ny;
  1024. int cratile;
  1025. int blackile;
  1026. int ultrablack;
  1027. int frag;
  1028. int differ;
  1029. int rzp;
  1030. vector<int> fragmenty;
  1031. int aktualny_fragment;
  1032.  
  1033. void add_rect()
  1034. {
  1035. if (xx<(W-(10)-1)) xx+= 10;
  1036.  
  1037. int pole = (xx-nx)*(xy-ny); if (pole==0) return;
  1038.  
  1039. rectangle A;
  1040. A.probe = TM;
  1041. A.pole = pole;
  1042. A.black = (LD)blackile/pole;
  1043. A.crater = (LD)cratile/pole;
  1044. A.frag = (LD)frag/pole;
  1045. A.ultrablack = (LD)ultrablack/pole;
  1046. A.differ = (LD)differ/pole;
  1047. A.rzp = (LD)rzp/cratile;
  1048. A.minx = nx;
  1049. A.miny = ny;
  1050. A.maxx = xx;
  1051. A.maxy = xy;
  1052. A.name = name;
  1053. A.md = 0;
  1054. A.uniq = 0;
  1055. A.algo = 1;
  1056. A.fragmenty = fragmenty;
  1057. calc_rectangle(A);
  1058. if (AA.size() > 90000) return;
  1059. int six = xx-nx;
  1060. int siy = xy-ny;
  1061.  
  1062. if (six==0 || siy==0 || (max(six, siy) / min(six, siy)) > 5) return;
  1063.  
  1064. AA.PB(A);
  1065. }
  1066.  
  1067. void dfs2_clear_global(int y, int x)
  1068. {
  1069. fragmenty.clear();
  1070. aktualny_fragment = 0;
  1071. xx = x;
  1072. nx = x;
  1073. ny = y;
  1074. xy = y;
  1075. cratile = 0;
  1076. blackile = 0;
  1077. ultrablack = 0;
  1078. frag=0;
  1079. differ=0;
  1080. rzp=0;
  1081. }
  1082.  
  1083. void start_dfs2(int y, int x)
  1084. {
  1085. if(name[name.size()-3] == 't')
  1086. {
  1087. FOR(i, -10, 10) FOR(j, -10, 10)
  1088. {
  1089. if (x+j<0 || y+i<0 || x+j>W-1 || y+i>H-1 || crat[y+i][x+j]) continue;
  1090. dfs2_clear_global(y, x);
  1091. //if (aktualny_fragment != 0) fragmenty.PB(aktualny_fragment);
  1092. //aktualny_fragment=0;
  1093. dfs2(y+i,x+j);
  1094. if (aktualny_fragment > 50 || TM!=0) {aktualny_fragment = 0; add_rect();}
  1095. }
  1096. }else
  1097. {
  1098. dfs2_clear_global(y, x);
  1099. FOR(i, -10, 10) FOR(j, -10, 10)
  1100. {
  1101. if (x+j<0 || y+i<0 || x+j>W-1 || y+i>H-1 || crat[y+i][x+j]) continue;
  1102. if (aktualny_fragment != 0) fragmenty.PB(aktualny_fragment);
  1103. aktualny_fragment=0;
  1104. dfs2(y+i,x+j);
  1105. }
  1106. if (aktualny_fragment != 0) fragmenty.PB(aktualny_fragment);
  1107. if (cratile > 150) add_rect();
  1108.  
  1109. }
  1110. //if (aktualny_fragment != 0) fragmenty.PB(aktualny_fragment);
  1111. }
  1112.  
  1113. bool dfs2(int y, int x)
  1114. {
  1115. if (x<0 || y<0 || x>W-1 || y>H-1 || crat[y][x]) return 0;
  1116. crat[y][x]=1;
  1117.  
  1118. if (TM == 0) if (P[y][x] > lights[y/100][x/100]-35) return 0;
  1119. if (TM == 1) if (P[y][x] > lights[y/100][x/100]-55) return 0;
  1120. if (TM == 2) if (P[y][x] > lights[y/100][x/100]-25) return 0;
  1121. if (TM == 3) if (P[y][x] > lights[y/100][x/100]-45) return 0;
  1122. if (TM == 4) if (P[y][x] > lights[y/100][x/100]-15) return 0;
  1123. if (TM == 5) if (P[y][x] > lights[y/100][x/100]-65) return 0;
  1124. if (TM == 6) if (P[y][x] > lights[y/100][x/100]-5) return 0;
  1125. if (TM == 7) if (P[y][x] > lights[y/100][x/100]-75) return 0;
  1126. if (TM > 7) if (P[y][x] > lights[y/100][x/100]-35) return 0;
  1127.  
  1128. if (P[y][x] <= max(20, lights[y/100][x/100]-40)) blackile++;
  1129. if (P[y][x] <= max(10, lights[y/100][x/100]-60)) ultrablack++;
  1130.  
  1131.  
  1132. frag+=frag_calc(y, x);
  1133. if (frag_calc(y,x) > 10) rzp++;
  1134. differ += abs(P[y][x] - lights[y/100][x/100]);
  1135. cratile++;
  1136. aktualny_fragment++;
  1137.  
  1138. xx = max(xx,x);
  1139. xy = max(xy,y);
  1140. nx = min(nx,x);
  1141. ny = min(ny,y);
  1142.  
  1143.  
  1144. dfs2(y-1,x);
  1145. dfs2(y+1,x);
  1146. dfs2(y,x-1);
  1147. dfs2(y,x+1);
  1148.  
  1149. return 1;
  1150.  
  1151. }
  1152.  
  1153. void start_dfs(string name, int y, int x)
  1154. {
  1155. minx = x;
  1156. miny = y;
  1157.  
  1158. maxx=0;
  1159. maxy=0;
  1160.  
  1161. dfs(y,x);
  1162.  
  1163. miny*=kwadrat_size;
  1164. minx*=kwadrat_size;
  1165. maxy*=kwadrat_size;
  1166. maxx*=kwadrat_size;
  1167.  
  1168. minx = max(0, minx);
  1169. miny = max(0, miny);
  1170. maxy = min(H-1, maxy);
  1171. maxx = min(W-1, maxx);
  1172.  
  1173. int cx = (minx+maxx)/2;
  1174. int cy = (miny+maxy)/2;
  1175.  
  1176. start_dfs2(cy, cx);
  1177.  
  1178. }
  1179.  
  1180. void dfs(int y, int x)
  1181. {
  1182. if (!used[y][x]) return;
  1183. minx = min(minx, x);
  1184. miny = min(miny, y);
  1185. maxx = max(maxx, x);
  1186. maxy = max(maxy, y);
  1187. used[y][x]=0;
  1188. if (x!=W/kwadrat_size-1 && used[y][x+1]) dfs(y, x+1);
  1189. if (y!=H/kwadrat_size-1 && used[y+1][x]) dfs(y+1, x);
  1190. if (x!=0 && used[y][x-1]) dfs(y, x-1);
  1191. if (y!=0 && used[y-1][x]) dfs(y-1, x);
  1192. }
  1193.  
  1194. ///Speed Hough transform
  1195.  
  1196. void algo1(string name)
  1197. {
  1198. kwadrat_size = 13;//13;
  1199. if (name[name.size()-3] == 't') kwadrat_size = 8;
  1200. if (TM%7 == 1) kwadrat_size = 7;
  1201. if (TM%7 == 2) kwadrat_size = 4;
  1202. if (TM%7 == 3) kwadrat_size = 2;
  1203. kwadrat_sum = kwadrat_size*kwadrat_size*15;//*ZI;
  1204. if (TM%7 == 4) kwadrat_sum = kwadrat_size*kwadrat_size*25;//*ZI;
  1205. if (TM%7 == 5) kwadrat_sum = kwadrat_size*kwadrat_size*5;//*Z;
  1206. if (TM%7 == 6)
  1207. {
  1208. kwadrat_size = 20;
  1209. kwadrat_sum = kwadrat_size*kwadrat_size*30;
  1210. }
  1211.  
  1212. //kwadrat_sums = kwadrat_size*kwadrat_size*190;//*ZI;
  1213.  
  1214. check_light();
  1215. memset(used, 0, sizeof(used));
  1216. memset(crat, 0, sizeof(crat));
  1217.  
  1218. int kwadrat;
  1219.  
  1220. FOR(i, 0, H-kwadrat_size-1)
  1221. {
  1222. kwadrat = 0;
  1223. FOR(y, i, i+kwadrat_size-1) FOR(x, 0, kwadrat_size-1) kwadrat += P[y][x];
  1224. FOR(j, 0, W-kwadrat_size-1)
  1225. {
  1226. FOR(y, i, i+kwadrat_size-1) kwadrat -= P[y][j];
  1227. FOR(y, i, i+kwadrat_size-1) kwadrat += P[y][j+kwadrat_size];
  1228. if (kwadrat < kwadrat_sum && !used[i/kwadrat_size][j/kwadrat_size])
  1229. {
  1230. used[i/kwadrat_size][j/kwadrat_size]=1;
  1231. }
  1232. }
  1233. }
  1234.  
  1235. int k = 0;
  1236. REP(i, H/kwadrat_size-1) REP(j, W/kwadrat_size-1) if (used[i][j] == 1) {start_dfs(name, i, j);}
  1237. }
  1238.  
  1239.  
  1240. void check_light()
  1241. {
  1242. LL l_sum = 0;
  1243. memset(lights, 0, sizeof(lights));
  1244. FOR(i, 0, H-1)
  1245. {
  1246. FOR(j, 0, W-1)
  1247. {
  1248. l_sum += P[i][j];
  1249. lights[i/100][j/100] += P[i][j];
  1250. }
  1251. }
  1252. ///TODO: 43 ends
  1253. REP(i, 20) REP(j, 20) lights[i][j]/=10000;
  1254.  
  1255. light = l_sum / (W*H);
  1256. //cerr << light << endl;
  1257. kwadrat_sum = kwadrat_size * kwadrat_size * (light*0.2);
  1258. //cerr << "LIGHT: " << light << endl;
  1259. }
  1260.  
  1261. ///Speed Hough transform
  1262.  
  1263. void algo2(string name, double gran, double _ratio, int maxnum)
  1264. {
  1265.  
  1266. int inum, obro;
  1267. inum = 0;
  1268. obro = 0;
  1269. bool chosen[1500][1500];
  1270. memset(chosen, 0, sizeof(chosen));
  1271.  
  1272. FORD(km, 34, 10)
  1273. //FORD(km, 25, 25)
  1274. {
  1275. obro = 0;
  1276. //double delta = 0.15;
  1277. double delta = 3;
  1278. //if (km < 20) delta = 0.06;
  1279. //cerr << "----" << km << "----" << endl;
  1280. FOR(sy, 0, 1300)
  1281. //FOR(sy, 182, 182)
  1282. {
  1283. //if (sy%200 == 0) cerr << "*" << endl;
  1284. //if (inum > 1000 || (sy%100==0 && isTimeup())) break;
  1285. if (inum > maxnum || (sy%100==0 && isTimeup())) break;
  1286. kwadrat_size = km;
  1287. double cd, ci, ced, cei, fra, fri;
  1288. cd = 0;
  1289. ci = 0;
  1290. ced = 0;
  1291. cei = 0;
  1292. fra = 0;
  1293. fri = 0;
  1294.  
  1295.  
  1296.  
  1297. int cx = kwadrat_size/2;
  1298. int cy = sy + kwadrat_size/2;
  1299.  
  1300. int NYO[1500];
  1301. int MYO[1500];
  1302. int NYO2[1500];
  1303. int MYO2[1500];
  1304.  
  1305. REP(i, 1500) NYO[i] = -1;
  1306. REP(i, 1500) MYO[i] = -1;
  1307. REP(i, 1500) NYO2[i] = -1;
  1308. REP(i, 1500) MYO2[i] = -1;
  1309.  
  1310. FOR(y, sy, sy+kwadrat_size) FOR(x, 0, kwadrat_size)
  1311. {
  1312. int xdist = abs(x-cx);
  1313. int ydist = abs(y-cy);
  1314. double dist = sqrt ( pow((double)xdist, 2) + pow((double)ydist, 2) );
  1315.  
  1316. if (dist > ((LD)kwadrat_size * (0.5)))
  1317. {
  1318. cd += diff_calc_e7(y, x);
  1319. ci += 1;
  1320. } else
  1321. if (dist < ((LD)kwadrat_size*0.5 -delta))
  1322. {
  1323. ced += diff_calc_e7(y, x);
  1324. cei += 1;
  1325. if (NYO[y] == -1) NYO[y] = x;
  1326. if (MYO[y] == -1) MYO[y] = x;
  1327. NYO[y] = min(NYO[y], x);
  1328. MYO[y] = max(MYO[y], x);
  1329. } else
  1330. {
  1331.  
  1332. fra+= diff_calc_e7(y, x);
  1333. fri++;
  1334.  
  1335. if (NYO2[y] == -1) NYO2[y] = x;
  1336. if (MYO2[y] == -1) MYO2[y] = x;
  1337. NYO2[y] = min(NYO2[y], x);
  1338. MYO2[y] = max(MYO2[y], x);
  1339.  
  1340. }
  1341.  
  1342. }
  1343.  
  1344. FOR(j, 0, W-kwadrat_size-2)
  1345. {
  1346. double ratio;
  1347. if (fri > 0 && cei > 0 && fra > 0 && fri > 0) ratio = (fra/fri) / (ced/cei) ;
  1348. else ratio = (fra==0 && ced > 0) ? 100 : 0;
  1349. double frag = (fri==0)?0:fra/fri;
  1350. double cfrag = (cei==0)?0:ced/cei;
  1351.  
  1352. if (ratio > _ratio && frag > gran && sy > 20 && ((km > 15 || ratio > 1.5*_ratio)))
  1353. {
  1354.  
  1355. rectangle R;
  1356. R.minx = j;
  1357. R.maxx = j+kwadrat_size;
  1358. R.miny = sy;
  1359. R.maxy = sy+kwadrat_size;
  1360. R.name = name;
  1361. R.ratio2 = inum;
  1362. R.probe = 0;
  1363. R.md = 0;
  1364. R.uniq = 0;
  1365. R.algo = 2;
  1366.  
  1367. bool ok = 1;
  1368.  
  1369. FOR(yy, R.miny, R.maxy)
  1370. {
  1371. FOR(xx, R.minx, R.maxx) if (chosen[yy][xx]) ok = 0;
  1372. if (ok==0) break;
  1373. }
  1374.  
  1375. if (ok)
  1376. {
  1377. inum++;
  1378. FOR(yy, R.miny, R.maxy) FOR(xx, R.minx, R.maxx) chosen[yy][xx] = 1;
  1379. calc_rectangle(R);
  1380. AA.PB(R);
  1381. }
  1382. }
  1383. FOR(y, sy, sy+kwadrat_size)
  1384. {
  1385. cd += diff_calc_e7(y,j+kwadrat_size+1);
  1386. if (MYO2[y] != -1)
  1387. {
  1388. cd -= diff_calc_e7(y, MYO2[y]);
  1389. fra += diff_calc_e7(y, MYO2[y]);
  1390. }
  1391.  
  1392. if (MYO[y] != -1)
  1393. {
  1394. fra -= diff_calc_e7(y, MYO[y]);
  1395. ced += diff_calc_e7(y, MYO[y]);
  1396. }
  1397.  
  1398. if (NYO[y] != -1)
  1399. {
  1400. ced -= diff_calc_e7(y, NYO[y]);
  1401. fra += diff_calc_e7(y, NYO[y]);
  1402. }
  1403.  
  1404. if (NYO2[y] != -1)
  1405. {
  1406. fra -= diff_calc_e7(y, NYO2[y]);
  1407. cd += diff_calc_e7(y, NYO2[y]);
  1408. }
  1409. cd -= diff_calc_e7(y,j);
  1410.  
  1411. if (NYO[y] != -1) NYO[y]++;
  1412. if (MYO[y] != -1) MYO[y]++;
  1413. if (NYO2[y] != -1) NYO2[y]++;
  1414. if (MYO2[y] != -1) MYO2[y]++;
  1415. }
  1416. }
  1417. }
  1418. }
  1419. sleep(1);
  1420. }
  1421.  
  1422.  
  1423.  
  1424.  
  1425. void present()
  1426. {
  1427. check_light();
  1428. FOR(i, 34, 71)
  1429. {
  1430. FOR(j, 205, 246)
  1431. {
  1432. //if ( abs(P[i][j] - P[i][j-1]) > 30) cerr << " R "; else
  1433. //if ( abs(P[i][j] - P[i][j+1]) > 30) cerr << " R "; else
  1434. //if ( abs(P[i][j] - P[i-1][j]) > 30) cerr << " R "; else
  1435. //if ( abs(P[i][j] - P[i+1][j]) > 30) cerr << " R "; else
  1436. //cerr << " * ";
  1437. cerr << P[i][j]-lights[i/100][j/100];
  1438. if (P[i][j]-lights[i/100][j/100] <= -100) cerr << ""; else
  1439. if (P[i][j]-lights[i/100][j/100] <= -10) cerr << " "; else
  1440. if (P[i][j]-lights[i/100][j/100] <= -1) cerr << " "; else
  1441. if (P[i][j]-lights[i/100][j/100] <= 9) cerr << " "; else
  1442. if (P[i][j]-lights[i/100][j/100] <= 99) cerr << " "; else
  1443. cerr << " ";
  1444. }
  1445. cerr << endl;
  1446. }
  1447. }
  1448.  
  1449. void train_read_answers()
  1450. {
  1451. ///Read answers
  1452. fstream fs("GTF.lms", ios::in);
  1453. //fstream fs("training/LRO/GTF.lms", ios::in);
  1454. //fstream fs("training/A15/GTF.lms", ios::in);
  1455.  
  1456. string str;
  1457. string _name;
  1458. while(fs >> _name)
  1459. {
  1460. answer A;
  1461. A.name = _name;
  1462. A.used = 0;
  1463. int num;
  1464. string foo;
  1465. fs >> foo;
  1466. fs >> num;
  1467. fs >> foo;
  1468. REP(i, num)
  1469. {
  1470. int minx,miny,maxx,maxy;
  1471. fs >> minx >> foo >> miny >> foo >> maxx >> foo >> maxy >> foo;
  1472. rectangle R;
  1473. R.minx = minx;
  1474. R.miny = miny;
  1475. R.maxx = maxx;
  1476. R.maxy = maxy;
  1477. R.md = 0;
  1478. A.rectangles.PB(R);
  1479. }
  1480. answers.PB(A);
  1481. }
  1482.  
  1483. }
  1484.  
  1485. void train_init()
  1486. {
  1487. train_read_answers();
  1488. }
  1489.  
  1490. void train(string name)
  1491. {
  1492. int tid = -1;
  1493. int found = 0;
  1494. check_light();
  1495. REP(i, answers.size()) if (name == answers[i].name) tid = i;
  1496. //cerr << "ANS = " << answers[tid].rectangles.size() << endl;
  1497.  
  1498. int n = answers[tid].rectangles.size();
  1499.  
  1500. REP(i, H) REP(j, W) crat[i][j]=0;
  1501.  
  1502. REP(z, n)
  1503. {
  1504. int kwadrat;
  1505. bool can_found = 0;
  1506. int minx = answers[tid].rectangles[z].minx;
  1507. int miny = answers[tid].rectangles[z].miny;
  1508. int maxx = answers[tid].rectangles[z].maxx;
  1509. int maxy = answers[tid].rectangles[z].maxy;
  1510. FOR(i, miny, maxy-kwadrat_size-1)
  1511. {
  1512. kwadrat = 0;
  1513. FOR(y, i, i+kwadrat_size-1) FOR(x, minx, kwadrat_size-1) kwadrat += P[y][x];
  1514. FOR(j, minx, maxx-kwadrat_size-1)
  1515. {
  1516. FOR(y, i, i+kwadrat_size-1) kwadrat -= P[y][j];
  1517. FOR(y, i, i+kwadrat_size-1) kwadrat += P[y][j+kwadrat_size];
  1518. if (kwadrat < kwadrat_sum)
  1519. {
  1520. can_found = 1;
  1521. }
  1522. }
  1523. }
  1524. //if (!can_found) cerr << "NAME: " << name << " MINX: " << minx << " MINY: " << miny << endl;
  1525.  
  1526. int cx = (minx+maxx)/2;
  1527. int cy = (miny+maxy)/2;
  1528. //start_dfs2(cy,cx);
  1529.  
  1530. frag=differ=cratile=blackile=ultrablack;
  1531.  
  1532. FOR(i, miny, maxy) FOR(j, minx, maxx)
  1533. {
  1534. differ += abs(P[i][j] - lights[i/100][j/100]);
  1535. int fr = 0;
  1536. fr = max(fr, abs(P[i][j+1] - P[i][j]));
  1537. fr = max(fr, abs(P[i][j-1] - P[i][j]));
  1538. fr = max(fr, abs(P[i-1][j] - P[i][j]));
  1539. fr = max(fr, abs(P[i+1][j] - P[i][j]));
  1540. frag+=fr;
  1541. }
  1542.  
  1543. int pole = (maxx-minx) * (maxy-miny);
  1544. if (pole == 0) return;
  1545. LD cr = (LD)cratile/pole;
  1546. LD bc = (LD)blackile/pole;
  1547. LD ub = (LD)ultrablack/pole;
  1548. LD frg = (LD)frag/pole;
  1549. LD di = (LD)differ/pole;
  1550.  
  1551. cerr << "POLE = " << pole << " CR = " << cr << " BC = " << bc << " UB = " << ub << " FRG = " << frg << " DI = " << di << endl;
  1552. cerr << " MINX: " << minx << " MINY: " << miny << " MAXX: " << maxx << " MAXY: " << maxy << endl;
  1553. cerr << "MY " << " MINX: " << nx << " MINY: " << ny << " MAXX: " << xx << " MAXY: " << xy << endl;
  1554.  
  1555.  
  1556. rectangle A;
  1557. A.pole = pole;
  1558. A.black = (LD)blackile/pole;
  1559. A.crater = (LD)cratile/pole;
  1560. A.frag = (LD)frag/pole;
  1561. A.ultrablack = (LD)ultrablack/pole;
  1562. A.minx = nx;
  1563. A.miny = ny;
  1564. A.maxx = xx;
  1565. A.maxy = xy;
  1566. A.name = name;
  1567.  
  1568. AA.PB(A);
  1569.  
  1570. }
  1571.  
  1572.  
  1573. }
  1574.  
  1575.  
  1576. };
  1577.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'void CraterDetection::algo2(std::string, double, double, int)':
prog.cpp:1419:10: error: 'sleep' was not declared in this scope
   sleep(1);
          ^
stdout
Standard output is empty