Guest User

Srm 646 Div2 Medium

a guest
Jan 16th, 2015
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. #include<bits/stdc++.h>#include <sstream>
  2. #include <queue>
  3. #include <set>
  4. #include <map>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cctype>
  8. #include <cmath>
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13.  
  14. using namespace std;
  15. int mx[]={1,0,-1,0};
  16. int my[]={0,1,0,-1};
  17. int lowx,lowy;
  18. map<pair<int,int>,int > m,vis;
  19. bool flag[3002][3002];
  20. int dfs(int x,int y,int k)
  21. {
  22.      int ret =-1000000;
  23.      queue<pair< pair<int,int>,int> >q;
  24.      flag[x][y] = true;
  25.      q.push(make_pair(make_pair(x,y),k));
  26.      while(q.size())
  27.      {
  28.           int curx = q.front().first.first;
  29.           int cury = q.front().first.second;
  30.           int curk = q.front().second;
  31.           q.pop();
  32.  
  33.           for(int i=0;i<4;i++)
  34.           {
  35.                if(curk<=0) continue;
  36.                int nx = curx+mx[i];
  37.                int ny = cury+my[i];
  38.  
  39.                if(flag[nx][ny]==false && curk>0)
  40.                {
  41.                     flag[nx][ny] = true;
  42.                     q.push(make_pair(make_pair(nx,ny),curk-1));
  43.                     ret= max(ret,nx);
  44.                }
  45.           }
  46.      }
  47.      return ret-1002;
  48. }
  49. class TheGridDivTwo
  50. {
  51. public:
  52.     int find(vector <int> x, vector <int> y, int k)
  53.     {
  54.          m.clear();
  55.          vis.clear();
  56.          cout<<"OK"<<endl;
  57.          memset(flag,false,sizeof flag);
  58.          for(int i=0;i<x.size();i++)
  59.           {
  60.                flag[x[i]+1002][y[i]+1002] = true;
  61.           }
  62.           int ans = dfs(1002,1002,k);
  63.                return max(ans,0);
  64.  
  65.     }
  66. };
  67.  
  68.  
  69. template<typename T> void print( T a ) {
  70.     cerr << a;
  71. }
  72.  
  73. void print( long long a ) {
  74.     cerr << a << "L";
  75. }
  76.  
  77. void print( string a ) {
  78.     cerr << '"' << a << '"';
  79. }
  80.  
  81. template<typename T> void print( vector<T> a ) {
  82.     cerr << "{";
  83.     for ( int i = 0 ; i != a.size() ; i++ ) {
  84.         if ( i != 0 ) cerr << ", ";
  85.         print( a[i] );
  86.     }
  87.     cerr << "}" << endl;
  88. }
  89.  
  90. template<typename T> void assert_eq( int n, T have, T need ) {
  91.     if ( have == need ) {
  92.         cerr << "Case " << n << " passed." << endl;
  93.     } else {
  94.         cerr << "Case " << n << " failed: expected ";
  95.         print( need );
  96.         cerr << " received ";
  97.         print( have );
  98.         cerr << "." << endl;
  99.     }
  100. }
  101.  
  102. template<typename T> void assert_eq( int n, vector<T> have, vector<T> need ) {
  103.     if( have.size() != need.size() ) {
  104.         cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
  105.         print( have );
  106.         print( need );
  107.         return;
  108.     }
  109.     for( int i= 0; i < have.size(); i++ ) {
  110.         if( have[i] != need[i] ) {
  111.             cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
  112.             print( have );
  113.             print( need );
  114.             return;
  115.         }
  116.     }
  117.     cerr << "Case " << n << " passed." << endl;
  118. }
  119. void assert_eq( int n, string have, string need ) {
  120.     if ( have == need ) {
  121.         cerr << "Case " << n << " passed." << endl;
  122.     } else {
  123.         cerr << "Case " << n << " failed: expected ";
  124.         print( need );
  125.         cerr << " received ";
  126.         print( have );
  127.         cerr << "." << endl;
  128.     }
  129. }
  130.  
  131. int main( )
  132. {
  133.  
  134.     TheGridDivTwo objectTheGridDivTwo;
  135.  
  136.     //test case0
  137.     vector <int> param00;
  138.     param00.push_back(1);
  139.     param00.push_back(1);
  140.     param00.push_back(1);
  141.     param00.push_back(1);
  142.     vector <int> param01;
  143.     param01.push_back(-2);
  144.     param01.push_back(-1);
  145.     param01.push_back(0);
  146.     param01.push_back(1);
  147.     int param02 = 4;
  148.     int ret0 = objectTheGridDivTwo.find(param00,param01,param02);
  149.     int need0 = 2;
  150.     assert_eq(0,ret0,need0);
  151.  
  152.     //test case1
  153.     vector <int> param10;
  154.     param10.push_back(-1);
  155.     param10.push_back(0);
  156.     param10.push_back(0);
  157.     param10.push_back(1);
  158.     vector <int> param11;
  159.     param11.push_back(0);
  160.     param11.push_back(-1);
  161.     param11.push_back(1);
  162.     param11.push_back(0);
  163.     int param12 = 9;
  164.     int ret1 = objectTheGridDivTwo.find(param10,param11,param12);
  165.     int need1 = 0;
  166.     assert_eq(1,ret1,need1);
  167. //
  168. //  //test case2
  169.     vector <int> param20;
  170.     param20.push_back(1);
  171.     vector <int> param21;
  172.     param21.push_back(0);
  173.     int param22 = 1000;
  174.     int ret2 = objectTheGridDivTwo.find(param20,param21,param22);
  175.     int need2 = 1000;
  176.     assert_eq(2,ret2,need2);
  177.  
  178.     //test case3
  179.     vector <int> param30;
  180.     param30.push_back(1);
  181.     param30.push_back(0);
  182.     param30.push_back(0);
  183.     param30.push_back(-1);
  184.     param30.push_back(-1);
  185.     param30.push_back(-2);
  186.     param30.push_back(-2);
  187.     param30.push_back(-3);
  188.     param30.push_back(-3);
  189.     param30.push_back(-4);
  190.     param30.push_back(-4);
  191.     vector <int> param31;
  192.     param31.push_back(0);
  193.     param31.push_back(-1);
  194.     param31.push_back(1);
  195.     param31.push_back(-2);
  196.     param31.push_back(2);
  197.     param31.push_back(-3);
  198.     param31.push_back(3);
  199.     param31.push_back(-4);
  200.     param31.push_back(4);
  201.     param31.push_back(-5);
  202.     param31.push_back(5);
  203.     int param32 = 47;
  204.     int ret3 = objectTheGridDivTwo.find(param30,param31,param32);
  205.     int need3 = 31;
  206.     assert_eq(3,ret3,need3);
  207.     return 0;
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment